标签:state option algorithm sql视图 并且 replace ict 一个 column
1.视图概述:视图是从一个或多个表导出来的表,它是一种虚拟存在的表,并且表的结构和数据都依赖基本表2.创建视图的语法格式:视图创建基于SELECT语句
CREATE [OR REPLACE] [ ALGORITHM] = {UNDEFINDE | MERGE | TEMPTABLE} ]
VIEW view_name [(column_list)]
AS SELECT_statement
[WITH [ CASCADED | LOCAL] CHECK OPTION]//OR REPLACE表示该语句可以替换已有视图
//ALGORITHM 表示是视图选择的算法
//UNDEFINDE 表示MySQL自动选择算法
//MERGE:将使用视图的语句与视图定义合并起来,是的视图定义的某一部分取代语句的对应部分
//TEMPTABEL:视图存入临时表
//column_list:属性清单,指定了视图中各个属性的名
//AS : 视图要执行的操作
//CASCADED :级联,满足与该视图有关的的所有相关视图和表的条件
//LOCAL : 可选,满足该视图本身定义即可
2.在单表上创建视图
create view play_v as select id,name from star;
select * from play_v;
create view play_v1(number,player) as select id,name from star;
3.在多表上创建视图
create view p_t (number,player,team)
as
select star.id,star.name,team.name from star,team where star.team_id = team.id;select * from p_t;
4.查看视图
DESCRIBE 视图名称 // 或者 DESC 视图名称
desc play_v1;
SHOW TABLE STATUS LIKE ‘视图名称‘
show table status like ‘p_t‘;
SHOW CREATE VIEW 视图名称;
show create view play_v;
5.修改视图:当基本表中的某些字段发生变化
(1)使用CREATE OR REPLACE VIEW 语句修改视图
create or replace view play_v as select * from student;
(2)
ALTER [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
VIEW view_name [(column_list )]
AS SELECT_statement
[WITH [CASCADED | LOCAL] CHECK OPTION]
~使用alter修改视图结构
alter view play_v1 as select team_id,name from star;
6.更新视图
(1)UPDATE语句
update view p_t set number = null;
(2)INSERT 语句
insert into star values(11,‘克莱-汤普森‘,4); #视图也跟着改变
(3)DELETE语句
delete from p_t where number = 11; #视图删除的数据就是基本表里的数据
7.删除视图
DROP VIEW [IF EXISTS] view_name [,view_name......] [RESTRICT | CASCADE]
drop view play_v1;
标签:state option algorithm sql视图 并且 replace ict 一个 column
原文地址:http://blog.51cto.com/13742773/2328887