概念:视图是一张虚拟表,它是从数据库的一张或多张表中导出的表,其内容由查询语句定义。
作用:
环境:centos7、mysql版本如下图:

注:创建视图需要CREATE VIEW权限,同时需要查询涉及的列的SELECT权限。
查看创建视图的权限:
select select_priv,create_view_priv from mysql.user where user=‘用户名’;
mysql> select select_priv,create_view_priv from mysql.user where user='root';
+-------------+------------------+
| select_priv | create_view_priv |
+-------------+------------------+
| Y | Y |
| Y | Y |
| Y | Y |
+-------------+------------------+
3 rows in set (0.01 sec)
以下出现的实验例子都基于表ployeelnfo,以下是该表结构和表内容:

创建视图info_view,显示年龄大于20岁的聘任人员id,name,sex,address信息:
mysql> create view info_view as select id,name,sex,address from employeelnfo where Age>20;
Query OK, 0 rows affected (0.01 sec)
查看视图info_view的基本结构和详细结构:
mysql> desc info_view;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(4) | NO | | 0 | |
| name | varchar(20) | NO | | NULL | |
| sex | varchar(4) | NO | | NULL | |
| address | varchar(20) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
3.查看视图info_view的所有记录。
mysql> select * from info_view;
+----+-----------+-----+--------------------+
| id | name | sex | address |
+----+-----------+-----+--------------------+
| 2 | 李广 | 男 | 北京市昌平区 |
| 4 | 赵一枚 | 女 | 浙江宁波市 |
+----+-----------+-----+--------------------+
修改视图info_view,满足年龄小于20岁的聘任人员:id,name,sex,address信息。
mysql> alter view info_view as
-> select id,name,sex,address from employeelnfo where Age<20
-> with check option;
Query OK, 0 rows affected (0.00 sec)
更新视图,将id号为3的聘任员的性别,由“男”改为“女”:
mysql> update info_view set Sex='女' where Id=3;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
删除info_view视图:
mysql> drop view info_view;
Query OK, 0 rows affected (0.00 sec)