MySQL之视图
什么是视图
介绍:
MySQL从5.0.1版本开始提供视图功能。可以理解成一种虚拟存在的表,行和列的数据来自定义视图的查询中使用的表,并且是在使用视图时动态生成的,只保存了sql逻辑,不保存查询结果。
应用场景:
- 多个地方用到同样的查询结果
- 该查询结果使用的sql语句较复杂
视图和表的区别:
表:占用物理空间。
视图:不占用物理空间,仅仅保存的是sql逻辑。
视图的好处:
-
sql语句提高重用性,效率高。
和表实现了分离,提高了安全性。
视图的创建
语法:
CREATE VIEW 视图名
AS
查询语句;
示例代码:
/* * 1.查询姓名中包含a字符的员工名、部门名和工种信息 */ #创建 CREATE VIEW my_view1 AS SELECT e.last_name,d.department_name,j.job_title FROM employees e JOIN departments d ON e.department_id = d.department_id JOIN jobs j ON j.job_id = e.job_id; #使用 SELECT * FROM my_view1 WHERE last_name LIKE '%a%'; /* * 2.查询平均工资最低的部门信息 */ #创建 CREATE VIEW my_view2 AS SELECT AVG(salary) ag_salary,department_id FROM employees GROUP BY department_id; #使用 SELECT * FROM my_view2 ORDER BY ag_salary LIMIT 1; /* * 3.查询平均工资最低的部门名和工资 */ #创建 CREATE VIEW my_view3 AS SELECT * FROM my_view2 ORDER BY ag_salary LIMIT 1; #使用 SELECT d.*,v.ag_salary FROM my_view3 v JOIN departments d ON v.department_id = d.department_id;
视图的修改
语法一:
CREATE OR REPLACE VIEW 视图名
AS
查询语句;
示例代码:
/* * 方式一 */ CREATE OR REPLACE VIEW my_view2 AS SELECT last_name FROM employees WHERE employee_id > 100;
语法二:
ALTER VIEW 视图名
AS
查询语句;
示例代码:
/* * 方式二 */ ALTER VIEW my_view2 AS SELECT employee_id FROM employees; SELECT * FROM my_view2;
视图的可更新性和视图中查询的定义有关系,以下类型的视图是不能更新的:
-
包含以下关键字的sql语句:分组函数、distinct、group by、having、union或者union all:
#创建 CREATE OR REPLACE VIEW my_view AS SELECT MAX(salary) m,department_id FROM employees GROUP BY department_id; #查询 SELECT * FROM my_view; #更新 UPDATE my_view SET m = 9000 WHERE department_id = 10;
-
常量视图:
#创建 CREATE OR REPLACE VIEW my_view AS SELECT 'john' NAME; #查询 SELECT * FROM my_view; #更新 UPDATE my_view SET NAME='lucy';
-
select中包含子查询:
#创建 CREATE OR REPLACE VIEW my_view AS SELECT department_id,(SELECT MAX(salary) FROM employees) 最高工资 FROM departments; #查询 SELECT * FROM my_view; #更新 UPDATE my_view SET 最高工资 = 100000;
-
join:
#创建 CREATE OR REPLACE VIEW my_view AS SELECT last_name,department_name FROM employees e JOIN departments d ON e.department_id = d.department_id; #查询 SELECT * FROM my_view; #更新 UPDATE my_view SET last_name = '张飞' WHERE last_name='Whalen'; INSERT INTO my_view VALUES('陈真','xxxx');
-
from一个不能更新的视图:
#创建 CREATE OR REPLACE VIEW my_view AS SELECT department_id,(SELECT MAX(salary) FROM employees) 最高工资 FROM departments; #查询 SELECT * FROM my_view; #更新 UPDATE my_view SET 最高工资 = 10000 WHERE department_id = 60;
-
where子句的子查询引用了from子句中的表:
#创建 CREATE OR REPLACE VIEW my_view AS SELECT last_name,email,salary FROM employees WHERE employee_id IN( SELECT manager_id FROM employees WHERE manager_id IS NOT NULL ); #查询 SELECT * FROM my_view; #更新 UPDATE my_view SET salary = 10000 WHERE last_name = 'k_ing';
视图的查看
语法:
DESC 视图名;
SHOW CREATE VIEW 视图名;
示例代码:
DESC my_view1; SHOW CREATE VIEW my_view1;
视图的删除
语法:
DROP VIEW 视图名,视图名,...;
示例代码:
DROP VIEW my_view2,my_view3;
视图数据的增删改查
1.查看视图的数据
SELECT * FROM my_view; SELECT * FROM my_view WHERE last_name='Partners';
2.插入视图的数据
INSERT INTO my_view(last_name,department_id) VALUES('虚竹',90);
3.修改视图的数据
UPDATE my_view SET last_name ='梦姑' WHERE last_name='虚竹';
4.删除视图的数据
DELETE FROM my_view;