mysql> CREATE DATABASE viewdb; #创建练习库
#创建视图
mysql> CREATE VIEW viewdb.v1 AS
-> SELECT name,uid,shell FROM tarena.user;#创建视图引用原表字段名
mysql> CREATE VIEW viewdb.v2(用户名,用户id,解释器) AS
-> SELECT name,uid,shell FROM tarena.user;#创建视图自定义字段名
mysql> CREATE VIEW viewdb.emp(name,email,dept_name) AS
-> SELECT e.name,e.email,d.dept_name FROM
-> tarena.employees AS e INNER JOIN tarena.departments AS d
-> ON e.dept_id=d.dept_id; #联表视图
mysql> SHOW TABLES FROM viewdb; #查看指定库下表(视图是特殊表)
#使用视图(SELECT为主要场景)
mysql> SELECT * FROM viewdb.v1; #对v1执行查询操作
mysql> SELECT * FROM viewdb.v2; #对v2执行查询操作
mysql> SELECT * FROM viewdb.emp; #对emp执行查询操作
#查看视图
mysql> SHOW TABLE STATUS FROM viewdb\G #视图Comment: VIEW
mysql> SHOW TABLE STATUS FROM tarena\G #真实表Comment:
mysql> SHOW CREATE VIEW viewdb.emp; #查看创建视图完整语句
#删除使用
mysql> DROP VIEW viewdb.v1; #删除视图