view.result 45 KB
Newer Older
1 2
drop table if exists t1,t2,`t1a``b`,v1,v2,v3,v4,v5,v6;
drop view if exists t1,t2,`t1a``b`,v1,v2,v3,v4,v5,v6;
unknown's avatar
VIEW  
unknown committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
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 exist
create temporary table t1 (a int, b int);
create view v1 (c) as select b+1 from t1;
ERROR HY000: View's SELECT contains 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 parameter
create view v1 (c) as select b+1 from t1;
select c from v1;
c
3
4
5
6
11
create temporary table t1 (a int, b int);
select * from t1;
a	b
select c from v1;
c
3
4
5
6
11
show create table v1;
unknown's avatar
unknown committed
34
View	Create View
35
v1	CREATE ALGORITHM=UNDEFINED VIEW `test`.`v1` AS select (`test`.`t1`.`b` + 1) AS `c` from `test`.`t1`
unknown's avatar
VIEW  
unknown committed
36
show create view v1;
unknown's avatar
unknown committed
37
View	Create View
38
v1	CREATE ALGORITHM=UNDEFINED VIEW `test`.`v1` AS select (`test`.`t1`.`b` + 1) AS `c` from `test`.`t1`
unknown's avatar
VIEW  
unknown committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
show create view t1;
ERROR HY000: 'test.t1' is not VIEW
drop 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	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	
Warnings:
54
Note	1003	select (`test`.`t1`.`b` + 1) AS `c` from `test`.`v1`
unknown's avatar
VIEW  
unknown committed
55
create algorithm=temptable view v2 (c) as select b+1 from t1;
unknown's avatar
unknown committed
56 57
show create view v2;
View	Create View
58
v2	CREATE ALGORITHM=TEMPTABLE VIEW `test`.`v2` AS select (`test`.`t1`.`b` + 1) AS `c` from `test`.`t1`
unknown's avatar
VIEW  
unknown committed
59 60 61 62 63 64 65 66 67 68
select c from v2;
c
3
4
5
6
11
explain extended select c from v2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	5	
69
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	5	
unknown's avatar
VIEW  
unknown committed
70
Warnings:
71
Note	1003	select `v2`.`c` AS `c` from `test`.`v2`
unknown's avatar
VIEW  
unknown committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
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;
c
4
5
6
7
12
explain extended select c from v3;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	
Warnings:
88
Note	1003	select ((`test`.`t1`.`b` + 1) + 1) AS `c` from `test`.`v3`
unknown's avatar
VIEW  
unknown committed
89 90 91 92 93 94 95 96 97 98 99
create algorithm=temptable view v4 (c) as select c+1 from v2;
select c from v4;
c
4
5
6
7
12
explain extended select c from v4;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	5	
100 101
2	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	5	
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	5	
unknown's avatar
VIEW  
unknown committed
102
Warnings:
103
Note	1003	select `v4`.`c` AS `c` from `test`.`v4`
unknown's avatar
VIEW  
unknown committed
104 105 106 107 108 109 110 111 112 113 114
create view v5 (c) as select c+1 from v2;
select c from v5;
c
4
5
6
7
12
explain extended select c from v5;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived3>	ALL	NULL	NULL	NULL	NULL	5	
115
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	5	
unknown's avatar
VIEW  
unknown committed
116
Warnings:
117
Note	1003	select (`v2`.`c` + 1) AS `c` from `test`.`v5`
unknown's avatar
VIEW  
unknown committed
118 119 120 121 122 123 124 125 126 127 128
create algorithm=temptable view v6 (c) as select c+1 from v1;
select c from v6;
c
4
5
6
7
12
explain extended select c from v6;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	5	
129
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	5	
unknown's avatar
VIEW  
unknown committed
130
Warnings:
131
Note	1003	select `v6`.`c` AS `c` from `test`.`v6`
unknown's avatar
VIEW  
unknown committed
132
show tables;
133 134 135 136 137 138 139 140
Tables_in_test	table_type
t1	BASE TABLE
v1	VIEW
v2	VIEW
v3	VIEW
v4	VIEW
v5	VIEW
v6	VIEW
unknown's avatar
VIEW  
unknown committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
show 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	Comment
t1	MyISAM	9	Fixed	5	9	45	38654705663	1024	0	NULL	#	#	NULL	latin1_swedish_ci	NULL		
v1	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	#	#	NULL	NULL	NULL	NULL	view
v2	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	#	#	NULL	NULL	NULL	NULL	view
v3	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	#	#	NULL	NULL	NULL	NULL	view
v4	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	#	#	NULL	NULL	NULL	NULL	view
v5	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	#	#	NULL	NULL	NULL	NULL	view
v6	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	#	#	NULL	NULL	NULL	NULL	view
drop 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	f
1	2	0	0
1	3	0	0
2	4	0	0
2	5	0	0
3	10	1	0
select * from v2;
c	d
1	2
1	3
2	4
2	5
3	10
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;
ERROR 42S01: Table 'v1' already exists
create 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 exist
create 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	d
1	3
2	5
3	10
select * from v2;
c	d
1	3
2	5
3	10
grant create view on test.* to test@localhost;
show grants for test@localhost;
Grants for test@localhost
GRANT USAGE ON *.* TO 'test'@'localhost'
GRANT CREATE VIEW ON `test`.* TO 'test'@'localhost'
revoke create view on test.* from test@localhost;
show grants for test@localhost;
Grants for test@localhost
GRANT USAGE ON *.* TO 'test'@'localhost'
drop view v100;
ERROR 42S02: Unknown table 'test.v100'
drop view t1;
ERROR HY000: 'test.t1' is not VIEW
drop 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;
a	a
1	NULL
2	2
3	3
select * from v2 natural left join t1;
a	a
0	NULL
1	1
2	2
select * from v2 natural left join v1;
a	a
0	NULL
1	NULL
2	2
drop view v1, v2;
drop table t1;
create database mysqltest;
create table mysqltest.t1 (a int, b int);
create table mysqltest.t2 (a int, b int);
grant select on mysqltest.t1 to mysqltest_1@localhost;
grant create view,select on test.* to mysqltest_1@localhost;
229 230 231 232 233
create view v1 as select * from mysqltest.t1;
alter view v1 as select * from mysqltest.t1;
ERROR 42000: delete command denied to user 'mysqltest_1'@'localhost' for table 'v1'
create or replace view v1 as select * from mysqltest.t1;
ERROR 42000: delete command denied to user 'mysqltest_1'@'localhost' for table 'v1'
unknown's avatar
VIEW  
unknown committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
create view mysqltest.v2  as select * from mysqltest.t1;
ERROR 42000: create view command denied to user 'mysqltest_1'@'localhost' for table 'v2'
create view v2 as select * from mysqltest.t2;
ERROR 42000: ANY command denied to user 'mysqltest_1'@'localhost' for table 't2'
revoke all privileges on mysqltest.t1 from mysqltest_1@localhost;
revoke all privileges on test.* from mysqltest_1@localhost;
drop database mysqltest;
drop view test.v1;
create database mysqltest;
create table mysqltest.t1 (a int, b int);
create view mysqltest.v1 (c,d) as select a+1,b+1 from mysqltest.t1;
grant select (c) on mysqltest.v1 to mysqltest_1@localhost;
select c from mysqltest.v1;
c
select d from mysqltest.v1;
ERROR 42000: SELECT command denied to user 'mysqltest_1'@'localhost' for column 'd' in table 'v1'
revoke all privileges on mysqltest.v1 from mysqltest_1@localhost;
delete from mysql.user where user='mysqltest_1';
drop database mysqltest;
create database mysqltest;
create table mysqltest.t1 (a int, b int);
create algorithm=temptable view mysqltest.v1 (c,d) as select a+1,b+1 from mysqltest.t1;
grant select (c) on mysqltest.v1 to mysqltest_1@localhost;
select c from mysqltest.v1;
c
select d from mysqltest.v1;
ERROR 42000: SELECT command denied to user 'mysqltest_1'@'localhost' for column 'd' in table 'v1'
revoke all privileges on mysqltest.v1 from mysqltest_1@localhost;
delete from mysql.user where user='mysqltest_1';
drop database mysqltest;
create database mysqltest;
create table mysqltest.t1 (a int, b int);
create table mysqltest.t2 (a int, b int);
create view mysqltest.v1 (c,d) as select a+1,b+1 from mysqltest.t1;
create algorithm=temptable view mysqltest.v2 (c,d) as select a+1,b+1 from mysqltest.t1;
create view mysqltest.v3 (c,d) as select a+1,b+1 from mysqltest.t2;
create algorithm=temptable view mysqltest.v4 (c,d) as select a+1,b+1 from mysqltest.t2;
grant select on mysqltest.v1 to mysqltest_1@localhost;
grant select on mysqltest.v2 to mysqltest_1@localhost;
grant select on mysqltest.v3 to mysqltest_1@localhost;
grant select on mysqltest.v4 to mysqltest_1@localhost;
select c from mysqltest.v1;
c
select c from mysqltest.v2;
c
select c from mysqltest.v3;
c
select c from mysqltest.v4;
c
show columns from mysqltest.v1;
Field	Type	Null	Key	Default	Extra
c	bigint(20)	YES		NULL	
d	bigint(20)	YES		NULL	
show columns from mysqltest.v2;
Field	Type	Null	Key	Default	Extra
c	bigint(20)	YES		NULL	
d	bigint(20)	YES		NULL	
explain select c from mysqltest.v1;
ERROR HY000: EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
unknown's avatar
unknown committed
293
show create view mysqltest.v1;
294
ERROR 42000: show create view command denied to user 'mysqltest_1'@'localhost' for table 'v1'
unknown's avatar
VIEW  
unknown committed
295 296
explain select c from mysqltest.v2;
ERROR HY000: EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
unknown's avatar
unknown committed
297
show create view mysqltest.v2;
298
ERROR 42000: show create view command denied to user 'mysqltest_1'@'localhost' for table 'v2'
unknown's avatar
VIEW  
unknown committed
299 300
explain select c from mysqltest.v3;
ERROR HY000: EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
unknown's avatar
unknown committed
301
show create view mysqltest.v3;
302
ERROR 42000: show create view command denied to user 'mysqltest_1'@'localhost' for table 'v3'
unknown's avatar
VIEW  
unknown committed
303 304
explain select c from mysqltest.v4;
ERROR HY000: EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
unknown's avatar
unknown committed
305
show create view mysqltest.v4;
306
ERROR 42000: show create view command denied to user 'mysqltest_1'@'localhost' for table 'v4'
unknown's avatar
VIEW  
unknown committed
307 308 309 310
grant select on mysqltest.t1 to mysqltest_1@localhost;
explain select c from mysqltest.v1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	const row not found
unknown's avatar
unknown committed
311
show create view mysqltest.v1;
312
ERROR 42000: show create view command denied to user 'mysqltest_1'@'localhost' for table 'v1'
unknown's avatar
VIEW  
unknown committed
313 314 315
explain select c from mysqltest.v2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	system	NULL	NULL	NULL	NULL	0	const row not found
316
2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
317
show create view mysqltest.v2;
318
ERROR 42000: show create view command denied to user 'mysqltest_1'@'localhost' for table 'v2'
unknown's avatar
VIEW  
unknown committed
319 320
explain select c from mysqltest.v3;
ERROR HY000: EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
unknown's avatar
unknown committed
321
show create view mysqltest.v3;
322
ERROR 42000: show create view command denied to user 'mysqltest_1'@'localhost' for table 'v3'
unknown's avatar
VIEW  
unknown committed
323 324
explain select c from mysqltest.v4;
ERROR HY000: EXPLAIN/SHOW can not be issued; lacking privileges for underlying table
unknown's avatar
unknown committed
325
show create view mysqltest.v4;
326
ERROR 42000: show create view command denied to user 'mysqltest_1'@'localhost' for table 'v4'
unknown's avatar
VIEW  
unknown committed
327 328 329 330
grant show view on mysqltest.* to mysqltest_1@localhost;
explain select c from mysqltest.v1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	system	NULL	NULL	NULL	NULL	0	const row not found
unknown's avatar
unknown committed
331 332
show create view mysqltest.v1;
View	Create View
333
v1	CREATE ALGORITHM=UNDEFINED VIEW `mysqltest`.`v1` AS select (`mysqltest`.`t1`.`a` + 1) AS `c`,(`mysqltest`.`t1`.`b` + 1) AS `d` from `mysqltest`.`t1`
unknown's avatar
VIEW  
unknown committed
334 335 336
explain select c from mysqltest.v2;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	system	NULL	NULL	NULL	NULL	0	const row not found
337
2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
338 339
show create view mysqltest.v2;
View	Create View
340
v2	CREATE ALGORITHM=TEMPTABLE VIEW `mysqltest`.`v2` AS select (`mysqltest`.`t1`.`a` + 1) AS `c`,(`mysqltest`.`t1`.`b` + 1) AS `d` from `mysqltest`.`t1`
unknown's avatar
VIEW  
unknown committed
341 342 343
explain select c from mysqltest.v3;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t2	system	NULL	NULL	NULL	NULL	0	const row not found
unknown's avatar
unknown committed
344 345
show create view mysqltest.v3;
View	Create View
346
v3	CREATE ALGORITHM=UNDEFINED VIEW `mysqltest`.`v3` AS select (`mysqltest`.`t2`.`a` + 1) AS `c`,(`mysqltest`.`t2`.`b` + 1) AS `d` from `mysqltest`.`t2`
unknown's avatar
VIEW  
unknown committed
347 348 349
explain select c from mysqltest.v4;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	system	NULL	NULL	NULL	NULL	0	const row not found
350
2	DERIVED	NULL	NULL	NULL	NULL	NULL	NULL	NULL	no matching row in const table
unknown's avatar
unknown committed
351 352
show create view mysqltest.v4;
View	Create View
353
v4	CREATE ALGORITHM=TEMPTABLE VIEW `mysqltest`.`v4` AS select (`mysqltest`.`t2`.`a` + 1) AS `c`,(`mysqltest`.`t2`.`b` + 1) AS `d` from `mysqltest`.`t2`
unknown's avatar
VIEW  
unknown committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
revoke all privileges on mysqltest.* from mysqltest_1@localhost;
delete from mysql.user where user='mysqltest_1';
drop database mysqltest;
set GLOBAL query_cache_size=1355776;
flush status;
create table t1 (a int, b int);
create view v1 (c,d) as select sql_no_cache a,b from t1;
create view v2 (c,d) as select a+rand(),b from t1;
show status like "Qcache_queries_in_cache";
Variable_name	Value
Qcache_queries_in_cache	0
show status like "Qcache_inserts";
Variable_name	Value
Qcache_inserts	0
show status like "Qcache_hits";
Variable_name	Value
Qcache_hits	0
select * from v1;
c	d
select * from v2;
c	d
show status like "Qcache_queries_in_cache";
Variable_name	Value
Qcache_queries_in_cache	0
show status like "Qcache_inserts";
Variable_name	Value
Qcache_inserts	0
show status like "Qcache_hits";
Variable_name	Value
Qcache_hits	0
select * from v1;
c	d
select * from v2;
c	d
show status like "Qcache_queries_in_cache";
Variable_name	Value
Qcache_queries_in_cache	0
show status like "Qcache_inserts";
Variable_name	Value
Qcache_inserts	0
show status like "Qcache_hits";
Variable_name	Value
Qcache_hits	0
drop view v1,v2;
set query_cache_type=demand;
flush status;
create view v1 (c,d) as select sql_cache a,b from t1;
show status like "Qcache_queries_in_cache";
Variable_name	Value
Qcache_queries_in_cache	0
show status like "Qcache_inserts";
Variable_name	Value
Qcache_inserts	0
show status like "Qcache_hits";
Variable_name	Value
Qcache_hits	0
select * from v1;
c	d
show status like "Qcache_queries_in_cache";
Variable_name	Value
Qcache_queries_in_cache	1
show status like "Qcache_inserts";
Variable_name	Value
Qcache_inserts	1
show status like "Qcache_hits";
Variable_name	Value
Qcache_hits	0
select * from t1;
a	b
show status like "Qcache_queries_in_cache";
Variable_name	Value
Qcache_queries_in_cache	1
show status like "Qcache_inserts";
Variable_name	Value
Qcache_inserts	1
show status like "Qcache_hits";
Variable_name	Value
Qcache_hits	0
select * from v1;
c	d
show status like "Qcache_queries_in_cache";
Variable_name	Value
Qcache_queries_in_cache	1
show status like "Qcache_inserts";
Variable_name	Value
Qcache_inserts	1
show status like "Qcache_hits";
Variable_name	Value
Qcache_hits	1
select * from t1;
a	b
show status like "Qcache_queries_in_cache";
Variable_name	Value
Qcache_queries_in_cache	1
show status like "Qcache_inserts";
Variable_name	Value
Qcache_inserts	1
show status like "Qcache_hits";
Variable_name	Value
Qcache_hits	1
drop view v1;
set query_cache_type=default;
drop table t1;
set GLOBAL query_cache_size=default;
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;
a
1
2
3
explain select * from v1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	3	
469
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	6	Using temporary
unknown's avatar
VIEW  
unknown committed
470 471 472 473 474 475 476 477 478 479 480 481
select * from t1;
a
1
2
3
1
2
3
drop view v1;
drop table t1;
create table t1 (a int);
create view v1 as select distinct a from t1 WITH CHECK OPTION;
unknown's avatar
unknown committed
482
ERROR HY000: CHECK OPTION on non-updatable view 'test.v1'
unknown's avatar
unknown committed
483 484 485
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;
unknown's avatar
VIEW  
unknown committed
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
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;
c
3
4
5
6
11
create algorithm=temptable view v2 (c) as select b+1 from t1;
select test.c from v2 test;
c
3
4
5
6
11
select test1.* from v1 test1, v2 test2 where test1.c=test2.c;
c
3
4
5
6
11
select test2.* from v1 test1, v2 test2 where test1.c=test2.c;
c
3
4
5
6
11
drop 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+1
5
4
explain select * from v1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	<derived2>	ALL	NULL	NULL	NULL	NULL	2	
534
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	4	Using filesort
unknown's avatar
VIEW  
unknown committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
drop 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	Extra
a+1	bigint(17)	YES		NULL	
select * from t2;
a+1
2
3
4
5
drop 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 updatable
update v2 set a=a+c;
ERROR HY000: The target table v2 of the UPDATE is not updatable
update v1 set a=a+c;
select * from v1;
a	c
13	3
24	4
35	5
46	6
61	11
select * from t1;
a	b
13	2
24	3
35	4
46	5
61	10
drop 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 updatable
update 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 updatable
update t2,v1 set v1.a=v1.a+v1.c where t2.x=v1.a;
select * from v1;
a	c
13	3
24	4
30	5
40	6
50	11
select * from t1;
a	b
13	2
24	3
30	4
40	5
50	10
drop table t1,t2;
drop view v1,v2;
create database mysqltest;
create table mysqltest.t1 (a int, b int, primary key(a));
insert into mysqltest.t1 values (10,2), (20,3), (30,4), (40,5), (50,10);
create table mysqltest.t2 (x int);
insert into mysqltest.t2 values (3), (4), (5), (6);
create view mysqltest.v1 (a,c) as select a, b+1 from mysqltest.t1;
create view mysqltest.v2 (a,c) as select a, b from mysqltest.t1;
create view mysqltest.v3 (a,c) as select a, b+1 from mysqltest.t1;
grant update (a) on mysqltest.v2 to mysqltest_1@localhost;
grant update on mysqltest.v1 to mysqltest_1@localhost;
grant select on mysqltest.* to mysqltest_1@localhost;
use mysqltest;
update t2,v1 set v1.a=v1.a+v1.c where t2.x=v1.c;
select * from t1;
a	b
13	2
24	3
35	4
46	5
50	10
update v1 set a=a+c;
select * from t1;
a	b
16	2
28	3
40	4
52	5
61	10
update t2,v2 set v2.a=v2.a+v2.c where t2.x=v2.c;
select * from t1;
a	b
16	2
31	3
44	4
57	5
61	10
update v2 set a=a+c;
select * from t1;
a	b
18	2
34	3
48	4
62	5
71	10
update t2,v2 set v2.c=v2.a+v2.c where t2.x=v2.c;
ERROR 42000: UPDATE command denied to user 'mysqltest_1'@'localhost' for column 'c' in table 'v2'
update v2 set c=a+c;
ERROR 42000: UPDATE command denied to user 'mysqltest_1'@'localhost' for column 'c' in table 'v2'
update t2,v3 set v3.a=v3.a+v3.c where t2.x=v3.c;
ERROR 42000: UPDATE command denied to user 'mysqltest_1'@'localhost' for column 'a' in table 'v3'
update v3 set a=a+c;
ERROR 42000: update command denied to user 'mysqltest_1'@'localhost' for table 'v3'
use test;
REVOKE ALL PRIVILEGES, GRANT OPTION FROM mysqltest_1@localhost;
drop database mysqltest;
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;
c
20
30
explain extended select * from v1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	5	Using where
Warnings:
670
Note	1003	select `test`.`t1`.`b` AS `c` from `test`.`v1` where (`test`.`t1`.`a` < 3)
unknown's avatar
VIEW  
unknown committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
update v1 set c=c+1;
select * from t1;
a	b
1	21
2	31
3	40
4	50
5	100
create view v2 (c) as select b from t1 where a>=3;
select * from v1, v2;
c	c
21	40
31	40
21	50
31	50
21	100
31	100
drop 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 updatable
delete from v1 where c < 4;
select * from v1;
a	c
2	4
3	5
4	6
5	11
select * from t1;
a	b
2	3
3	4
4	5
5	10
drop table t1;
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 updatable
delete v1 from t2,v1 where t2.x=v1.a;
select * from v1;
a	c
5	11
select * from t1;
a	b
5	10
drop table t1,t2;
drop view v1,v2;
create database mysqltest;
create table mysqltest.t1 (a int, b int, primary key(a));
insert into mysqltest.t1 values (1,2), (2,3), (3,4), (4,5), (5,10);
create table mysqltest.t2 (x int);
insert into mysqltest.t2 values (3), (4), (5), (6);
create view mysqltest.v1 (a,c) as select a, b+1 from mysqltest.t1;
create view mysqltest.v2 (a,c) as select a, b+1 from mysqltest.t1;
grant delete on mysqltest.v1 to mysqltest_1@localhost;
grant select on mysqltest.* to mysqltest_1@localhost;
use mysqltest;
delete from v1 where c < 4;
select * from t1;
a	b
2	3
3	4
4	5
5	10
delete v1 from t2,v1 where t2.x=v1.c;
select * from t1;
a	b
5	10
delete v2 from t2,v2 where t2.x=v2.c;
ERROR 42000: delete command denied to user 'mysqltest_1'@'localhost' for table 'v2'
delete from v2 where c < 4;
ERROR 42000: delete command denied to user 'mysqltest_1'@'localhost' for table 'v2'
use test;
REVOKE ALL PRIVILEGES, GRANT OPTION FROM mysqltest_1@localhost;
drop database mysqltest;
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;
760
set updatable_views_with_limit=NO;
unknown's avatar
VIEW  
unknown committed
761 762 763 764 765
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 updatable
766
set updatable_views_with_limit=YES;
unknown's avatar
VIEW  
unknown committed
767 768 769
update v1 set x=x+1 limit 1;
update v2 set x=x+1 limit 1;
Warnings:
770
Note	1355	View being updated does not have complete key of underlying table in it
771 772 773 774
set updatable_views_with_limit=DEFAULT;
show variables like "updatable_views_with_limit";
Variable_name	Value
updatable_views_with_limit	YES
unknown's avatar
VIEW  
unknown committed
775 776
select * from t1;
a	b	c
777 778 779 780 781
15	2	-1
22	3	-2
32	4	-3
42	5	-4
52	10	-5
unknown's avatar
VIEW  
unknown committed
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
drop 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 updatable
insert into v4 values (-60,4,30);
ERROR HY000: The target table v4 of the INSERT is not updatable
insert into v5 values (-60,4,30);
ERROR HY000: The target table v5 of the INSERT is not updatable
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;
a	b	c
10	2	-1
20	3	-2
30	4	-60
50	6	-100
40	5	NULL
drop 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 updatable
insert into v4 select c, b, a from t2;
ERROR HY000: The target table v4 of the INSERT is not updatable
insert into v5 select c, b, a from t2;
ERROR HY000: The target table v5 of the INSERT is not updatable
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;
a	b	c
10	2	-1
20	3	-2
30	4	-60
50	6	-100
40	5	NULL
834
drop table t1, t2;
unknown's avatar
VIEW  
unknown committed
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
drop view v1,v2,v3,v4,v5;
create database mysqltest;
create table mysqltest.t1 (a int, b int, primary key(a));
insert into mysqltest.t1 values (1,2), (2,3);
create table mysqltest.t2 (x int, y int);
insert into mysqltest.t2 values (3,4);
create view mysqltest.v1 (a,c) as select a, b from mysqltest.t1;
create view mysqltest.v2 (a,c) as select a, b from mysqltest.t1;
grant insert on mysqltest.v1 to mysqltest_1@localhost;
grant select on mysqltest.* to mysqltest_1@localhost;
use mysqltest;
insert into v1 values (5,6);
select * from t1;
a	b
1	2
2	3
5	6
insert into v1 select x,y from t2;
select * from t1;
a	b
1	2
2	3
5	6
3	4
insert into v2 values (5,6);
ERROR 42000: insert command denied to user 'mysqltest_1'@'localhost' for table 'v2'
insert into v2 select x,y from t2;
ERROR 42000: insert command denied to user 'mysqltest_1'@'localhost' for table 'v2'
use test;
REVOKE ALL PRIVILEGES, GRANT OPTION FROM mysqltest_1@localhost;
drop database mysqltest;
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	x
1	NULL
2	2
3	3
drop 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;
x
2
3
drop 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 updatable
drop 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()
0
insert into t1 (b) values (2);
select last_insert_id();
last_insert_id()
2
select * from t1;
a	b
1	1
2	2
drop view v1;
drop table t1;
create database mysqltest;
create table mysqltest.t1 (a int, b int);
create table mysqltest.t2 (a int, b int);
grant update on mysqltest.t1 to mysqltest_1@localhost;
grant update(b) on mysqltest.t2 to mysqltest_1@localhost;
grant create view,update on test.* to mysqltest_1@localhost;
create view v1 as select * from mysqltest.t1;
create view v2 as select b from mysqltest.t2;
create view mysqltest.v1 as select * from mysqltest.t1;
ERROR 42000: create view command denied to user 'mysqltest_1'@'localhost' for table 'v1'
create view v3 as select a from mysqltest.t2;
ERROR 42000: ANY command denied to user 'mysqltest_1'@'localhost' for column 'a' in table 't2'
create table mysqltest.v3 (b int);
grant create view on mysqltest.v3 to mysqltest_1@localhost;
drop table mysqltest.v3;
create view mysqltest.v3 as select b from mysqltest.t2;
grant create view, update on mysqltest.v3 to mysqltest_1@localhost;
927
drop view mysqltest.v3;
unknown's avatar
VIEW  
unknown committed
928
create view mysqltest.v3 as select b from mysqltest.t2;
929
grant create view, update, insert on mysqltest.v3 to mysqltest_1@localhost;
unknown's avatar
VIEW  
unknown committed
930 931
drop view mysqltest.v3;
create view mysqltest.v3 as select b from mysqltest.t2;
932 933 934 935 936
ERROR 42000: create view command denied to user 'mysqltest_1'@'localhost' for column 'b' in table 'v3'
create table mysqltest.v3 (b int);
grant select(b) on mysqltest.v3 to mysqltest_1@localhost;
drop table mysqltest.v3;
create view mysqltest.v3 as select b from mysqltest.t2;
unknown's avatar
VIEW  
unknown committed
937 938 939 940 941 942 943 944 945 946
ERROR 42000: create view command denied to user 'mysqltest_1'@'localhost' for table 'v3'
create view v4 as select b+1 from mysqltest.t2;
ERROR 42000: SELECT command denied to user 'mysqltest_1'@'localhost' for column 'b' in table 't2'
grant create view,update,select on test.* to mysqltest_1@localhost;
create view v4 as select b+1 from mysqltest.t2;
ERROR 42000: SELECT command denied to user 'mysqltest_1'@'localhost' for column 'b' in table 't2'
grant update,select(b) on mysqltest.t2 to mysqltest_1@localhost;
create view v4 as select b+1 from mysqltest.t2;
REVOKE ALL PRIVILEGES, GRANT OPTION FROM mysqltest_1@localhost;
drop database mysqltest;
947
drop view v1,v2,v4;
948 949 950 951
set sql_mode='ansi';
create table t1 ("a*b" int);
create view v1 as select "a*b" from t1;
show create view v1;
unknown's avatar
unknown committed
952
View	Create View
953
v1	CREATE VIEW "test"."v1" AS select `test`.`t1`.`a*b` AS `a*b` from `test`.`t1`
954 955 956
drop view v1;
drop table t1;
set sql_mode=default;
957 958 959 960 961 962
create table t1 (t_column int);
create view v1 as select 'a';
select * from v1, t1;
a	t_column
drop view v1;
drop table t1;
963 964 965 966 967 968 969 970 971
create table `t1a``b` (col1 char(2));
create view v1 as select * from `t1a``b`;
select * from v1;
col1
describe v1;
Field	Type	Null	Key	Default	Extra
col1	char(2)	YES		NULL	
drop view v1;
drop table `t1a``b`;
972 973 974 975 976 977 978 979 980 981
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)
drop table t1;
select * from v1;
ERROR HY000: View 'test.v1' references invalid table(s) or column(s)
drop view v1;
982 983
create view v1 (a,a) as select 'a','a';
ERROR 42S21: Duplicate column name 'a'
984
create procedure p1 () begin declare v int; create view v1 as select v; end;//
985
Warnings:
986
Warning	1311	Referring to uninitialized variable v
987
call p1();
988
ERROR HY000: View's SELECT contains a variable or parameter
989 990 991 992 993 994 995 996 997 998 999
drop 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	col2
5	Hello, view world
drop view v2, v1;
drop table t1;
1000 1001 1002 1003 1004 1005
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 table
drop view v1;
drop table t1;
unknown's avatar
unknown committed
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
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);
col2
p1
p2
p4
select distinct first.col2 from v1 first where first.col2 in (select second.col2 from t1 second where second.col1<>first.col1);
col2
p1
p2
p4
drop view v1;
drop table t1;
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
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;
a
2
3
4
drop view v1;
drop table t1;
1043 1044 1045 1046
CREATE VIEW v02 AS SELECT * FROM DUAL;
ERROR HY000: No tables used
SHOW TABLES;
Tables_in_test	table_type
1047 1048 1049 1050 1051
CREATE VIEW v1 AS SELECT EXISTS (SELECT 1 UNION SELECT 2);
select * from v1;
EXISTS (SELECT 1 UNION SELECT 2)
1
drop view v1;
1052 1053 1054
create table t1 (col1 int,col2 char(22));
create view v1 as select * from t1;
create index i1 on v1 (col1);
unknown's avatar
unknown committed
1055
ERROR HY000: 'test.v1' is not BASE TABLE
1056 1057
drop view v1;
drop table t1;
1058 1059
CREATE VIEW v1 (f1,f2,f3,f4) AS SELECT connection_id(), pi(), current_user(), version();
SHOW CREATE VIEW v1;
unknown's avatar
unknown committed
1060
View	Create View
1061
v1	CREATE ALGORITHM=UNDEFINED VIEW `test`.`v1` AS select sql_no_cache connection_id() AS `f1`,pi() AS `f2`,current_user() AS `f3`,version() AS `f4`
1062
drop view v1;
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
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	s2
1	2
2	2
1	3
2	3
1	2
2	2
1	3
2	3
drop view v1;
drop tables t1, t2;
1080 1081 1082 1083 1084 1085 1086 1087 1088
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(*)
2
drop view v1;
drop table t1;
1089 1090 1091 1092 1093
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;
unknown's avatar
unknown committed
1094
View	Create View
1095
v2	CREATE ALGORITHM=UNDEFINED VIEW `test`.`v2` AS select `test`.`t2`.`a` AS `a` from `test`.`t2` where `a` in (select `v1`.`a` AS `a` from `test`.`v1`)
1096 1097
drop view v2, v1;
drop table t1, t2;
1098 1099
CREATE VIEW `v 1` AS select 5 AS `5`;
show create view `v 1`;
unknown's avatar
unknown committed
1100
View	Create View
1101
v 1	CREATE ALGORITHM=UNDEFINED VIEW `test`.`v 1` AS select 5 AS `5`
1102
drop view `v 1`;
1103 1104 1105 1106 1107 1108
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;
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
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	c2
1	real Beer
7	almost real Beer
CREATE VIEW v1 AS SELECT  * from t1 WHERE match (c2) against ('Beer');
select * from v1;
c1	c2
1	real Beer
7	almost real Beer
drop view v1;
drop table t1;
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
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;
a
1
2
3
select distinct a from v1 limit 2;
a
1
2
select distinct a from t1 limit 2;
a
1
2
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
prepare stmt1 from "select distinct a from v1 limit 2";
execute stmt1;
a
1
2
execute stmt1;
a
1
2
deallocate 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.0000
1155 1156
drop view v1;
drop table t1;
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
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	b
1	1
show create view v3;
unknown's avatar
unknown committed
1168
View	Create View
1169
v3	CREATE ALGORITHM=UNDEFINED VIEW `test`.`v3` AS select `v1`.`col1` AS `a`,`v2`.`col1` AS `b` from `test`.`v1` join `test`.`v2` where (`v1`.`col1` = `v2`.`col1`)
1170 1171
drop view v3, v2, v1;
drop table t2, t1;
1172 1173 1174
create function `f``1` () returns int return 5;
create view v1 as select test.`f``1` ();
show create view v1;
unknown's avatar
unknown committed
1175
View	Create View
1176
v1	CREATE ALGORITHM=UNDEFINED VIEW `test`.`v1` AS select `test`.`f``1`() AS `test.``f````1`` ()`
1177 1178 1179 1180 1181
select * from v1;
test.`f``1` ()
5
drop view v1;
drop function `f``1`;
1182 1183 1184 1185 1186 1187 1188
create function x () returns int return 5;
create view v1 as select x ();
select * from v1;
x ()
5
drop view v1;
drop function x;
1189 1190 1191
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;
unknown's avatar
unknown committed
1192
View	Create View
1193
v2	CREATE ALGORITHM=UNDEFINED VIEW `test`.`v2` AS select (`test`.`t2`.`col1` collate latin1_german1_ci) AS `col1 collate latin1_german1_ci` from `test`.`t2`
1194
show create view v2;
unknown's avatar
unknown committed
1195
View	Create View
1196
v2	CREATE ALGORITHM=UNDEFINED VIEW `test`.`v2` AS select (`test`.`t2`.`col1` collate latin1_german1_ci) AS `col1 collate latin1_german1_ci` from `test`.`t2`
1197 1198
drop view v2;
drop table t2;
1199 1200 1201 1202 1203 1204 1205 1206 1207
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;
5
5
5
drop view v1;
drop table t1;
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
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 42000: FUNCTION test.x1 does not exist
show 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	Comment
t1	MyISAM	9	Fixed	0	0	0	21474836479	1024	0	NULL	#	#	NULL	latin1_swedish_ci	NULL		
v1	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	#	#	NULL	NULL	NULL	NULL	view
show 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	Comment
t1	MyISAM	9	Fixed	0	0	0	21474836479	1024	0	NULL	#	#	NULL	latin1_swedish_ci	NULL		
v1	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	NULL	#	#	NULL	NULL	NULL	NULL	view
drop view v1;
drop table t1;
1224 1225
create view v1 as select 99999999999999999999999999999999999999999999999999999 as col1;
show create view v1;
unknown's avatar
unknown committed
1226
View	Create View
1227
v1	CREATE ALGORITHM=UNDEFINED VIEW `test`.`v1` AS select 99999999999999999999999999999999999999999999999999999 AS `col1`
1228
drop view v1;
1229 1230 1231 1232 1233 1234 1235 1236
create table t (c char);
create view v as select c from t;
insert into v values ('');
select * from v;
c

drop view v;
drop table t;
1237 1238 1239 1240 1241 1242 1243 1244
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 a+1 from t1 where b >= 4;
select c from v1 where exists (select * from t1 where a=2 and b=c);
c
4
drop view v1;
drop table t1;
1245 1246
create view v1 as select cast(1 as char(3));
show create view v1;
unknown's avatar
unknown committed
1247
View	Create View
1248
v1	CREATE ALGORITHM=UNDEFINED VIEW `test`.`v1` AS select cast(1 as char(3) charset latin1) AS `cast(1 as char(3))`
1249 1250 1251 1252
select * from v1;
cast(1 as char(3))
1
drop view v1;
1253 1254 1255 1256 1257 1258 1259
create view v1 as select 'a',1;
create view v2 as select * from v1 union all select * from v1;
create view v3 as select * from v2 where 1 = (select `1` from v2);
create view v4 as select * from v3;
select * from v4;
ERROR 21000: Subquery returns more than 1 row
drop view v4, v3, v2, v1;
1260 1261 1262 1263 1264 1265 1266 1267
create view v1 as select 5 into @w;
ERROR HY000: View's SELECT contains a 'INTO' clause
create view v1 as select 5 into outfile 'ttt';
ERROR HY000: View's SELECT contains a 'INTO' clause
create table t1 (a int);
create view v1 as select a from t1 procedure analyse();
ERROR HY000: View's SELECT contains a 'PROCEDURE' clause
drop table t1;
1268 1269 1270 1271 1272 1273 1274 1275 1276
create table t1 (s1 int, primary key (s1));
create view v1 as select * from t1;
insert into v1 values (1) on duplicate key update s1 = 7;
insert into v1 values (1) on duplicate key update s1 = 7;
select * from t1;
s1
7
drop view v1;
drop table t1;
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
create table t1 (col1 int);
create table t2 (col1 int);
create view v1 as select * from t1;
create view v2 as select * from v1;
update v2 set col1 = (select max(col1) from v1);
ERROR HY000: You can't specify target table 'v2' for update in FROM clause
delete from v2 where col1 = (select max(col1) from v1);
ERROR HY000: You can't specify target table 'v2' for update in FROM clause
insert into v2 values ((select max(col1) from v1));
ERROR HY000: You can't specify target table 'v2' for update in FROM clause
drop view v2,v1;
drop table t1,t2;
1289 1290 1291 1292 1293 1294
create table t1 (s1 int);
create view v1 as select * from t1;
handler v1 open as xx;
ERROR HY000: 'test.v1' is not BASE TABLE
drop view v1;
drop table t1;
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
create table t1(a int);
insert into t1 values (0), (1), (2), (3);
create table t2 (a int);
insert into t2 select a from t1 where a > 1;
create view v1 as select a from t1 where a > 1;
select * from t1 left join (t2 as t, v1) on v1.a=t1.a;
a	a	a
0	NULL	NULL
1	NULL	NULL
2	2	2
2	3	2
3	2	3
3	3	3
select * from t1 left join (t2 as t, t2) on t2.a=t1.a;
a	a	a
0	NULL	NULL
1	NULL	NULL
2	2	2
2	3	2
3	2	3
3	3	3
drop view v1;
unknown's avatar
unknown committed
1317
drop table t1, t2;
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
create table t1 (s1 char);
create view v1 as select s1 collate latin1_german1_ci as s1 from t1;
insert into v1 values ('a');
select * from v1;
s1
a
update v1 set s1='b';
select * from v1;
s1
b
update v1,t1 set v1.s1='c' where t1.s1=v1.s1;
select * from v1;
s1
c
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
prepare stmt1 from "update v1,t1 set v1.s1=? where t1.s1=v1.s1";
set @arg='d';
execute stmt1 using @arg;
select * from v1;
s1
d
set @arg='e';
execute stmt1 using @arg;
select * from v1;
s1
e
deallocate prepare stmt1;
1344 1345
drop view v1;
drop table t1;
unknown's avatar
unknown committed
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
create table t1 (a int);
create table t2 (a int);
create view v1 as select * from t1;
lock tables t1 read, v1 read;
select * from v1;
a
select * from t2;
ERROR HY000: Table 't2' was not locked with LOCK TABLES
drop view v1;
drop table t1, t2;
unknown's avatar
unknown committed
1356 1357 1358 1359
create table t1 (a int);
create view v1 as select * from t1 where a < 2 with check option;
insert into v1 values(1);
insert into v1 values(3);
unknown's avatar
unknown committed
1360
ERROR HY000: CHECK OPTION failed 'test.v1'
unknown's avatar
unknown committed
1361 1362
insert ignore into v1 values (2),(3),(0);
Warnings:
unknown's avatar
unknown committed
1363 1364
Error	1369	CHECK OPTION failed 'test.v1'
Error	1369	CHECK OPTION failed 'test.v1'
unknown's avatar
unknown committed
1365 1366 1367 1368 1369 1370 1371
select * from t1;
a
1
0
delete from t1;
insert into v1 SELECT 1;
insert into v1 SELECT 3;
unknown's avatar
unknown committed
1372
ERROR HY000: CHECK OPTION failed 'test.v1'
unknown's avatar
unknown committed
1373 1374 1375 1376
create table t2 (a int);
insert into t2 values (2),(3),(0);
insert ignore into v1 SELECT a from t2;
Warnings:
unknown's avatar
unknown committed
1377 1378
Error	1369	CHECK OPTION failed 'test.v1'
Error	1369	CHECK OPTION failed 'test.v1'
unknown's avatar
unknown committed
1379 1380 1381 1382 1383 1384
select * from t1;
a
1
0
update v1 set a=-1 where a=0;
update v1 set a=2 where a=1;
unknown's avatar
unknown committed
1385
ERROR HY000: CHECK OPTION failed 'test.v1'
unknown's avatar
unknown committed
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
select * from t1;
a
1
-1
update v1 set a=0 where a=0;
insert into t2 values (1);
update v1,t2 set v1.a=v1.a-1 where v1.a=t2.a;
select * from t1;
a
0
-1
update v1 set a=a+1;
update ignore v1,t2 set v1.a=v1.a+1 where v1.a=t2.a;
Warnings:
unknown's avatar
unknown committed
1400
Error	1369	CHECK OPTION failed 'test.v1'
unknown's avatar
unknown committed
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
select * from t1;
a
1
1
drop view v1;
drop table t1, t2;
create table t1 (a int);
create view v1 as select * from t1 where a < 2 with check option;
create view v2 as select * from v1 where a > 0 with local check option;
create view v3 as select * from v1 where a > 0 with cascaded check option;
insert into v2 values (1);
insert into v3 values (1);
insert into v2 values (0);
unknown's avatar
unknown committed
1414
ERROR HY000: CHECK OPTION failed 'test.v2'
unknown's avatar
unknown committed
1415
insert into v3 values (0);
unknown's avatar
unknown committed
1416
ERROR HY000: CHECK OPTION failed 'test.v3'
unknown's avatar
unknown committed
1417 1418
insert into v2 values (2);
insert into v3 values (2);
unknown's avatar
unknown committed
1419
ERROR HY000: CHECK OPTION failed 'test.v3'
unknown's avatar
unknown committed
1420 1421 1422 1423 1424 1425 1426
select * from t1;
a
1
1
2
drop view v3,v2,v1;
drop table t1;
1427 1428 1429 1430
create table t1 (a int, primary key (a));
create view v1 as select * from t1 where a < 2 with check option;
insert into v1 values (1) on duplicate key update a=2;
insert into v1 values (1) on duplicate key update a=2;
unknown's avatar
unknown committed
1431
ERROR HY000: CHECK OPTION failed 'test.v1'
1432 1433
insert ignore into v1 values (1) on duplicate key update a=2;
Warnings:
unknown's avatar
unknown committed
1434
Error	1369	CHECK OPTION failed 'test.v1'
1435 1436 1437 1438 1439
select * from t1;
a
1
drop view v1;
drop table t1;
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
create table t1 (s1 int);
create view v1 as select * from t1;
create view v2 as select * from v1;
alter view v1 as select * from v2;
ERROR 42S02: Table 'test.v1' doesn't exist
alter view v1 as select * from v1;
ERROR 42000: Not unique table/alias: 'v1'
create or replace view v1 as select * from v2;
ERROR 42S02: Table 'test.v1' doesn't exist
create or replace view v1 as select * from v1;
ERROR 42000: Not unique table/alias: 'v1'
drop view v2,v1;
drop table t1;
create table t1 (a int);
create view v1 as select * from t1;
show create view v1;
View	Create View
v1	CREATE ALGORITHM=UNDEFINED VIEW `test`.`v1` AS select `test`.`t1`.`a` AS `a` from `test`.`t1`
alter algorithm=undefined view v1 as select * from t1 with check option;
show create view v1;
View	Create View
v1	CREATE ALGORITHM=UNDEFINED VIEW `test`.`v1` AS select `test`.`t1`.`a` AS `a` from `test`.`t1` WITH LOCAL CHECK OPTION
alter algorithm=merge view v1 as select * from t1 with cascaded check option;
show create view v1;
View	Create View
v1	CREATE ALGORITHM=MERGE VIEW `test`.`v1` AS select `test`.`t1`.`a` AS `a` from `test`.`t1` WITH CASCADED CHECK OPTION
alter algorithm=temptable view v1 as select * from t1;
show create view v1;
View	Create View
v1	CREATE ALGORITHM=TEMPTABLE VIEW `test`.`v1` AS select `test`.`t1`.`a` AS `a` from `test`.`t1`
drop view v1;
drop table t1;
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
create table t1 (s1 int);
create table t2 (s1 int);
create view v2 as select * from t2 where s1 in (select s1 from t1);
insert into v2 values (5);
insert into t1 values (5);
select * from v2;
s1
5
update v2 set s1 = 0;
select * from v2;
s1
select * from t2;
s1
0
alter view v2 as select * from t2 where s1 in (select s1 from t1) with check option;
insert into v2 values (5);
update v2 set s1 = 1;
ERROR HY000: CHECK OPTION failed 'test.v2'
insert into t1 values (1);
update v2 set s1 = 1;
select * from v2;
s1
1
select * from t2;
s1
0
1
prepare stmt1 from "select * from v2;";
execute stmt1;
s1
1
insert into t1 values (0);
execute stmt1;
s1
0
1
deallocate prepare stmt1;
drop view v2;
drop table t1, t2;