#视图
/*
含义:虚拟表,和普通表一样使用,数据来自定义视图的使用的原始表,并且是动态生成的,只保存sql逻辑,不保存查询结果。
mysql5.1版本出现的新特性,是通过表动态生成的数据
应用场景:
多个地方用到了相同的查询结果
该查询结果使用的sql语句较为复杂
比如:舞蹈班和普通班的对比,舞蹈班来自于普通班,在MySQL中就是查询普通班的结果集,可以把这个结果集变为一个动态的表,下次来查询的时候,可以直接查结果集。
使用视图的好处:
1.重用sql语句
2.简化复杂的sql操作,不必知道查询细节
3.保护数据,提高安全性(只能看视图,却不知道视图的原始表(数据)来自于哪里,或者有哪些列字段,只提供关键信息供其他人调用)
视图和表的对比
创建语法的关键字 | 是否实际占用物理空间 | 使用上 | |
视图 | create view | 只是保存sql逻辑 | 增删改查(一般不能增删改) |
表 | create table | 保存了数据 | 增删改查 |
*/
DESC stuinfo;
#案例:查询性张的学生名和专业名
SELECT stuName,majorName
FROM stuinfo s
INNER JOIN major m ON s.`marjorId` = m.`id`
WHERE s.`stuName` LIKE '张%';
CREATE VIEW v1
AS
SELECT stuName,majorName
FROM stuinfo s
INNER JOIN major m ON s.`marjorId` = m.`id`
WHERE s.`stuName` LIKE '张%';
SELECT * FROM v1 WHERE stuName LIKE '张%';
#一、创建视图
/*
语法:
create view 视图名
as 查询语句;(一般是一个比较复杂语句)
*/
#1.查询姓名中中包含a字符的员工名、部门名和工种信息
#①创建
CREATE VIEW myv1
AS
SELECT last_name,department_name,job_title
FROM employees e
LEFT JOIN departments d ON e.department_id = d.department_id
LEFT JOIN jobs j ON j.job_id = e.job_id;
#②使用
SELECT * FROM myv1 WHERE last_name LIKE '%a%';
#2.查询各部门的平均工资级别
#创建视图查看每个部门的平均工资
CREATE VIEW myv2
AS
SELECT AVG(salary) ag,department_id
FROM employees e
GROUP BY department_id
#②使用
SELECT myv2.`ag`,g.grade_level
FROM myv2
LEFT JOIN job_grades g ON myv2.`ag`
BETWEEN g.`lowest_sal` AND g.`highest_sal`;
#3.查询平均工资最低的部门信息
SELECT * FROM myv2
ORDER BY ag LIMIT 1;
#4.查询平均工资最低的部门名和工资
CREATE VIEW myv3
AS
SELECT * FROM myv2 ORDER BY ag LIMIT 1;
SELECT d.*,m.ag
FROM myv3 m
LEFT JOIN departments d ON d.`department_id` = m.`department_id`
#二、视图的修改
#方式一:
/*
create or replace view 视图名
as
查询语句;
*/
CREATE OR REPLACE VIEW myv3
AS
SELECT AVG(salary),job_id
FROM employees
GROUP BY job_id;
#方式二:
/*
alter view 视图名
as
查询语句;
*/
ALTER VIEW myv3
AS
SELECT * FROM employees;
#三、删除视图
/*
语法:drop view 视图名,视图名,...;
*/
DROP VIEW myv1,myv2,myv3;
#四、查看视图结构
DESC myv3;
SHOW CREATE VIEW myv3;
#视图的更新(更改视图中的数据)
CREATE OR REPLACE VIEW myv1
AS
SELECT last_name,email
FROM employees;
SELECT * FROM myv1;
SELECT * FROM employees;
#1.插入数据
INSERT INTO myv1 VALUES('张飞','[email protected]');
#2.修改数据
UPDATE myv1 SET last_name = '张无忌' WHERE last_name = '张飞';
#3.删除
DELETE FROM myv1 WHERE last_name = '张无忌';
/*
视图可更新性和视图中查询的定义有关系,一下类型的视图是不能更新的。
1.包含一下关键字的sql语句:分组函数、distinct、group by、having、union或union all
2.常量视图
3.select中包含子查询
4.join
5.from一个不能更新的视图
6.where子句的子查询引用了from子句的表
*/
#1
CREATE OR REPLACE VIEW myv1
AS
SELECT MAX(salary) m,department_id
FROM employees
GROUP BY department_id;
#更新
UPDATE myv1 SET m = 9000 WHERE department_id=1;#更新失败:The target table myv1 of the UPDATE is not updatable
#2.常量视图
CREATE OR REPLACE VIEW myv2
AS
SELECT 'john' NAME;
#更新
UPDATE myv2 SET NAME = '11';
#3.select 中包含子查询
CREATE OR REPLACE VIEW myv3
AS
SELECT (SELECT MAX(salary) FROM employees) 最高工资;
#更新
UPDATE myv3 SET 最高工资 = 10000;
#4.join.注:不一定要用到join,只要是链接就算。
CREATE OR REPLACE VIEW myv4
AS
SELECT last_name,department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id
#更新
UPDATE myv4 SET last_name = '张飞' WHERE last_name = 'Whalen'
INSERT INTO myv4 VALUES('陈真','dddd');
#可以updatea,但不能插入和删除
#5.from一个不能更新视图
CREATE OR REPLACE VIEW myv5
AS
SELECT * FROM myv3;
#更新
UPDATE myv5 SET 最高工资 = 10000;
#6.where子句的子查询引用了from子句中的表
CREATE OR REPLACE VIEW myv6
AS
SELECT last_name,email,salary
FROM employees
WHERE employee_id IN (
SELECT DISTINCT manager_id
FROM employees
WHERE manager_id IS NOT NULL
);
#更新
UPDATE myv6 SET salary = 10000 WHERE last_name = 'K_ing';