备份与还原
视图概述
- VIEW 视图;
- 视图相当于虚拟表,不真正存放数据,存放的只是一段SQL语句的定义,从而简化查询;
- 视图保存有实表的查询结果,表更新,视图的查询结果也会更新;
- 视图还可以实现数据安全,因为它可以只显示真实表的部分列,或计算后的结果,从而隐藏真实表的数据;
- 可以通过视图来修改表内容,但视图通常只做查询 而不做修改。
创建视图语法
create view view_name as 查询的sql语句创建视图范例
create view test_view as SELECT classid 班级,count(classid) 总人数 FROM students GROUP BY classid
#实际上就相当于生成一张表
MySQL [hellodb]> show tables;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| test_view | #
| toc |
+-------------------+
8 rows in set (0.02 sec)查看视图
#和正常查询表一样,只不过相当于将sql语句集合了而已
#因为存储的只是sql语句的别名,所以视图的查询结果会根据sql语句中表的改变而改变
MySQL [hellodb]> select * from test_view;
+--------+-----------+
| 班级 | 总人数 |
+--------+-----------+
| 2 | 3 |
| 1 | 4 |
| 4 | 4 |
| 3 | 4 |
| 5 | 1 |
| 7 | 3 |
| 6 | 4 |
+--------+-----------+
7 rows in set (0.01 sec)查看视图定义
SHOW CREATE VIEW view_name #只能看视图定义
SHOW CREATE TABLE view_name # 可以查看表和视图查看是否为视图
#视图的显示
MySQL [hellodb]> show table status like 'test_view'\G;
*************************** 1. row ***************************
Name: test_view
Engine: NULL
Version: NULL
Row_format: NULL
Rows: NULL
Avg_row_length: NULL
Data_length: NULL
Max_data_length: NULL
Index_length: NULL
Data_free: NULL
Auto_increment: NULL
Create_time: 2021-11-27 20:42:29
Update_time: NULL
Check_time: NULL
Collation: NULL
Checksum: NULL
Create_options: NULL
Comment: VIEW #为视图
1 row in set (0.00 sec)
#正常表
MySQL [hellodb]> show table status like 'students'\G;
*************************** 1. row ***************************
Name: students
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 25
Avg_row_length: 655
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: 27
Create_time: 2021-11-26 20:59:20
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment: #不为视图则不会显示 删除视图
DROP VIEW view_name