含义:本身是一个虚拟表,数据来自于表,通过执行时动态生成,和普通表一样使用(mysql5.1 版本出现的新特征)
好处:
- 简化复杂的SQL操作,不必知道它的查询细节
- 临时性
- 实现了SQL语句的重用
- 保护数据,提高安全性
比如:临时在23个班中临时组建舞蹈班
与表的对比
-- | 创建语法的关键字 | 是否实际占用物理空间 | 使用 |
---|---|---|---|
视图 | CREATE VIEW | 只保存了SQL逻辑所需的空间 | 增删改查,一般不能增删改 |
表 | CREATE TABLE | 占用 | 增删改查 |
案例:查询姓张的学生名和专业名
SELECT studentname,majorname
FROM student
INNER JOIN major
ON student.majorid = major.`majorid`
WHERE studentname LIKE '%张%';
视图:
CREATE VIEW v1
AS
SELECT studentname,majorname
FROM student
INNER JOIN major
ON student.majorid = major.`majorid`
WHERE studentname LIKE '%张%';
SELECT * FROM v1;
CREATE VIEW v2
AS
SELECT studentname,majorname
FROM student
INNER JOIN major
ON student.majorid = major.`majorid`;
SELECT * FROM v2
WHERE studentname LIKE '%张%';
一、创建视图
语法:CREATE VIEW 视图名
AS
查询语句;
创建
- 查询姓名中包含a字符的员工名、部门名和工种信息
CREATE VIEW myv1
AS
SELECT last_name,department_name,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 myv1 WHERE last_name LIKE '%a%';
查询各部门的工资级别
创建视图查看每个部门的平均工资
CREATE VIEW myv2
AS
SELECT AVG(salary) ag,department_id
FROM employees
GROUP BY department_id;
使用
SELECT m.ag,g.grade_level
FROM myv2 m
JOIN job_grades g
ON m.ag BETWEEN g.lowest_sal AND g.highest_sal;
查询平均工资最低的部门信息
SELECT * FROM myv2 ORDER BY ag LIMIT 1;
查询平均工资最低的部门名和工资
CREATE VIEW myv3
AS
SELECT * FROM myv2 ORDER BY ag LIMIT 1;
SELECT d.*,m.ag
FROM myv3 m
JOIN departments d
ON m.department_id = d.department_id;
视图的修改
方式一:
CREATE OR REPLACE VIEW 视图名
AS
查询语句;
SELECT * FROM myv3;
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
WHERE last_name = '张无忌';
1.插入
INSERT INTO myv1 VALUES('张飞','zf@qq.com');
2.修改
UPDATE myv1 SET last_name = '张无忌' WHERE last_name = '张飞';
3.删除
DELETE FROM myv1 WHERE last_name = '张无忌';
具备以下特点的视图不允许更新
包含分组函数、DISTINCT、GROUP BY、HAVING、UNION、UNION ALL
CREATE OR REPLACE VIEW myv1
AS
SELECT MAX(salary) m,department_id
FROM employees
GROUP BY department_id;
SELECT * FROM myv1;
#更新
UPDATE muv1 SET = 9000 WHERE department_id = 10;
常量视图
CREATE OR REPLACE VIEW myv2
AS
SELECT 'john' NAME;
SELECT * FROM myv2;
#更新
UPDATE myv2 SET NAME = 'luck';
select 中包含子查询
CREATE OR REPLACE VIEW myv3
AS
SELECT (SELECT MAX(salary) FROM employees) 最高工资;
SELECT * FROM myv3;
#更新
UPDATE myv2 SET 最高工资 = '100000';
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;
SELECT * FROM myv4;
#更新(可以update不能insert)
UPDATE myv4 SET last_name= '张飞' WHERE last_name = 'whalen';
INSERT INTO myv4 VALUE('真沉','xxx');
from一个不能更新的视图
CREATE OR REPLACE VIEW myv5
AS
SELECT * FROM myv3;
SELECT * FROM myv5;
UPDATE myv5 SET 最高工资 = 10000;
where子句的子查询引用了from子句中的表
CREATE OR REPLACE VIEW myv6
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 myv6;
UPDATE myv6 SET salary = 10000 WHERE last_name = 'k_ing';