MySQL基础-20视图

Posted Lz_蚂蚱

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL基础-20视图相关的知识,希望对你有一定的参考价值。

声明:此mysql基础学习源自尚硅谷。(推荐)b站官方链接:https://www.bilibili.com/video/BV1xW411u7ax?p=1

视图

含义:虚拟表,和普通表一样使用
MySQL从5.0.1版本出现的新特性。一种虚拟存在的表,行和列的数据来自定义视图的查询中使用的表,并且是在使用视图时动态生成的,只保存了sql逻辑,不保存查询结果。

应用场景:
	多个地方用到同样的查询结果
	该查询结果使用的sql语句较复杂

好处:1.重用sql语句  2.简化复杂的sql操作,不必知道它的查询细节  3.保护数据,提高安全性

案例:查询姓张的学生名和专业名

-- 原始操作
SELECT stuname,majorname
FROM stuinfo s
INNER JOIN major m ON s.`majorid`= m.`id`
WHERE s.`stuname` LIKE ‘张%‘;

-- 使用视图操作
CREATE VIEW v1
AS
SELECT stuname,majorname
FROM stuinfo s
INNER JOIN major m ON s.`majorid`= m.`id`;

-- 查询视图
SELECT * FROM v1 WHERE stuname LIKE ‘张%‘;

1.创建视图

语法:
create view 视图名
as
查询语句;

1.查询姓名中包含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%‘;

2.查询各部门的平均工资级别

#①创建视图查看每个部门的平均工资
CREATE VIEW myv2
AS
SELECT AVG(salary) ag,department_id
FROM employees
GROUP BY department_id;

#②使用
SELECT myv2.`ag`,g.grade_level
FROM myv2
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
JOIN departments d
ON m.`department_id`=d.`department_id`;

2.视图的修改

方式一:

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;

3.删除视图

语法:
drop view 视图名1,视图名2,...;
DROP VIEW emp_v1,emp_v2,myv3;

4.查看视图

# 方式1
DESC myv3;
# 方式2
SHOW CREATE VIEW myv3;

5.视图的更新

更新了视图中数据,原始表中也会更新视图中的数据

视图一般用于查询的,而不是更新的,一般视图也不会更新数据,由于视图更新限制条件,一般也不能更新

CREATE OR REPLACE VIEW myv1
AS
SELECT last_name,email
FROM employees;

SELECT * FROM myv1;

1.插入

INSERT INTO myv1 VALUES(‘张飞‘,‘zf@qq.com‘);

2.修改

UPDATE myv1 SET last_name = ‘张无忌‘ WHERE last_name=‘张飞‘;

3.删除

DELETE FROM myv1 WHERE last_name = ‘张无忌‘;

具备以下特点的视图不允许更新?

#①包含以下关键字的sql语句:分组函数、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; # 存在group by,不允许更新

SELECT * FROM myv1;

#更新失败
UPDATE myv1 SET m=9000 WHERE department_id=10;

#②常量视图
CREATE OR REPLACE VIEW myv2
AS
SELECT ‘john‘ NAME;

SELECT * FROM myv2;

#更新失败
UPDATE myv2 SET NAME=‘lucy‘;
#③Select中包含子查询
CREATE OR REPLACE VIEW myv3
AS
SELECT department_id,(SELECT MAX(salary) FROM employees) 最高工资
FROM departments;

SELECT * FROM myv3;

#更新失败
UPDATE myv3 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 myv4 SET last_name  = ‘张飞‘ WHERE last_name=‘Whalen‘;
INSERT INTO myv4 VALUES(‘陈真‘,‘xxxx‘);
#⑤from一个不能更新的视图
CREATE OR REPLACE VIEW myv5
AS
SELECT * FROM myv3;

SELECT * FROM myv5;

#更新失败
UPDATE myv5 SET 最高工资=10000 WHERE department_id=60;
#⑥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‘;

视图与实际表对比

创建语法关键字 是否实际占用物理空间 使用
视图 create view 只保存SQL逻辑 增删改查,一般不能增删改
create table 保存数据 增上改查

以上是关于MySQL基础-20视图的主要内容,如果未能解决你的问题,请参考以下文章

如何在片段中填充列表视图?

片段中的 notifyDataSetChanged() 不刷新列表视图

从mysql的片段中加载ListView

Android:NullPointerException 无法将数据库加载到片段内的列表视图中

在画布上绘制片段视图

Codeigniter 助手重复 HTML 代码片段