MySQL进阶:视图

-- 视图

-- 创建视图
create view EmployeeView as
    select id, name, age,depart,salary
from  employee where salary>=20000;

-- 删除视图
drop view if exists employeeview;

-- 修改视图
create or replace view EmployeeView as select id,name,age,depart,salary from employee where salary>=20000;
alter view employeeview as select id,name,age,depart,salary from employee where salary>=20000 ;

-- 查询视图
select * from EmployeeView;

-- 查看创建数据库语句
show create view employeeview;

-- 查看当前数据库所有视图
show table status where Comment = 'view';

-- 视图的作用

-- 简化查询语句(简化外连接多表查询)
create view SuperEmployeeView as
    select employee.id,employee.name,age,depart,salary,manager.name manager
from employee ,manager
where employee.mgr_id = manager.id;

select * from SuperEmployeeView;

-- 提高数据库安全性(某些敏感信息字段不展示)
-- 提供一定程度的数据逻辑独立性
-- 集中展示用户所感兴趣的特定数据

-- 视图不可更新条件
-- 如果视图包含下述结构中的一种,则视图不可更新
/*
(1)聚合函数;(2)DISTINCT关键字;(3)GROUP BY子句;
(4)ORDER BY子句;(5)HAVING子句;(6)UNION运算符;
(7)位于选择列表中的子查询;(8)FROM子句中包含多个表;
(9)SELECT语句中引用了不可更新视图;
(10)WHERE子句中的子查询,引用FROM子句中的表
*/

-- 视图的检验

create or replace view EmployeeView as select id,name,age,depart,salary from employee where salary>=20000;
create or replace view view_simpleEmp as select id, name ,age from employee where id < 10;   -- 创建视图
select *from view_simpleEmp;    -- 查询视图
select * from employee;
desc employee;
alter table employee modify depart varchar(30) comment '工作部门';

-- 有id<10但还是可以插到基表
insert into view_simpleEmp values(12,'王昭君',16);
select *from employee;     -- 成功添加数据
select *from view_simpleEmp;   -- 不用看了,本来就是显示id<10,视图里不可能有的



-- 全局检验:cascaded check option
create or replace view view_simpleEmp as select id, name ,age from employee where id < 25 with cascaded check option ;   -- 创建视图
-- 插入基表失败
insert into view_simpleEmp values(26,'西施',17);
-- 基于view_simpleEmp创建view1
create or replace view view1 as select * from test.view_simpleEmp where id>15;  -- 无检查
insert into view1 values(14,'貂蝉',18);  -- 插入数据,成功
select * from view1;
create or replace view view1 as select * from test.view_simpleEmp where id>15 with cascaded check option ;  -- 附加检查
insert into view1 values(14,'貂蝉',18);  -- 插入数据,失败
-- 接下来注意!cascaded检查特点:强制向上级进行检查
insert into test.view1 values(27,'卓文君',21);  -- 失败!可以理解,毕竟上级视图view——simpleEmp有cascaded检查,下面把检查删了
create or replace view view_simpleEmp as select id, name ,age from employee where id < 25;   -- 修改,无检查
insert into test.view1 values(27,'卓文君',21);  -- 还是失败,因为cascaded强制为上级视图附加检查




-- 本地检验:local check option
create or replace view view1 as select * from test.view_simpleEmp where id>15 with local check option ;  -- 附加本地检查
create or replace view view_simpleEmp as select id, name ,age from employee where id < 25;   -- 修改,无检查
insert into test.view1 values(27,'卓文君',21);  -- 插入成功,local不对上级视图进行检查
create or replace view view_simpleEmp as select id, name ,age from employee where id < 25 with cascaded check option ;   -- 附加检查
insert into test.view1 values(26,'杜夫人',22);  -- 还是成功,local不对上级视图进行检查

select *from employee;
select *from test.view1;
select *from test.view_simpleEmp;

你可能感兴趣的:(Mysql数据库,mysql,数据库,sql)