📄 view.test
字号:
--disable_warningsdrop table if exists t1,t2,t3,t4,t9,`t1a``b`,v1,v2,v3,v4,v5,v6;drop view if exists t1,t2,`t1a``b`,v1,v2,v3,v4,v5,v6;drop database if exists mysqltest;--enable_warningsuse test;## some basic test of views and its functionality## create view on nonexistent table-- error 1146create view v1 (c,d) as select a,b from t1;create temporary table t1 (a int, b int);# view on temporary table-- error 1352create view v1 (c) as select b+1 from t1;drop table t1;create table t1 (a int, b int);insert into t1 values (1,2), (1,3), (2,4), (2,5), (3,10);# view with variable-- error 1351create view v1 (c,d) as select a,b+@@global.max_user_connections from t1;# simple viewcreate view v1 (c) as select b+1 from t1;select c from v1;# temporary table should not hide table of viewcreate temporary table t1 (a int, b int);# this is emptyselect * from t1;# but this based on normal t1select c from v1;show create table v1;show create view v1;-- error 1347show create view t1;drop table t1;# try to use fields from underlying table-- error 1054select a from v1;-- error 1054select v1.a from v1;-- error 1054select b from v1;-- error 1054select v1.b from v1;# view with different algorithms (explain output differs)explain extended select c from v1;create algorithm=temptable view v2 (c) as select b+1 from t1;show create view v2;select c from v2;explain extended select c from v2;# try to use underlying table fields in VIEW creation process-- error 1054create view v3 (c) as select a+1 from v1;-- error 1054create view v3 (c) as select b+1 from v1;# VIEW on VIEW test with mixing different algorithms on different ordercreate view v3 (c) as select c+1 from v1;select c from v3;explain extended select c from v3;create algorithm=temptable view v4 (c) as select c+1 from v2;select c from v4;explain extended select c from v4;create view v5 (c) as select c+1 from v2;select c from v5;explain extended select c from v5;create algorithm=temptable view v6 (c) as select c+1 from v1;select c from v6;explain extended select c from v6;# show table/table status testshow tables;show full tables;--replace_column 8 # 12 # 13 #show table status;drop view v1,v2,v3,v4,v5,v6;## alter/create view test## view with subqueries of different typescreate view v1 (c,d,e,f) as select a,b,a in (select a+2 from t1), a = all (select a from t1) from t1;create view v2 as select c, d from v1;select * from v1;select * from v2;# try to create VIEW with name of existing VIEW-- error 1050create view v1 (c,d,e,f) as select a,b, a in (select a+2 from t1), a = all (select a from t1) from t1;# 'or replace' should work in this casecreate or replace view v1 (c,d,e,f) as select a,b, a in (select a+2 from t1), a = all (select a from t1) from t1;# try to ALTER unexisting VIEWdrop view v2;-- error 1146alter view v2 as select c, d from v1;# 'or replace' on unexisting viewcreate or replace view v2 as select c, d from v1;# alter view on existing viewalter view v1 (c,d) as select a,max(b) from t1 group by a;# check that created view worksselect * from v1;select * from v2;# try to drop nonexistent VIEW-- error 1051drop view v100;# try to drop table with DROP VIEW-- error 1347drop view t1;# try to drop VIEW with DROP TABLE-- error 1051drop table v1;# try to drop table with DROP VIEWdrop view v1,v2;drop table t1;## outer left join with merged views#create table t1 (a int);insert into t1 values (1), (2), (3);create view v1 (a) as select a+1 from t1;create view v2 (a) as select a-1 from t1;select * from t1 natural left join v1;select * from v2 natural left join t1;select * from v2 natural left join v1;drop view v1, v2;drop table t1;## DISTINCT option for VIEW#create table t1 (a int);insert into t1 values (1), (2), (3), (1), (2), (3);create view v1 as select distinct a from t1;select * from v1;explain select * from v1;select * from t1;drop view v1;drop table t1;## syntax compatibility#create table t1 (a int);-- error 1368create view v1 as select distinct a from t1 WITH CHECK OPTION;create view v1 as select a from t1 WITH CHECK OPTION;create view v2 as select a from t1 WITH CASCADED CHECK OPTION;create view v3 as select a from t1 WITH LOCAL CHECK OPTION;drop view v3 RESTRICT;drop view v2 CASCADE;drop view v1;drop table t1;## aliases#create table t1 (a int, b int);insert into t1 values (1,2), (1,3), (2,4), (2,5), (3,10);create view v1 (c) as select b+1 from t1;select test.c from v1 test;create algorithm=temptable view v2 (c) as select b+1 from t1;select test.c from v2 test;select test1.* from v1 test1, v2 test2 where test1.c=test2.c;select test2.* from v1 test1, v2 test2 where test1.c=test2.c;drop table t1;drop view v1,v2;## LIMIT clause test#create table t1 (a int);insert into t1 values (1), (2), (3), (4);create view v1 as select a+1 from t1 order by 1 desc limit 2;select * from v1;explain select * from v1;drop view v1;drop table t1;## CREATE ... SELECT view test#create table t1 (a int);insert into t1 values (1), (2), (3), (4);create view v1 as select a+1 from t1;create table t2 select * from v1;show columns from t2;select * from t2;drop view v1;drop table t1,t2;## simple view + simple update#create table t1 (a int, b int, primary key(a));insert into t1 values (10,2), (20,3), (30,4), (40,5), (50,10);create view v1 (a,c) as select a, b+1 from t1;create algorithm=temptable view v2 (a,c) as select a, b+1 from t1;# try to update expression-- error 1348update v1 set c=a+c;# try to update VIEW with forced TEMPORARY TABLE algorithm-- error 1288update v2 set a=a+c;# updatable field of updateable viewupdate v1 set a=a+c;select * from v1;select * from t1;drop table t1;drop view v1,v2;## simple view + simple multi-update#create table t1 (a int, b int, primary key(a));insert into t1 values (10,2), (20,3), (30,4), (40,5), (50,10);create table t2 (x int);insert into t2 values (10), (20);create view v1 (a,c) as select a, b+1 from t1;create algorithm=temptable view v2 (a,c) as select a, b+1 from t1;# try to update expression-- error 1348update t2,v1 set v1.c=v1.a+v1.c where t2.x=v1.a;# try to update VIEW with forced TEMPORARY TABLE algorithm-- error 1288update t2,v2 set v2.a=v2.v2.a+c where t2.x=v2.a;# updatable field of updateable viewupdate t2,v1 set v1.a=v1.a+v1.c where t2.x=v1.a;select * from v1;select * from t1;drop table t1,t2;drop view v1,v2;## MERGE VIEW with WHERE clause#create table t1 (a int, b int, primary key(b));insert into t1 values (1,20), (2,30), (3,40), (4,50), (5,100);create view v1 (c) as select b from t1 where a<3;# simple select and explaint to be sure that it is MERGEselect * from v1;explain extended select * from v1;# update testupdate v1 set c=c+1;select * from t1;# join of such VIEWs testcreate view v2 (c) as select b from t1 where a>=3;select * from v1, v2;drop view v1, v2;drop table t1;## simple view + simple delete#create table t1 (a int, b int, primary key(a));insert into t1 values (1,2), (2,3), (3,4), (4,5), (5,10);create view v1 (a,c) as select a, b+1 from t1;create algorithm=temptable view v2 (a,c) as select a, b+1 from t1;# try to update VIEW with forced TEMPORARY TABLE algorithm-- error 1288delete from v2 where c < 4;# updatable field of updateable viewdelete from v1 where c < 4;select * from v1;select * from t1;drop table t1;drop view v1,v2;## simple view + simple multi-delete#create table t1 (a int, b int, primary key(a));insert into t1 values (1,2), (2,3), (3,4), (4,5), (5,10);create table t2 (x int);insert into t2 values (1), (2), (3), (4);create view v1 (a,c) as select a, b+1 from t1;create algorithm=temptable view v2 (a,c) as select a, b+1 from t1;# try to update VIEW with forced TEMPORARY TABLE algorithm-- error 1288delete v2 from t2,v2 where t2.x=v2.a;# updatable field of updateable viewdelete v1 from t2,v1 where t2.x=v1.a;select * from v1;select * from t1;drop table t1,t2;drop view v1,v2;## key presence check#create table t1 (a int, b int, c int, primary key(a,b));insert into t1 values (10,2,-1), (20,3,-2), (30,4,-3), (40,5,-4), (50,10,-5);create view v1 (x,y) as select a, b from t1;create view v2 (x,y) as select a, c from t1;set updatable_views_with_limit=NO;update v1 set x=x+1;update v2 set x=x+1;update v1 set x=x+1 limit 1;-- error 1288update v2 set x=x+1 limit 1;set updatable_views_with_limit=YES;update v1 set x=x+1 limit 1;update v2 set x=x+1 limit 1;set updatable_views_with_limit=DEFAULT;show variables like "updatable_views_with_limit";select * from t1;drop table t1;drop view v1,v2;## simple insert#create table t1 (a int, b int, c int, primary key(a,b));insert into t1 values (10,2,-1), (20,3,-2);create view v1 (x,y,z) as select c, b, a from t1;create view v2 (x,y) as select b, a from t1;create view v3 (x,y,z) as select b, a, b from t1;create view v4 (x,y,z) as select c+1, b, a from t1;create algorithm=temptable view v5 (x,y,z) as select c, b, a from t1;# try insert to VIEW with fields duplicate-- error 1288insert into v3 values (-60,4,30);# try insert to VIEW with expression in SELECT list-- error 1288insert into v4 values (-60,4,30);# try insert to VIEW using temporary table algorithm-- error 1288insert into v5 values (-60,4,30);insert into v1 values (-60,4,30);insert into v1 (z,y,x) values (50,6,-100);insert into v2 values (5,40);select * from t1;drop table t1;drop view v1,v2,v3,v4,v5;## insert ... select#create table t1 (a int, b int, c int, primary key(a,b));insert into t1 values (10,2,-1), (20,3,-2);create table t2 (a int, b int, c int, primary key(a,b));insert into t2 values (30,4,-60);create view v1 (x,y,z) as select c, b, a from t1;create view v2 (x,y) as select b, a from t1;create view v3 (x,y,z) as select b, a, b from t1;create view v4 (x,y,z) as select c+1, b, a from t1;create algorithm=temptable view v5 (x,y,z) as select c, b, a from t1;# try insert to VIEW with fields duplicate-- error 1288insert into v3 select c, b, a from t2;# try insert to VIEW with expression in SELECT list-- error 1288
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -