视图
- 介绍
视图(view)是一种虚拟存在的表。视图中的数据并不是在数据库中真实存在,行和列数据来自 定义视图的查询中使用的表,并且是在使用视图时动态生成的。
通俗的讲,视图只保存了查询的SQL逻辑,不保存查询结果。所以我们在创建视图的时候,主要的工作就落在创建这条SQL查询语句上。
#创建
create [or replace] view 视图名称[(列名列表)] as select语句 [with [cascaded | local] check option];
#查询
#查看创建视图语句:
show create view 视图名称;
#查看视图数据
select *from 视图名称...;
#修改
#方式一:
create [or replace] view 视图名称[(列名列表)] as select语句 [with [cascaded | local] check option ];
#方式二
alter view 视图名称[(列名列表)] as select语句 [with [cascaded |local] check option];
#删除
drop view [if exists] 视图名称 [,视图名称]...;
2、检查选项(cascaded 、local)
- 视图的检查选项
当使用with check option
子句创建视图时,MySQL会通过视图检查正在更改的每个行,例如 插入、更新、删除,以使其符合视图的定义。MySQL允许基于另一个视图创建视图,它还会检查依赖视图中的规则以保持一致性。为了确定检查的范围,MySQL提供了两个选项:cascaded和local
默认是cascaded
.
create or replace view stu_v_1 as select id ,name from tb_student where id <= 20 with local check option ;
create or replace view stu_v_1 as select id ,name from tb_student where id <= 20;
select *from stu_v_1;
insert into stu_v_1 values(12,'Tom');
insert into stu_v_1 values(30,'Tom');
create view stu_v_2 as select id,name from stu_v_1 where id>=10 with cascaded check option ;
insert into stu_v_2 values(7,'tom');
insert into stu_v_2 values(26,'tom1');
# 只有id在 [10,20]之间的才可以插入
insert into stu_v_2 values(15,'tom15');
create view stu_v_3 as select id,name from stu_v_1 where id <= 15 ;
insert into stu_v_3 values(11,'tom11');
insert into stu_v_3 values(17,'tom17');
#只可以插入id在[10,20]以内,因为stu_v_3并没有开启检查选项,且stu_v_3依赖于stu_v_2,而stu_v_2依赖于stu_v_1,所以会从stu_v_2开始查看是否在符合级联检查
insert into stu_v_3 values(28,'tom28');
3、视图的更新及作用
-
视图的更新
- 要使视图可更新,视图中的行与基础表中的行之间必须存在一对一的关系。如果视图包含以下任何一项,则该驶入不可更新:
- 聚合函数或者窗口函数(sum()、min()、max()、count()等)
- distinct
- group by
- having
- union 或者union all
-
作用
- 简单:视图不仅可以简化用户对数据的理解,也可以简化他们的操作。那些被经常使用的查询可以被定义为视图,从而使得用户不必为以后操作每次钉钉全部的条件。
- 安全:数据库可以授权,但不能授权到数据库特定行和特定列上。通过视图用户只能查询和修改他们所能见到的数据
- 数据独立:视图可以帮助用户屏蔽真实表结构变化带来的影响。
#local:
create or replace view stu_v_4 as select id,name from tb_student where id <= 15 with local check option;
insert into stu_v_4 values (11,'tom11');
insert into stu_v_4 values(16,'tom16');
create or replace view stu_v_5 as select id,name from stu_v_4 where id >=10 with local check option ;
#不能插入
insert into stu_v_5 values (9,'tom9');
insert into stu_v_5 values(16,'tom16');
# 需要检查插入数据的id是否在 [10,15]之间,因为 stu_v_5是基于stu_v_4创建的,且stu_v_4需要进行选项检查
insert into stu_v_5 values(14,'tom14');
create or replace view stu_v_6 as select id ,name from stu_v_5 where id <20;
# 需要检查插入数据的id是否在 [10,15]之间,因为stu_v_6是基于stu_v_5创建的 stu_v_5是基于stu_v_4创建的,且stu_v_4和stu_v_5需要进行选项检查
insert into stu_v_6 values (12,'tom12');
4、案例
- 视图名不得与表名相同
#为了保证数据库表的安全性,开发人员在操作tb_user表时,只能看到的用户的基本字段,屏蔽手机号和邮箱两个字段。
create or replace view tb_user_view as select id,name,age,profession,gender,status,createtime from tb_user;
select *from tb_user_view;
#查询每个学生所选修的课程(三张表联查),这个功能在很多业务中都有使用到,为了简化操作,定义一个视图
select s.no,s.name,c.name from tb_student s,tb_student_course sc,tb_course c where s.id = sc.studentid and c.id = sc.courseid;
create or replace view tb_student_course_view as select s.no stu_no,s.name stu_name,c.name course_name from tb_student s,tb_student_course sc,tb_course c where s.id = sc.studentid and c.id = sc.courseid;
select *from tb_student_course_view;
如有侵权请联系删除
此篇来源于黑马程序员的课程,感谢黑马!!!
最后学习累了吧,福利在这里