视图

视图view

# 数据依赖:单表emp
"""
1)视图是存在内存中的临时表
2)视图的创建依赖select语句,所有就是select语句操作的结果形参的表
3)视图支持对数据的增删查改 ?
4)视图不允许对视图表的字段做修改
5)视图不仅支持创建,也支持更新与删除
"""
# 语法
# 创建视图
mysql>: create view 视图名[(别名们)] as select 语句;
eg>: create view v1 as select dep, max(salary) from emp group by dep;

# 创建或替换视图
mysql>: create or replace 视图名[(别名们)] as select 语句;
mysql>: alter 视图名[(别名们)] as select 语句;
eg>: create or replace view v1(dep_name, max_salary) as select dep, max(salary) from emp group by dep;
eg>: alter view v1(name, salary) as select dep, max(salary) from emp group by dep;

# 删除视图
mysql>: drop view 视图名
eg>: drop view v1;

# 视图可以作为正常表完成连表查询
select name, dep_name, salary 
from emp join v1 
on emp.dep=v1.dep_name and emp.salary=v1.max_salary;

视图的增删改

# 前提:视图的增删改操作可以直接映射给真实表(本质就是对真实表进行操作)

# 视图可以完成增删改,增删改本质是直接对创建视图的真实表进行操作
create or replace view v2 as select id,name,age,salary from emp;
update v2 set salary=salary+1 where id=1;
delete from v2 where id=1;

create or replace view v3 as select * from emp;
insert into v3 values(1, 'yangsir', '男', 66, 1.11, '上海', '那噶的', '教职部');

# 总结:操作视图,会影响真实表,反之也会影响
update emp set salary=salary+1 where id=1;

你可能感兴趣的:(视图)