📄 view.test
字号:
insert into v4 select c, b, a from t2;# try insert to VIEW using temporary table algorithm-- error 1288insert into v5 select c, b, a from t2;insert 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;drop table t1, t2;drop view v1,v2,v3,v4,v5;## outer join based on VIEW with WHERE clause#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);drop table t1;drop view v1;## merging WHERE condition on VIEW on VIEW#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;drop table t1;drop view v1,v2;## VIEW on non-updatable view#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;-- error 1288update v2 set y=10 where y=2;drop table t1;drop view v1,v2;## auto_increment field out of VIEW#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();insert into t1 (b) values (2);select last_insert_id();select * from t1;drop view v1;drop table t1;## VIEW fields quoting#set sql_mode='ansi';create table t1 ("a*b" int);create view v1 as select "a*b" from t1;show create view v1;drop view v1;drop table t1;set sql_mode=default;## VIEW without tables#create table t1 (t_column int);create view v1 as select 'a';select * from v1, t1;drop view v1;drop table t1;## quote mark inside table name#create table `t1a``b` (col1 char(2));create view v1 as select * from `t1a``b`;select * from v1;describe v1;drop view v1;drop table `t1a``b`;## Changing of underlying table#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));-- error 1356insert into v1 values('a','aa');drop table t1;-- error 1356select * from v1;drop view v1;## check of duplication of column names#-- error 1060create view v1 (a,a) as select 'a','a';## SP variables inside view test#--disable_warningsdrop procedure if exists p1;--enable_warningsdelimiter //;create procedure p1 () begin declare v int; create view v1 as select v; end;//delimiter ;//-- error 1351call p1();drop procedure p1;## updatablity should be transitive#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;drop view v2, v1;drop table t1;## check 'use index' on view with temporary table#create table t1 (a int, b int);create view v1 as select a, sum(b) from t1 group by a;-- error 1072select b from v1 use index (some_index) where b=1;drop view v1;drop table t1;## using VIEW fields several times in query resolved via temporary tables#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);select distinct first.col2 from v1 first where first.col2 in (select second.col2 from t1 second where second.col1<>first.col1);drop view v1;drop table t1;## Test of view updatability in prepared statement#create table t1 (a int);create view v1 as select a from t1;insert into t1 values (1);#updateSET @v0 = '2';PREPARE stmt FROM 'UPDATE v1 SET a = ?';EXECUTE stmt USING @v0;DEALLOCATE PREPARE stmt;#insert without field listSET @v0 = '3';PREPARE stmt FROM 'insert into v1 values (?)';EXECUTE stmt USING @v0;DEALLOCATE PREPARE stmt;#insert with field listSET @v0 = '4';PREPARE stmt FROM 'insert into v1 (a) values (?)';EXECUTE stmt USING @v0;DEALLOCATE PREPARE stmt;select * from t1;drop view v1;drop table t1;## error on preparation#-- error 1096CREATE VIEW v02 AS SELECT * FROM DUAL;SHOW TABLES;## EXISTS with UNION VIEW#CREATE VIEW v1 AS SELECT EXISTS (SELECT 1 UNION SELECT 2);select * from v1;drop view v1;## using VIEW where table is required#create table t1 (col1 int,col2 char(22));create view v1 as select * from t1;-- error 1347create index i1 on v1 (col1);drop view v1;drop table t1;## connection_id(), pi(), current_user(), version() representation test#CREATE VIEW v1 (f1,f2,f3,f4) AS SELECT connection_id(), pi(), current_user(), version();SHOW CREATE VIEW v1;drop view v1;## VIEW built over UNION#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;drop view v1;drop tables t1, t2;## Aggregate functions in view list#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;drop view v1;drop table t1;## Showing VIEW with VIEWs in subquery#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;drop view v2, v1;drop table t1, t2;## SHOW VIEW view with name with spaces#CREATE VIEW `v 1` AS select 5 AS `5`;show create view `v 1`;drop view `v 1`;## Removing database with .frm archives#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;## VIEW with full text#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');CREATE VIEW v1 AS SELECT * from t1 WHERE match (c2) against ('Beer');select * from v1;drop view v1;drop table t1;## distinct in temporary table with a VIEW#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;select distinct a from v1 limit 2;select distinct a from t1 limit 2;prepare stmt1 from "select distinct a from v1 limit 2";execute stmt1;execute stmt1;deallocate prepare stmt1;drop view v1;drop table t1;## aggregate function of aggregate function#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;drop view v1;drop table t1;## VIEW of VIEW with column renaming#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;show create view v3;drop view v3, v2, v1;drop table t2, t1;## VIEW based on functions with complex names#create function `f``1` () returns int return 5;create view v1 as select test.`f``1` ();show create view v1;select * from v1;drop view v1;drop function `f``1`;## tested problem when function name length close to ALIGN_SIZE#create function x () returns int return 5;create view v1 as select x ();select * from v1;drop view v1;drop function x;## VIEW with collation#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;show create view v2;drop view v2;drop table t2;## order by refers on integer field#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;drop view v1;drop table t1;## VIEW over dropped function#create function x1 () returns int return 5;create table t1 (s1 int);create view v1 as select x1() from t1;drop function x1;-- error 1356select * from v1;--replace_column 8 # 12 # 13 #show table status;drop view v1;drop table t1;## VIEW with floating point (long number) as column#create view v1 as select 99999999999999999999999999999999999999999999999999999 as col1;show create view v1;drop view v1;## VIEWs with national characters#create table t
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -