📄 view.result
字号:
drop view v1,v2;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;delete v2 from t2,v2 where t2.x=v2.a;ERROR HY000: The target table v2 of the DELETE is not updatabledelete v1 from t2,v1 where t2.x=v1.a;select * from v1;a c5 11select * from t1;a b5 10drop table t1,t2;drop view v1,v2;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;update v2 set x=x+1 limit 1;ERROR HY000: The target table v2 of the UPDATE is not updatableset updatable_views_with_limit=YES;update v1 set x=x+1 limit 1;update v2 set x=x+1 limit 1;Warnings:Note 1355 View being updated does not have complete key of underlying table in itset updatable_views_with_limit=DEFAULT;show variables like "updatable_views_with_limit";Variable_name Valueupdatable_views_with_limit YESselect * from t1;a b c15 2 -122 3 -232 4 -342 5 -452 10 -5drop table t1;drop view v1,v2;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;insert into v3 values (-60,4,30);ERROR HY000: The target table v3 of the INSERT is not updatableinsert into v4 values (-60,4,30);ERROR HY000: The target table v4 of the INSERT is not updatableinsert into v5 values (-60,4,30);ERROR HY000: The target table v5 of the INSERT is not updatableinsert 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;a b c10 2 -120 3 -230 4 -6050 6 -10040 5 NULLdrop table t1;drop view v1,v2,v3,v4,v5;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;insert into v3 select c, b, a from t2;ERROR HY000: The target table v3 of the INSERT is not updatableinsert into v4 select c, b, a from t2;ERROR HY000: The target table v4 of the INSERT is not updatableinsert into v5 select c, b, a from t2;ERROR HY000: The target table v5 of the INSERT is not updatableinsert into v1 select c, b, a from t2;insert into v1 (z,y,x) select a+20,b+2,-100 from t2;insert into v2 select b+1, a+10 from t2;select * from t1;a b c10 2 -120 3 -230 4 -6050 6 -10040 5 NULLdrop table t1, t2;drop view v1,v2,v3,v4,v5;create table t1 (a int, primary key(a));insert into t1 values (1), (2), (3);create view v1 (x) as select a from t1 where a > 1;select t1.a, v1.x from t1 left join v1 on (t1.a= v1.x);a x1 NULL2 23 3drop table t1;drop view v1;create table t1 (a int, primary key(a));insert into t1 values (1), (2), (3), (200);create view v1 (x) as select a from t1 where a > 1;create view v2 (y) as select x from v1 where x < 100;select * from v2;y23drop table t1;drop view v1,v2;create table t1 (a int, primary key(a));insert into t1 values (1), (2), (3), (200);create ALGORITHM=TEMPTABLE view v1 (x) as select a from t1;create view v2 (y) as select x from v1;update v2 set y=10 where y=2;ERROR HY000: The target table v2 of the UPDATE is not updatabledrop table t1;drop view v1,v2;create table t1 (a int not null auto_increment, b int not null, primary key(a), unique(b));create view v1 (x) as select b from t1;insert into v1 values (1);select last_insert_id();last_insert_id()0insert into t1 (b) values (2);select last_insert_id();last_insert_id()2select * from t1;a b1 12 2drop view v1;drop table t1;set sql_mode='ansi';create table t1 ("a*b" int);create view v1 as select "a*b" from t1;show create view v1;View Create Viewv1 CREATE VIEW "v1" AS select "t1"."a*b" AS "a*b" from "t1"drop view v1;drop table t1;set sql_mode=default;create table t1 (t_column int);create view v1 as select 'a';select * from v1, t1;a t_columndrop view v1;drop table t1;create table `t1a``b` (col1 char(2));create view v1 as select * from `t1a``b`;select * from v1;col1describe v1;Field Type Null Key Default Extracol1 char(2) YES NULL drop view v1;drop table `t1a``b`;create table t1 (col1 char(5),col2 char(5));create view v1 as select * from t1;drop table t1;create table t1 (col1 char(5),newcol2 char(5));insert into v1 values('a','aa');ERROR HY000: View 'test.v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use themdrop table t1;select * from v1;ERROR HY000: View 'test.v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use themdrop view v1;create view v1 (a,a) as select 'a','a';ERROR 42S21: Duplicate column name 'a'drop procedure if exists p1;create procedure p1 () begin declare v int; create view v1 as select v; end;//call p1();ERROR HY000: View's SELECT contains a variable or parameterdrop procedure p1;create table t1 (col1 int,col2 char(22));insert into t1 values(5,'Hello, world of views');create view v1 as select * from t1;create view v2 as select * from v1;update v2 set col2='Hello, view world';select * from t1;col1 col25 Hello, view worlddrop view v2, v1;drop table t1;create table t1 (a int, b int);create view v1 as select a, sum(b) from t1 group by a;select b from v1 use index (some_index) where b=1;ERROR 42000: Key column 'some_index' doesn't exist in tabledrop view v1;drop table t1;create table t1 (col1 char(5),col2 char(5));create view v1 (col1,col2) as select col1,col2 from t1;insert into v1 values('s1','p1'),('s1','p2'),('s1','p3'),('s1','p4'),('s2','p1'),('s3','p2'),('s4','p4');select distinct first.col2 from t1 first where first.col2 in (select second.col2 from t1 second where second.col1<>first.col1);col2p1p2p4select distinct first.col2 from v1 first where first.col2 in (select second.col2 from t1 second where second.col1<>first.col1);col2p1p2p4drop view v1;drop table t1;create table t1 (a int);create view v1 as select a from t1;insert into t1 values (1);SET @v0 = '2';PREPARE stmt FROM 'UPDATE v1 SET a = ?';EXECUTE stmt USING @v0;DEALLOCATE PREPARE stmt;SET @v0 = '3';PREPARE stmt FROM 'insert into v1 values (?)';EXECUTE stmt USING @v0;DEALLOCATE PREPARE stmt;SET @v0 = '4';PREPARE stmt FROM 'insert into v1 (a) values (?)';EXECUTE stmt USING @v0;DEALLOCATE PREPARE stmt;select * from t1;a234drop view v1;drop table t1;CREATE VIEW v02 AS SELECT * FROM DUAL;ERROR HY000: No tables usedSHOW TABLES;Tables_in_testCREATE VIEW v1 AS SELECT EXISTS (SELECT 1 UNION SELECT 2);select * from v1;EXISTS (SELECT 1 UNION SELECT 2)1drop view v1;create table t1 (col1 int,col2 char(22));create view v1 as select * from t1;create index i1 on v1 (col1);ERROR HY000: 'test.v1' is not BASE TABLEdrop view v1;drop table t1;CREATE VIEW v1 (f1,f2,f3,f4) AS SELECT connection_id(), pi(), current_user(), version();SHOW CREATE VIEW v1;View Create Viewv1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select sql_no_cache connection_id() AS `f1`,pi() AS `f2`,current_user() AS `f3`,version() AS `f4`drop view v1;create table t1 (s1 int);create table t2 (s2 int);insert into t1 values (1), (2);insert into t2 values (2), (3);create view v1 as select * from t1,t2 union all select * from t1,t2;select * from v1;s1 s21 22 21 32 31 22 21 32 3drop view v1;drop tables t1, t2;create table t1 (col1 int);insert into t1 values (1);create view v1 as select count(*) from t1;insert into t1 values (null);select * from v1;count(*)2drop view v1;drop table t1;create table t1 (a int);create table t2 (a int);create view v1 as select a from t1;create view v2 as select a from t2 where a in (select a from v1);show create view v2;View Create Viewv2 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v2` AS select `t2`.`a` AS `a` from `t2` where `a` in (select `v1`.`a` AS `a` from `v1`)drop view v2, v1;drop table t1, t2;CREATE VIEW `v 1` AS select 5 AS `5`;show create view `v 1`;View Create Viewv 1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v 1` AS select 5 AS `5`drop view `v 1`;create database mysqltest;create table mysqltest.t1 (a int, b int);create view mysqltest.v1 as select a from mysqltest.t1;alter view mysqltest.v1 as select b from mysqltest.t1;alter view mysqltest.v1 as select a from mysqltest.t1;drop database mysqltest;CREATE TABLE t1 (c1 int not null auto_increment primary key, c2 varchar(20), fulltext(c2));insert into t1 (c2) VALUES ('real Beer'),('Water'),('Kossu'),('Coca-Cola'),('Vodka'),('Wine'),('almost real Beer');select * from t1 WHERE match (c2) against ('Beer');c1 c21 real Beer7 almost real BeerCREATE VIEW v1 AS SELECT * from t1 WHERE match (c2) against ('Beer');select * from v1;c1 c21 real Beer7 almost real Beerdrop view v1;drop table t1;create table t1 (a int);insert into t1 values (1),(1),(2),(2),(3),(3);create view v1 as select a from t1;select distinct a from v1;a123select distinct a from v1 limit 2;a12select distinct a from t1 limit 2;a12prepare stmt1 from "select distinct a from v1 limit 2";execute stmt1;a12execute stmt1;a12deallocate prepare stmt1;drop view v1;drop table t1;create table t1 (tg_column bigint);create view v1 as select count(tg_column) as vg_column from t1;select avg(vg_column) from v1;avg(vg_column)0.0000drop view v1;drop table t1;create table t1 (col1 bigint not null, primary key (col1));create table t2 (col1 bigint not null, key (col1));create view v1 as select * from t1;create view v2 as select * from t2;insert into v1 values (1);insert into v2 values (1);create view v3 (a,b) as select v1.col1 as a, v2.col1 as b from v1, v2 where v1.col1 = v2.col1;select * from v3;a b1 1show create view v3;View Create Viewv3 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v3` AS select `v1`.`col1` AS `a`,`v2`.`col1` AS `b` from (`v1` join `v2`) where (`v1`.`col1` = `v2`.`col1`)drop view v3, v2, v1;drop table t2, t1;create function `f``1` () returns int return 5;create view v1 as select test.`f``1` ();show create view v1;View Create Viewv1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select sql_no_cache `test`.`f``1`() AS `test.``f````1`` ()`select * from v1;test.`f``1` ()5drop view v1;drop function `f``1`;create function x () returns int return 5;create view v1 as select x ();select * from v1;x ()5drop view v1;drop function x;create table t2 (col1 char collate latin1_german2_ci);create view v2 as select col1 collate latin1_german1_ci from t2;show create view v2;View Create Viewv2 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v2` AS select (`t2`.`col1` collate latin1_german1_ci) AS `col1 collate latin1_german1_ci` from `t2`show create view v2;View Create Viewv2 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v2` AS select (`t2`.`col1` collate latin1_german1_ci) AS `col1 collate latin1_german1_ci` from `t2`drop view v2;drop table t2;create table t1 (a int);insert into t1 values (1), (2);create view v1 as select 5 from t1 order by 1;select * from v1;555drop view v1;drop table t1;create function x1 () returns int return 5;create table t1 (s1 int);create view v1 as select x1() from t1;drop function x1;select * from v1;ERROR HY000: View 'test.v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use themshow 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 0 0 0 # 1024 0 NULL # # NULL latin1_swedish_ci NULL v1 NULL NULL NULL NULL NULL NULL # NULL NULL NULL # # NULL NULL NULL NULL View 'test.v1' references invalid table(s) or column(s) or function(s) or definedrop view v1;drop table t1;create view v1 as select 99999999999999999999999999999999999999999999999999999 as col1;show create view v1;View Create Viewv1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select 99999999999999999999999999999999999999999999999999999 AS `col1`drop view v1;create table t
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -