📄 view.result
字号:
drop 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;use test;create view v1 (c,d) as select a,b from t1;ERROR 42S02: Table 'test.t1' doesn't existcreate temporary table t1 (a int, b int);create view v1 (c) as select b+1 from t1;ERROR HY000: View's SELECT refers to a temporary table '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);create view v1 (c,d) as select a,b+@@global.max_user_connections from t1;ERROR HY000: View's SELECT contains a variable or parametercreate view v1 (c) as select b+1 from t1;select c from v1;c345611create temporary table t1 (a int, b int);select * from t1;a bselect c from v1;c345611show create table v1;View Create Viewv1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select (`t1`.`b` + 1) AS `c` from `t1`show create view v1;View Create Viewv1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select (`t1`.`b` + 1) AS `c` from `t1`show create view t1;ERROR HY000: 'test.t1' is not VIEWdrop table t1;select a from v1;ERROR 42S22: Unknown column 'a' in 'field list'select v1.a from v1;ERROR 42S22: Unknown column 'v1.a' in 'field list'select b from v1;ERROR 42S22: Unknown column 'b' in 'field list'select v1.b from v1;ERROR 42S22: Unknown column 'v1.b' in 'field list'explain extended select c from v1;id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t1 ALL NULL NULL NULL NULL 5 Warnings:Note 1003 select (`test`.`t1`.`b` + 1) AS `c` from `test`.`t1`create algorithm=temptable view v2 (c) as select b+1 from t1;show create view v2;View Create Viewv2 CREATE ALGORITHM=TEMPTABLE DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v2` AS select (`t1`.`b` + 1) AS `c` from `t1`select c from v2;c345611explain extended select c from v2;id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY <derived2> ALL NULL NULL NULL NULL 5 2 DERIVED t1 ALL NULL NULL NULL NULL 5 Warnings:Note 1003 select `v2`.`c` AS `c` from `test`.`v2`create view v3 (c) as select a+1 from v1;ERROR 42S22: Unknown column 'a' in 'field list'create view v3 (c) as select b+1 from v1;ERROR 42S22: Unknown column 'b' in 'field list'create view v3 (c) as select c+1 from v1;select c from v3;c456712explain extended select c from v3;id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t1 ALL NULL NULL NULL NULL 5 Warnings:Note 1003 select ((`test`.`t1`.`b` + 1) + 1) AS `c` from `test`.`t1`create algorithm=temptable view v4 (c) as select c+1 from v2;select c from v4;c456712explain extended select c from v4;id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY <derived2> ALL NULL NULL NULL NULL 5 2 DERIVED <derived3> ALL NULL NULL NULL NULL 5 3 DERIVED t1 ALL NULL NULL NULL NULL 5 Warnings:Note 1003 select `v4`.`c` AS `c` from `test`.`v4`create view v5 (c) as select c+1 from v2;select c from v5;c456712explain extended select c from v5;id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY <derived3> ALL NULL NULL NULL NULL 5 3 DERIVED t1 ALL NULL NULL NULL NULL 5 Warnings:Note 1003 select (`v2`.`c` + 1) AS `c` from `test`.`v2`create algorithm=temptable view v6 (c) as select c+1 from v1;select c from v6;c456712explain extended select c from v6;id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY <derived2> ALL NULL NULL NULL NULL 5 2 DERIVED t1 ALL NULL NULL NULL NULL 5 Warnings:Note 1003 select `v6`.`c` AS `c` from `test`.`v6`show tables;Tables_in_testt1v1v2v3v4v5v6show full tables;Tables_in_test Table_typet1 BASE TABLEv1 VIEWv2 VIEWv3 VIEWv4 VIEWv5 VIEWv6 VIEWshow table status;Name Engine Version Row_format Rows Avg_row_length Data_length Max_data_length Index_length Data_free Auto_increment Create_time Update_time Check_time Collation Checksum Create_options Commentt1 MyISAM 10 Fixed 5 9 45 # 1024 0 NULL # # NULL latin1_swedish_ci NULL v1 NULL NULL NULL NULL NULL NULL # NULL NULL NULL # # NULL NULL NULL NULL VIEWv2 NULL NULL NULL NULL NULL NULL # NULL NULL NULL # # NULL NULL NULL NULL VIEWv3 NULL NULL NULL NULL NULL NULL # NULL NULL NULL # # NULL NULL NULL NULL VIEWv4 NULL NULL NULL NULL NULL NULL # NULL NULL NULL # # NULL NULL NULL NULL VIEWv5 NULL NULL NULL NULL NULL NULL # NULL NULL NULL # # NULL NULL NULL NULL VIEWv6 NULL NULL NULL NULL NULL NULL # NULL NULL NULL # # NULL NULL NULL NULL VIEWdrop view v1,v2,v3,v4,v5,v6;create 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;c d e f1 2 0 01 3 0 02 4 0 02 5 0 03 10 1 0select * from v2;c d1 21 32 42 53 10create 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;ERROR 42S01: Table 'v1' already existscreate 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;drop view v2;alter view v2 as select c, d from v1;ERROR 42S02: Table 'test.v2' doesn't existcreate or replace view v2 as select c, d from v1;alter view v1 (c,d) as select a,max(b) from t1 group by a;select * from v1;c d1 32 53 10select * from v2;c d1 32 53 10drop view v100;ERROR 42S02: Unknown table 'test.v100'drop view t1;ERROR HY000: 'test.t1' is not VIEWdrop table v1;ERROR 42S02: Unknown table 'v1'drop view v1,v2;drop table t1;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;a123select * from v2 natural left join t1;a012select * from v2 natural left join v1;a012drop view v1, v2;drop table t1;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;a123explain select * from v1;id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY <derived2> ALL NULL NULL NULL NULL 3 2 DERIVED t1 ALL NULL NULL NULL NULL 6 Using temporaryselect * from t1;a123123drop view v1;drop table t1;create table t1 (a int);create view v1 as select distinct a from t1 WITH CHECK OPTION;ERROR HY000: CHECK OPTION on non-updatable view 'test.v1'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;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;c345611create algorithm=temptable view v2 (c) as select b+1 from t1;select test.c from v2 test;c345611select test1.* from v1 test1, v2 test2 where test1.c=test2.c;c345611select test2.* from v1 test1, v2 test2 where test1.c=test2.c;c345611drop table t1;drop view v1,v2;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;a+154explain select * from v1;id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY <derived2> ALL NULL NULL NULL NULL 2 2 DERIVED t1 ALL NULL NULL NULL NULL 4 Using filesortdrop view v1;drop table t1;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;Field Type Null Key Default Extraa+1 bigint(12) YES NULL select * from t2;a+12345drop view v1;drop table t1,t2;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;update v1 set c=a+c;ERROR HY000: Column 'c' is not updatableupdate v2 set a=a+c;ERROR HY000: The target table v2 of the UPDATE is not updatableupdate v1 set a=a+c;select * from v1;a c13 324 435 546 661 11select * from t1;a b13 224 335 446 561 10drop table t1;drop view v1,v2;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;update t2,v1 set v1.c=v1.a+v1.c where t2.x=v1.a;ERROR HY000: Column 'c' is not updatableupdate t2,v2 set v2.a=v2.v2.a+c where t2.x=v2.a;ERROR HY000: The target table v2 of the UPDATE is not updatableupdate t2,v1 set v1.a=v1.a+v1.c where t2.x=v1.a;select * from v1;a c13 324 430 540 650 11select * from t1;a b13 224 330 440 550 10drop table t1,t2;drop view v1,v2;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;select * from v1;c2030explain extended select * from v1;id select_type table type possible_keys key key_len ref rows Extra1 PRIMARY t1 ALL NULL NULL NULL NULL 5 Using whereWarnings:Note 1003 select `test`.`t1`.`b` AS `c` from `test`.`t1` where (`test`.`t1`.`a` < 3)update v1 set c=c+1;select * from t1;a b1 212 313 404 505 100create view v2 (c) as select b from t1 where a>=3;select * from v1, v2;c c21 4031 4021 5031 5021 10031 100drop view v1, v2;drop table t1;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;delete from v2 where c < 4;ERROR HY000: The target table v2 of the DELETE is not updatabledelete from v1 where c < 4;select * from v1;a c2 43 54 65 11select * from t1;a b2 33 44 55 10drop table t1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -