通俗的讲,视图就是一条SELECT语句执行后返回的结果集。所以我们在创建视图的时候,主要的工作就落在创建这条SQL查询语句上。
视图是对若干张基本表的引用,一张虚表,查询语句执行的结果,不存储具体的数据(基本表数据发生了改变,视图也会跟着改变);
可以跟基本表一样,进行增删改查操作(ps:增删改操作有条件限制);
方便操作,特别是查询操作,减少复杂的SQL语句,增强可读性;
更加安全,数据库授权命令不能限定到特定行和特定列,但是通过合理创建视图,可以把权限限定到行列级别;
权限控制的时候,不希望用户访问表中某些含敏感信息的列
关键信息来源于多个复杂关联表,可以创建视图提取我们需要的信息,简化操作;
代码实现:
# 学生表
CREATE TABLE students (
number CHAR(9), # 学号
name VARCHAR(20), # 姓名
klass VARCHAR(10), # 班级
age INT, # 年龄
birth DATE # 生日
);
INSERT INTO students
VALUES ('201804001', '刘一', 19, 16, '2002-01-01'),
('201804002', '陈二', 18, 17, '2001-01-02'),
('201804003', '张三', 19, 18, '2000-01-03'),
('201804004', '李四', 19, 19, '2001-01-04'),
('201804005', '王五', 19, 16, '2002-01-05'),
('201804006', '赵六', 18, 19, '1999-01-06'),
('201804007', '孙七', 19, 17, '2001-01-07'),
('201804008', '周八', 19, 18, '2000-01-08'),
('201804009', '吴九', 18, 17, '2001-01-09'),
('201804010', '郑十', 19, 18, '2000-01-10');
select * from students;
#结果打印:
+----+-----------+--------+------+------------+
| id | number | name | age | birth |
+----+-----------+--------+------+------------+
| 1 | 201804001 | 刘一 | 16 | 2002-01-01 |
| 2 | 201804002 | 陈二 | 17 | 2001-01-02 |
| 3 | 201804003 | 张三 | 18 | 2000-01-03 |
| 4 | 201804004 | 李四 | 19 | 2001-01-04 |
| 5 | 201804005 | 王五 | 20 | 2000-01-05 |
| 6 | 201804006 | 赵六 | 21 | 1999-01-06 |
| 7 | 201804007 | 孙七 | 22 | 1999-01-07 |
| 8 | 201804008 | 周八 | 23 | 1999-01-08 |
| 9 | 201804009 | 吴九 | 24 | 1999-01-09 |
| 10 | 201804010 | 郑十 | 25 | 1999-01-10 |
+----+-----------+--------+------+------------+
10 rows in set (0.11 sec)
创建一个视图:
# 封装了“19班”查询的视图
CREATE VIEW students_19 AS
SELECT * FROM students WHERE klass=19;
mysql> show tables;
+-------------------+
| Tables_in_python1 |
+-------------------+
| students |
| students_19 |
+-------------------+
2 rows in set (0.00 sec)
mysql> select * from student_19;
ERROR 1146 (42S02): Table 'python1.student_19' doesn't exist
mysql> select * from students_19;
+-----------+--------+-------+------+------------+
| number | name | klass | age | birth |
+-----------+--------+-------+------+------------+
| 201804001 | 刘一 | 19 | 16 | 2002-01-01 |
| 201804003 | 张三 | 19 | 18 | 2000-01-03 |
| 201804004 | 李四 | 19 | 19 | 2001-01-04 |
| 201804005 | 王五 | 19 | 16 | 2002-01-05 |
| 201804007 | 孙七 | 19 | 17 | 2001-01-07 |
| 201804008 | 周八 | 19 | 18 | 2000-01-08 |
| 201804010 | 郑十 | 19 | 18 | 2000-01-10 |
+-----------+--------+-------+------+------------+
7 rows in set (0.00 sec)
修改一个视图:
# 改为“18班”
ALTER VIEW students_19 AS
SELECT * FROM students WHERE klass=18;
mysql> ALTER VIEW students_19 AS
-> SELECT * FROM students WHERE klass=18;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from students_19;
+-----------+--------+-------+------+------------+
| number | name | klass | age | birth |
+-----------+--------+-------+------+------------+
| 201804002 | 陈二 | 18 | 17 | 2001-01-02 |
| 201804006 | 赵六 | 18 | 19 | 1999-01-06 |
| 201804009 | 吴九 | 18 | 17 | 2001-01-09 |
+-----------+--------+-------+------+------------+
3 rows in set (0.00 sec)