view.result 98.2 KB
Newer Older
1
drop table if exists t1,t2,t3,t4,t9,`t1a``b`,v1,v2,v3,v4,v5,v6;
2
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
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;
unknown's avatar
unknown committed
9
ERROR HY000: View's SELECT refers to a temporary table 't1'
unknown's avatar
VIEW  
unknown committed
10
drop table t1;
unknown's avatar
unknown committed
11
create table t1 (a int, b int);
unknown's avatar
VIEW  
unknown committed
12 13 14
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
15 16 17
create view v1 (c,d) as select a,b from t1
where a = @@global.max_user_connections;
ERROR HY000: View's SELECT contains a variable or parameter
unknown's avatar
VIEW  
unknown committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
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
37
View	Create View
unknown's avatar
unknown committed
38
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select (`t1`.`b` + 1) AS `c` from `t1`
unknown's avatar
VIEW  
unknown committed
39
show create view v1;
unknown's avatar
unknown committed
40
View	Create View
unknown's avatar
unknown committed
41
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select (`t1`.`b` + 1) AS `c` from `t1`
unknown's avatar
VIEW  
unknown committed
42 43 44 45 46 47 48 49 50 51 52 53 54
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
55
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	5	
unknown's avatar
VIEW  
unknown committed
56
Warnings:
unknown's avatar
unknown committed
57
Note	1003	select (`test`.`t1`.`b` + 1) AS `c` from `test`.`t1`
unknown's avatar
VIEW  
unknown committed
58
create algorithm=temptable view v2 (c) as select b+1 from t1;
unknown's avatar
unknown committed
59 60
show create view v2;
View	Create View
unknown's avatar
unknown committed
61
v2	CREATE ALGORITHM=TEMPTABLE DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v2` AS select (`t1`.`b` + 1) AS `c` from `t1`
unknown's avatar
VIEW  
unknown committed
62 63 64 65 66 67 68 69 70 71
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	
72
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	5	
unknown's avatar
VIEW  
unknown committed
73
Warnings:
74
Note	1003	select `v2`.`c` AS `c` from `test`.`v2`
unknown's avatar
VIEW  
unknown committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88
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
89
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	5	
unknown's avatar
VIEW  
unknown committed
90
Warnings:
unknown's avatar
unknown committed
91
Note	1003	select ((`test`.`t1`.`b` + 1) + 1) AS `c` from `test`.`t1`
unknown's avatar
VIEW  
unknown committed
92 93 94 95 96 97 98 99 100 101 102
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	
103 104
2	DERIVED	<derived3>	ALL	NULL	NULL	NULL	NULL	5	
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	5	
unknown's avatar
VIEW  
unknown committed
105
Warnings:
106
Note	1003	select `v4`.`c` AS `c` from `test`.`v4`
unknown's avatar
VIEW  
unknown committed
107 108 109 110 111 112 113 114 115 116 117
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	
118
3	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	5	
unknown's avatar
VIEW  
unknown committed
119
Warnings:
unknown's avatar
unknown committed
120
Note	1003	select (`v2`.`c` + 1) AS `c` from `test`.`v2`
unknown's avatar
VIEW  
unknown committed
121 122 123 124 125 126 127 128 129 130 131
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	
132
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	5	
unknown's avatar
VIEW  
unknown committed
133
Warnings:
134
Note	1003	select `v6`.`c` AS `c` from `test`.`v6`
unknown's avatar
VIEW  
unknown committed
135
show tables;
136 137 138 139 140 141 142 143 144
Tables_in_test
t1
v1
v2
v3
v4
v5
v6
show full tables;
unknown's avatar
unknown committed
145
Tables_in_test	Table_type
146 147 148 149 150 151 152
t1	BASE TABLE
v1	VIEW
v2	VIEW
v3	VIEW
v4	VIEW
v5	VIEW
v6	VIEW
unknown's avatar
VIEW  
unknown committed
153 154
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
155
t1	MyISAM	10	Fixed	5	9	45	#	1024	0	NULL	#	#	NULL	latin1_swedish_ci	NULL		
156 157 158 159 160 161
v1	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	VIEW
v3	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	VIEW
v5	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	VIEW
unknown's avatar
VIEW  
unknown committed
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
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
drop view v100;
199
ERROR 42S02: Unknown table 'v100'
unknown's avatar
VIEW  
unknown committed
200 201 202 203 204 205 206 207 208 209
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;
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
select * from t1 natural left join v1;
a
1
2
3
select * from v2 natural left join t1;
a
0
1
2
select * from v2 natural left join v1;
a
0
1
2
unknown's avatar
VIEW  
unknown committed
225 226 227 228 229 230 231 232 233 234 235 236 237
drop 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;
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	
238
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	6	Using temporary
unknown's avatar
VIEW  
unknown committed
239 240 241 242 243 244 245 246 247 248 249 250
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
251
ERROR HY000: CHECK OPTION on non-updatable view 'test.v1'
unknown's avatar
unknown committed
252 253 254
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
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 293 294 295 296 297 298 299 300 301 302
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	
303
2	DERIVED	t1	ALL	NULL	NULL	NULL	NULL	4	Using filesort
unknown's avatar
VIEW  
unknown committed
304 305 306 307 308 309 310 311
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
unknown's avatar
unknown committed
312
a+1	bigint(12)	YES		NULL	
unknown's avatar
VIEW  
unknown committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 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
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 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
382
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	5	Using where
unknown's avatar
VIEW  
unknown committed
383
Warnings:
unknown's avatar
unknown committed
384
Note	1003	select `test`.`t1`.`b` AS `c` from `test`.`t1` where (`test`.`t1`.`a` < 3)
unknown's avatar
VIEW  
unknown committed
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
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 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;
446
set updatable_views_with_limit=NO;
unknown's avatar
VIEW  
unknown committed
447 448 449 450 451
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
452
set updatable_views_with_limit=YES;
unknown's avatar
VIEW  
unknown committed
453 454 455
update v1 set x=x+1 limit 1;
update v2 set x=x+1 limit 1;
Warnings:
456
Note	1355	View being updated does not have complete key of underlying table in it
457 458 459 460
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
461 462
select * from t1;
a	b	c
463 464 465 466 467
15	2	-1
22	3	-2
32	4	-3
42	5	-4
52	10	-5
unknown's avatar
VIEW  
unknown committed
468 469 470 471 472 473 474 475 476 477
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);
478
ERROR HY000: The target table v3 of the INSERT is not insertable-into
unknown's avatar
VIEW  
unknown committed
479
insert into v4 values (-60,4,30);
480
ERROR HY000: The target table v4 of the INSERT is not insertable-into
unknown's avatar
VIEW  
unknown committed
481
insert into v5 values (-60,4,30);
482
ERROR HY000: The target table v5 of the INSERT is not insertable-into
unknown's avatar
VIEW  
unknown committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
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;
505
ERROR HY000: The target table v3 of the INSERT is not insertable-into
unknown's avatar
VIEW  
unknown committed
506
insert into v4 select c, b, a from t2;
507
ERROR HY000: The target table v4 of the INSERT is not insertable-into
unknown's avatar
VIEW  
unknown committed
508
insert into v5 select c, b, a from t2;
509
ERROR HY000: The target table v5 of the INSERT is not insertable-into
unknown's avatar
VIEW  
unknown committed
510 511 512 513 514 515 516 517 518 519
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
520
drop table t1, t2;
unknown's avatar
VIEW  
unknown committed
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
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	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;
537
y
unknown's avatar
VIEW  
unknown committed
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
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;
566 567 568 569
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
570
View	Create View
571
v1	CREATE VIEW "v1" AS select "t1"."a*b" AS "a*b" from "t1"
572 573 574
drop view v1;
drop table t1;
set sql_mode=default;
575 576 577 578 579 580
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;
581 582 583 584 585 586
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
587
col1	char(2)	YES		NULL	
588 589
drop view v1;
drop table `t1a``b`;
590 591 592 593 594
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');
595
ERROR HY000: View 'test.v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
596 597
drop table t1;
select * from v1;
598
ERROR HY000: View 'test.v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
599
drop view v1;
600 601
create view v1 (a,a) as select 'a','a';
ERROR 42S21: Duplicate column name 'a'
602 603 604 605 606 607 608 609 610 611
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;
612 613 614
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;
unknown's avatar
unknown committed
615
ERROR HY000: Key 'some_index' doesn't exist in table 'v1'
616 617
drop view v1;
drop table t1;
unknown's avatar
unknown committed
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
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;
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
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;
655 656 657
CREATE VIEW v02 AS SELECT * FROM DUAL;
ERROR HY000: No tables used
SHOW TABLES;
658
Tables_in_test
659 660 661 662 663
CREATE VIEW v1 AS SELECT EXISTS (SELECT 1 UNION SELECT 2);
select * from v1;
EXISTS (SELECT 1 UNION SELECT 2)
1
drop view v1;
664 665 666
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
667
ERROR HY000: 'test.v1' is not BASE TABLE
668 669
drop view v1;
drop table t1;
670 671
CREATE VIEW v1 (f1,f2,f3,f4) AS SELECT connection_id(), pi(), current_user(), version();
SHOW CREATE VIEW v1;
unknown's avatar
unknown committed
672
View	Create View
673
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select connection_id() AS `f1`,pi() AS `f2`,current_user() AS `f3`,version() AS `f4`
674
drop view v1;
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
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;
692 693 694 695 696 697 698 699 700
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;
701 702 703 704 705
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
706
View	Create View
707
v2	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v2` AS select `t2`.`a` AS `a` from `t2` where `t2`.`a` in (select `v1`.`a` AS `a` from `v1`)
708 709
drop view v2, v1;
drop table t1, t2;
710 711
CREATE VIEW `v 1` AS select 5 AS `5`;
show create view `v 1`;
unknown's avatar
unknown committed
712
View	Create View
unknown's avatar
unknown committed
713
v 1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v 1` AS select 5 AS `5`
714
drop view `v 1`;
715 716 717 718 719 720
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;
721 722 723 724 725 726 727 728 729 730 731 732 733
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;
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
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
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
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
767 768
drop view v1;
drop table t1;
769 770 771 772 773 774 775 776 777 778 779
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
780
View	Create View
unknown's avatar
unknown committed
781
v3	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`)
782 783
drop view v3, v2, v1;
drop table t2, t1;
784 785 786
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
787
View	Create View
788
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `test`.`f``1`() AS `test.``f````1`` ()`
789 790 791 792 793
select * from v1;
test.`f``1` ()
5
drop view v1;
drop function `f``1`;
794 795 796 797 798 799 800
create function x () returns int return 5;
create view v1 as select x ();
select * from v1;
x ()
5
drop view v1;
drop function x;
801 802 803
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
804
View	Create View
unknown's avatar
unknown committed
805
v2	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`
806
show create view v2;
unknown's avatar
unknown committed
807
View	Create View
unknown's avatar
unknown committed
808
v2	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`
809 810
drop view v2;
drop table t2;
811 812 813 814 815 816 817 818 819
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;
820
create function x1 () returns int return 5;
unknown's avatar
unknown committed
821
create table t1 (s1 int);
822 823 824
create view v1 as select x1() from t1;
drop function x1;
select * from v1;
825
ERROR HY000: View 'test.v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
826 827
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
828
t1	MyISAM	10	Fixed	0	0	0	#	1024	0	NULL	#	#	NULL	latin1_swedish_ci	NULL		
829
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 define
830 831
drop view v1;
drop table t1;
832 833
create view v1 as select 99999999999999999999999999999999999999999999999999999 as col1;
show create view v1;
unknown's avatar
unknown committed
834
View	Create View
unknown's avatar
unknown committed
835
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select 99999999999999999999999999999999999999999999999999999 AS `col1`
836
drop view v1;
837 838 839 840 841 842 843 844 845 846
set names utf8;
create table tü (cü char);
create view vü as select cü from tü;
insert into vü values ('ü');
select * from vü;

ü
drop view vü;
drop table tü;
set names latin1;
847 848 849 850 851 852 853 854
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;
855 856
create view v1 as select cast(1 as char(3));
show create view v1;
unknown's avatar
unknown committed
857
View	Create View
unknown's avatar
unknown committed
858
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select cast(1 as char(3) charset latin1) AS `cast(1 as char(3))`
859 860 861 862
select * from v1;
cast(1 as char(3))
1
drop view v1;
863 864
create table t1 (a int);
create view v1 as select a from t1;
unknown's avatar
unknown committed
865 866 867 868
create view v3 as select a from t1;
create database mysqltest;
rename table v1 to mysqltest.v1;
ERROR HY000: Changing schema from 'test' to 'mysqltest' is not allowed.
869
rename table v1 to v2;
unknown's avatar
unknown committed
870 871
rename table v3 to v1, v2 to t1;
ERROR 42S01: Table 't1' already exists
872
drop table t1;
unknown's avatar
unknown committed
873 874
drop view v2,v3;
drop database mysqltest;
875 876 877 878 879 880 881
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;
882 883 884 885 886 887 888
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
889 890
create view v1 as select 1 from (select 1) as d1;
ERROR HY000: View's SELECT contains a subquery in the FROM clause
891
drop table t1;
892 893 894 895 896 897 898 899 900
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;
901 902 903 904
create table t1 (col1 int);
create table t2 (col1 int);
create view v1 as select * from t1;
create view v2 as select * from v1;
905
create view v3 as select v2.col1 from v2,t2 where v2.col1 = t2.col1;
906
update v2 set col1 = (select max(col1) from v1);
907
ERROR HY000: The definition of table 'v1' prevents operation UPDATE on table 'v2'.
908
update v2 set col1 = (select max(col1) from t1);
909
ERROR HY000: The definition of table 'v2' prevents operation UPDATE on table 'v2'.
910 911 912
update v2 set col1 = (select max(col1) from v2);
ERROR HY000: You can't specify target table 'v2' for update in FROM clause
update v2,t2 set v2.col1 = (select max(col1) from v1) where v2.col1 = t2.col1;
913
ERROR HY000: The definition of table 'v1' prevents operation UPDATE on table 'v2'.
914
update t1,t2 set t1.col1 = (select max(col1) from v1) where t1.col1 = t2.col1;
915
ERROR HY000: The definition of table 'v1' prevents operation UPDATE on table 't1'.
916 917 918
update v1,t2 set v1.col1 = (select max(col1) from v1) where v1.col1 = t2.col1;
ERROR HY000: You can't specify target table 'v1' for update in FROM clause
update t2,v2 set v2.col1 = (select max(col1) from v1) where v2.col1 = t2.col1;
919
ERROR HY000: The definition of table 'v1' prevents operation UPDATE on table 't2'.
920
update t2,t1 set t1.col1 = (select max(col1) from v1) where t1.col1 = t2.col1;
921
ERROR HY000: The definition of table 'v1' prevents operation UPDATE on table 't2'.
922
update t2,v1 set v1.col1 = (select max(col1) from v1) where v1.col1 = t2.col1;
923
ERROR HY000: The definition of table 'v1' prevents operation UPDATE on table 't2'.
924
update v2,t2 set v2.col1 = (select max(col1) from t1) where v2.col1 = t2.col1;
925
ERROR HY000: The definition of table 'v2' prevents operation UPDATE on table 'v2'.
926 927 928
update t1,t2 set t1.col1 = (select max(col1) from t1) where t1.col1 = t2.col1;
ERROR HY000: You can't specify target table 't1' for update in FROM clause
update v1,t2 set v1.col1 = (select max(col1) from t1) where v1.col1 = t2.col1;
929
ERROR HY000: The definition of table 'v1' prevents operation UPDATE on table 'v1'.
930 931 932 933 934 935 936 937 938
update t2,v2 set v2.col1 = (select max(col1) from t1) where v2.col1 = t2.col1;
ERROR HY000: You can't specify target table 't2' for update in FROM clause
update t2,t1 set t1.col1 = (select max(col1) from t1) where t1.col1 = t2.col1;
ERROR HY000: You can't specify target table 't2' for update in FROM clause
update t2,v1 set v1.col1 = (select max(col1) from t1) where v1.col1 = t2.col1;
ERROR HY000: You can't specify target table 't2' for update in FROM clause
update v2,t2 set v2.col1 = (select max(col1) from v2) where v2.col1 = t2.col1;
ERROR HY000: You can't specify target table 'v2' for update in FROM clause
update t1,t2 set t1.col1 = (select max(col1) from v2) where t1.col1 = t2.col1;
939
ERROR HY000: The definition of table 'v2' prevents operation UPDATE on table 't1'.
940
update v1,t2 set v1.col1 = (select max(col1) from v2) where v1.col1 = t2.col1;
941
ERROR HY000: The definition of table 'v2' prevents operation UPDATE on table 'v1'.
942
update t2,v2 set v2.col1 = (select max(col1) from v2) where v2.col1 = t2.col1;
943
ERROR HY000: The definition of table 'v2' prevents operation UPDATE on table 't2'.
944
update t2,t1 set t1.col1 = (select max(col1) from v2) where t1.col1 = t2.col1;
945
ERROR HY000: The definition of table 'v2' prevents operation UPDATE on table 't2'.
946
update t2,v1 set v1.col1 = (select max(col1) from v2) where v1.col1 = t2.col1;
947
ERROR HY000: The definition of table 'v2' prevents operation UPDATE on table 't2'.
948
update v3 set v3.col1 = (select max(col1) from v1);
949
ERROR HY000: The definition of table 'v1' prevents operation UPDATE on table 'v3'.
950
update v3 set v3.col1 = (select max(col1) from t1);
951
ERROR HY000: The definition of table 'v3' prevents operation UPDATE on table 'v3'.
952
update v3 set v3.col1 = (select max(col1) from v2);
953
ERROR HY000: The definition of table 'v2' prevents operation UPDATE on table 'v3'.
954 955
update v3 set v3.col1 = (select max(col1) from v3);
ERROR HY000: You can't specify target table 'v3' for update in FROM clause
956
delete from v2 where col1 = (select max(col1) from v1);
957
ERROR HY000: The definition of table 'v1' prevents operation DELETE on table 'v2'.
958
delete from v2 where col1 = (select max(col1) from t1);
959
ERROR HY000: The definition of table 'v2' prevents operation DELETE on table 'v2'.
960 961 962
delete from v2 where col1 = (select max(col1) from v2);
ERROR HY000: You can't specify target table 'v2' for update in FROM clause
delete v2 from v2,t2 where (select max(col1) from v1) > 0 and v2.col1 = t2.col1;
963
ERROR HY000: The definition of table 'v1' prevents operation DELETE on table 'v2'.
964
delete t1 from t1,t2 where (select max(col1) from v1) > 0 and t1.col1 = t2.col1;
965
ERROR HY000: The definition of table 'v1' prevents operation DELETE on table 't1'.
966 967 968
delete v1 from v1,t2 where (select max(col1) from v1) > 0 and v1.col1 = t2.col1;
ERROR HY000: You can't specify target table 'v1' for update in FROM clause
delete v2 from v2,t2 where (select max(col1) from t1) > 0 and v2.col1 = t2.col1;
969
ERROR HY000: The definition of table 'v2' prevents operation DELETE on table 'v2'.
970 971 972
delete t1 from t1,t2 where (select max(col1) from t1) > 0 and t1.col1 = t2.col1;
ERROR HY000: You can't specify target table 't1' for update in FROM clause
delete v1 from v1,t2 where (select max(col1) from t1) > 0 and v1.col1 = t2.col1;
973
ERROR HY000: The definition of table 'v1' prevents operation DELETE on table 'v1'.
974 975 976
delete v2 from v2,t2 where (select max(col1) from v2) > 0 and v2.col1 = t2.col1;
ERROR HY000: You can't specify target table 'v2' for update in FROM clause
delete t1 from t1,t2 where (select max(col1) from v2) > 0 and t1.col1 = t2.col1;
977
ERROR HY000: The definition of table 'v2' prevents operation DELETE on table 't1'.
978
delete v1 from v1,t2 where (select max(col1) from v2) > 0 and v1.col1 = t2.col1;
979
ERROR HY000: The definition of table 'v2' prevents operation DELETE on table 'v1'.
980
insert into v2 values ((select max(col1) from v1));
981
ERROR HY000: The definition of table 'v1' prevents operation INSERT on table 'v2'.
982
insert into t1 values ((select max(col1) from v1));
983
ERROR HY000: The definition of table 'v1' prevents operation INSERT on table 't1'.
984
insert into v2 values ((select max(col1) from v1));
985
ERROR HY000: The definition of table 'v1' prevents operation INSERT on table 'v2'.
986
insert into v2 values ((select max(col1) from t1));
987
ERROR HY000: The definition of table 'v2' prevents operation INSERT on table 'v2'.
988 989 990
insert into t1 values ((select max(col1) from t1));
ERROR HY000: You can't specify target table 't1' for update in FROM clause
insert into v2 values ((select max(col1) from t1));
991
ERROR HY000: The definition of table 'v2' prevents operation INSERT on table 'v2'.
992 993 994
insert into v2 values ((select max(col1) from v2));
ERROR HY000: You can't specify target table 'v2' for update in FROM clause
insert into t1 values ((select max(col1) from v2));
995
ERROR HY000: The definition of table 'v2' prevents operation INSERT on table 't1'.
996 997 998
insert into v2 values ((select max(col1) from v2));
ERROR HY000: You can't specify target table 'v2' for update in FROM clause
insert into v3 (col1) values ((select max(col1) from v1));
999
ERROR HY000: The definition of table 'v1' prevents operation INSERT on table 'v3'.
1000
insert into v3 (col1) values ((select max(col1) from t1));
1001
ERROR HY000: The definition of table 'v3' prevents operation INSERT on table 'v3'.
1002
insert into v3 (col1) values ((select max(col1) from v2));
1003
ERROR HY000: The definition of table 'v2' prevents operation INSERT on table 'v3'.
1004
insert into v3 (col1) values ((select CONVERT_TZ('20050101000000','UTC','MET') from v2));
1005
ERROR HY000: The definition of table 'v2' prevents operation INSERT on table 'v3'.
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
insert into v3 (col1) values ((select CONVERT_TZ('20050101000000','UTC','MET') from t2));
insert into mysql.time_zone values ('', (select CONVERT_TZ('20050101000000','UTC','MET') from t2));
ERROR 23000: Column 'Use_leap_seconds' cannot be null
create algorithm=temptable view v4 as select * from t1;
insert into t1 values (1),(2),(3);
insert into t1 (col1) values ((select max(col1) from v4));
select * from t1;
col1
NULL
1
2
3
3
drop view v4,v3,v2,v1;
1020
drop table t1,t2;
1021 1022 1023 1024 1025 1026
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;
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
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
1049
drop table t1, t2;
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
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
1064 1065 1066 1067 1068
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
unknown's avatar
unknown committed
1069
d
1070 1071 1072 1073
set @arg='e';
execute stmt1 using @arg;
select * from v1;
s1
unknown's avatar
unknown committed
1074
e
1075
deallocate prepare stmt1;
1076 1077
drop view v1;
drop table t1;
unknown's avatar
unknown committed
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
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
1088 1089 1090 1091
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
1092
ERROR HY000: CHECK OPTION failed 'test.v1'
unknown's avatar
unknown committed
1093 1094
insert ignore into v1 values (2),(3),(0);
Warnings:
unknown's avatar
unknown committed
1095 1096
Error	1369	CHECK OPTION failed 'test.v1'
Error	1369	CHECK OPTION failed 'test.v1'
unknown's avatar
unknown committed
1097 1098 1099 1100 1101 1102 1103
select * from t1;
a
1
0
delete from t1;
insert into v1 SELECT 1;
insert into v1 SELECT 3;
unknown's avatar
unknown committed
1104
ERROR HY000: CHECK OPTION failed 'test.v1'
unknown's avatar
unknown committed
1105 1106 1107 1108
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
1109 1110
Error	1369	CHECK OPTION failed 'test.v1'
Error	1369	CHECK OPTION failed 'test.v1'
unknown's avatar
unknown committed
1111 1112 1113 1114 1115 1116
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
1117
ERROR HY000: CHECK OPTION failed 'test.v1'
unknown's avatar
unknown committed
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
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
1132
Error	1369	CHECK OPTION failed 'test.v1'
unknown's avatar
unknown committed
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
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
1146
ERROR HY000: CHECK OPTION failed 'test.v2'
unknown's avatar
unknown committed
1147
insert into v3 values (0);
unknown's avatar
unknown committed
1148
ERROR HY000: CHECK OPTION failed 'test.v3'
unknown's avatar
unknown committed
1149 1150
insert into v2 values (2);
insert into v3 values (2);
unknown's avatar
unknown committed
1151
ERROR HY000: CHECK OPTION failed 'test.v3'
unknown's avatar
unknown committed
1152 1153 1154 1155 1156 1157 1158
select * from t1;
a
1
1
2
drop view v3,v2,v1;
drop table t1;
1159 1160 1161 1162
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
1163
ERROR HY000: CHECK OPTION failed 'test.v1'
1164
insert ignore into v1 values (1) on duplicate key update a=2;
unknown's avatar
unknown committed
1165 1166
Warnings:
Error	1369	CHECK OPTION failed 'test.v1'
1167 1168 1169 1170 1171
select * from t1;
a
1
drop view v1;
drop table t1;
1172 1173 1174 1175 1176 1177
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;
unknown's avatar
unknown committed
1178
ERROR 42S02: Table 'test.v1' doesn't exist
1179 1180 1181
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;
unknown's avatar
unknown committed
1182
ERROR 42S02: Table 'test.v1' doesn't exist
1183 1184 1185 1186 1187 1188
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
unknown's avatar
unknown committed
1189
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`a` AS `a` from `t1`
1190 1191 1192
alter algorithm=undefined view v1 as select * from t1 with check option;
show create view v1;
View	Create View
unknown's avatar
unknown committed
1193
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`a` AS `a` from `t1` WITH CASCADED CHECK OPTION
1194 1195 1196
alter algorithm=merge view v1 as select * from t1 with cascaded check option;
show create view v1;
View	Create View
unknown's avatar
unknown committed
1197
v1	CREATE ALGORITHM=MERGE DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`a` AS `a` from `t1` WITH CASCADED CHECK OPTION
1198 1199 1200
alter algorithm=temptable view v1 as select * from t1;
show create view v1;
View	Create View
unknown's avatar
unknown committed
1201
v1	CREATE ALGORITHM=TEMPTABLE DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`a` AS `a` from `t1`
1202 1203
drop view v1;
drop table t1;
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
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;
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
create table t1 (t time);
create view v1 as select substring_index(t,':',2) as t from t1;
insert into t1 (t) values ('12:24:10');
select substring_index(t,':',2) from t1;
substring_index(t,':',2)
12:24
select substring_index(t,':',2) from v1;
substring_index(t,':',2)
12:24
drop view v1;
drop table t1;
1254 1255 1256 1257 1258 1259 1260
create table t1 (s1 tinyint);
create view v1 as select * from t1 where s1 <> 0 with local check option;
create view v2 as select * from v1 with cascaded check option;
insert into v2 values (0);
ERROR HY000: CHECK OPTION failed 'test.v2'
drop view v2, v1;
drop table t1;
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
create table t1 (s1 int);
create view v1 as select * from t1 where s1 < 5 with check option;
insert ignore into v1 values (6);
ERROR HY000: CHECK OPTION failed 'test.v1'
insert ignore into v1 values (6),(3);
Warnings:
Error	1369	CHECK OPTION failed 'test.v1'
select * from t1;
s1
3
drop view v1;
drop table t1;
1273 1274 1275 1276 1277 1278 1279 1280 1281
create table t1 (s1 tinyint);
create trigger t1_bi before insert on t1 for each row set new.s1 = 500;
create view v1 as select * from t1 where s1 <> 127 with check option;
insert into v1 values (0);
ERROR HY000: CHECK OPTION failed 'test.v1'
select * from v1;
s1
select * from t1;
s1
1282
drop trigger t1_bi;
1283 1284
drop view v1;
drop table t1;
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
create table t1 (s1 tinyint);
create view v1 as select * from t1 where s1 <> 0;
create view v2 as select * from v1 where s1 <> 1 with cascaded check option;
insert into v2 values (0);
ERROR HY000: CHECK OPTION failed 'test.v2'
select * from v2;
s1
select * from t1;
s1
drop view v2, v1;
drop table t1;
1296 1297
create table t1 (a int, b char(10));
create view v1 as select * from t1 where a != 0 with check option;
1298
load data infile '../std_data_ln/loaddata3.dat' into table v1 fields terminated by '' enclosed by '' ignore 1 lines;
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
ERROR HY000: CHECK OPTION failed 'test.v1'
select * from t1;
a	b
1	row 1
2	row 2
select * from v1;
a	b
1	row 1
2	row 2
delete from t1;
1309
load data infile '../std_data_ln/loaddata3.dat' ignore into table v1 fields terminated by '' enclosed by '' ignore 1 lines;
1310
Warnings:
1311
Warning	1366	Incorrect integer value: 'error      ' for column 'a' at row 3
1312
Error	1369	CHECK OPTION failed 'test.v1'
1313
Warning	1366	Incorrect integer value: 'wrong end  ' for column 'a' at row 4
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
Error	1369	CHECK OPTION failed 'test.v1'
select * from t1;
a	b
1	row 1
2	row 2
3	row 3
select * from v1;
a	b
1	row 1
2	row 2
3	row 3
drop view v1;
drop table t1;
create table t1 (a text, b text);
create view v1 as select * from t1 where a <> 'Field A' with check option;
1329
load data infile '../std_data_ln/loaddata2.dat' into table v1 fields terminated by ',' enclosed by '''';
1330 1331 1332 1333 1334 1335
ERROR HY000: CHECK OPTION failed 'test.v1'
select concat('|',a,'|'), concat('|',b,'|') from t1;
concat('|',a,'|')	concat('|',b,'|')
select concat('|',a,'|'), concat('|',b,'|') from v1;
concat('|',a,'|')	concat('|',b,'|')
delete from t1;
1336
load data infile '../std_data_ln/loaddata2.dat' ignore into table v1 fields terminated by ',' enclosed by '''';
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
Warnings:
Error	1369	CHECK OPTION failed 'test.v1'
Warning	1261	Row 2 doesn't contain data for all columns
select concat('|',a,'|'), concat('|',b,'|') from t1;
concat('|',a,'|')	concat('|',b,'|')
|Field 1|	|Field 2' 
Field 3,'Field 4|
|Field 5' ,'Field 6|	NULL
|Field 6|	| 'Field 7'|
select concat('|',a,'|'), concat('|',b,'|') from v1;
concat('|',a,'|')	concat('|',b,'|')
|Field 1|	|Field 2' 
Field 3,'Field 4|
|Field 5' ,'Field 6|	NULL
|Field 6|	| 'Field 7'|
drop view v1;
drop table t1;
1354 1355 1356
create table t1 (s1 smallint);
create view v1 as select * from t1 where 20 < (select (s1) from t1);
insert into v1 values (30);
1357
ERROR HY000: The target table v1 of the INSERT is not insertable-into
1358 1359 1360
create view v2 as select * from t1;
create view v3 as select * from t1 where 20 < (select (s1) from v2);
insert into v3 values (30);
1361
ERROR HY000: The target table v3 of the INSERT is not insertable-into
1362 1363
create view v4 as select * from v2 where 20 < (select (s1) from t1);
insert into v4 values (30);
1364
ERROR HY000: The target table v4 of the INSERT is not insertable-into
1365 1366
drop view v4, v3, v2, v1;
drop table t1;
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
create table t1 (a int);
create view v1 as select * from t1;
check table t1,v1;
Table	Op	Msg_type	Msg_text
test.t1	check	status	OK
test.v1	check	status	OK
check table v1,t1;
Table	Op	Msg_type	Msg_text
test.v1	check	status	OK
test.t1	check	status	OK
drop table t1;
check table v1;
Table	Op	Msg_type	Msg_text
1380
test.v1	check	error	View 'test.v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
1381
drop view v1;
unknown's avatar
unknown committed
1382
create table t1 (a int);
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
create table t2 (a int);
create table t3 (a int);
insert into t1 values (1), (2), (3);
insert into t2 values (1), (3);
insert into t3 values (1), (2), (4);
create view v3 (a,b) as select t1.a as a, t2.a as b from t1 left join t2 on (t1.a=t2.a);
select * from t3 left join v3 on (t3.a = v3.a);
a	a	b
1	1	1
2	2	NULL
4	NULL	NULL
explain extended select * from t3 left join v3 on (t3.a = v3.a);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1396 1397 1398
1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	3	
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	3	
1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	2	
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
Warnings:
Note	1003	select `test`.`t3`.`a` AS `a`,`test`.`t1`.`a` AS `a`,`test`.`t2`.`a` AS `b` from `test`.`t3` left join (`test`.`t1` left join `test`.`t2` on((`test`.`t1`.`a` = `test`.`t2`.`a`))) on((`test`.`t3`.`a` = `test`.`t1`.`a`)) where 1
create view v1 (a) as select a from t1;
create view v2 (a) as select a from t2;
create view v4 (a,b) as select v1.a as a, v2.a as b from v1 left join v2 on (v1.a=v2.a);
select * from t3 left join v4 on (t3.a = v4.a);
a	a	b
1	1	1
2	2	NULL
4	NULL	NULL
explain extended select * from t3 left join v4 on (t3.a = v4.a);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1411 1412 1413
1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	3	
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	3	
1	SIMPLE	t2	ALL	NULL	NULL	NULL	NULL	2	
1414
Warnings:
unknown's avatar
unknown committed
1415
Note	1003	select `test`.`t3`.`a` AS `a`,`test`.`t1`.`a` AS `a`,`test`.`t2`.`a` AS `b` from `test`.`t3` left join (`test`.`t1` left join (`test`.`t2`) on((`test`.`t1`.`a` = `test`.`t2`.`a`))) on((`test`.`t3`.`a` = `test`.`t1`.`a`)) where 1
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
prepare stmt1 from "select * from t3 left join v4 on (t3.a = v4.a);";
execute stmt1;
a	a	b
1	1	1
2	2	NULL
4	NULL	NULL
execute stmt1;
a	a	b
1	1	1
2	2	NULL
4	NULL	NULL
deallocate prepare stmt1;
drop view v4,v3,v2,v1;
drop tables t1,t2,t3;
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
create table t1 (a int, primary key (a), b int);
create table t2 (a int, primary key (a));
insert into t1 values (1,100), (2,200);
insert into t2 values (1), (3);
create view v3 (a,b) as select t1.a as a, t2.a as b from t1, t2;
update v3 set a= 10 where a=1;
select * from t1;
a	b
10	100
2	200
select * from t2;
a
1
3
create view v2 (a,b) as select t1.b as a, t2.a as b from t1, t2;
unknown's avatar
unknown committed
1445 1446 1447 1448
set updatable_views_with_limit=NO;
update v2 set a= 10 where a=200 limit 1;
ERROR HY000: The target table t1 of the UPDATE is not updatable
set updatable_views_with_limit=DEFAULT;
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 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
select * from v3;
a	b
2	1
10	1
2	3
10	3
select * from v2;
a	b
100	1
200	1
100	3
200	3
set @a= 10;
set @b= 100;
prepare stmt1 from "update v3 set a= ? where a=?";
execute stmt1 using @a,@b;
select * from v3;
a	b
2	1
10	1
2	3
10	3
set @a= 300;
set @b= 10;
execute stmt1 using @a,@b;
select * from v3;
a	b
2	1
300	1
2	3
300	3
deallocate prepare stmt1;
drop view v3,v2;
drop tables t1,t2;
create table t1 (a int, primary key (a), b int);
create table t2 (a int, primary key (a), b int);
insert into t2 values (1000, 2000);
create view v3 (a,b) as select t1.a as a, t2.a as b from t1, t2;
insert into v3 values (1,2);
ERROR HY000: Can not insert into join view 'test.v3' without fields list
insert into v3 select * from t2;
ERROR HY000: Can not insert into join view 'test.v3' without fields list
insert into v3(a,b) values (1,2);
ERROR HY000: Can not modify more than one base table through a join view 'test.v3'
insert into v3(a,b) select * from t2;
ERROR HY000: Can not modify more than one base table through a join view 'test.v3'
insert into v3(a) values (1);
insert into v3(b) values (10);
insert into v3(a) select a from t2;
insert into v3(b) select b from t2;
Warnings:
1500
Warning	1263	Column was set to data type implicit default; NULL supplied for NOT NULL column 'a' at row 2
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
insert into v3(a) values (1) on duplicate key update a=a+10000+VALUES(a);
select * from t1;
a	b
10002	NULL
10	NULL
1000	NULL
select * from t2;
a	b
1000	2000
10	NULL
2000	NULL
0	NULL
delete from v3;
ERROR HY000: Can not delete from join view 'test.v3'
delete v3,t1 from v3,t1;
ERROR HY000: Can not delete from join view 'test.v3'
1517 1518
delete t1,v3 from t1,v3;
ERROR HY000: Can not delete from join view 'test.v3'
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
delete from t1;
prepare stmt1 from "insert into v3(a) values (?);";
set @a= 100;
execute stmt1 using @a;
set @a= 300;
execute stmt1 using @a;
deallocate prepare stmt1;
prepare stmt1 from "insert into v3(a) select ?;";
set @a= 101;
execute stmt1 using @a;
set @a= 301;
execute stmt1 using @a;
deallocate prepare stmt1;
select * from v3;
a	b
100	1000
101	1000
300	1000
301	1000
100	10
101	10
300	10
301	10
100	2000
101	2000
300	2000
301	2000
100	0
101	0
300	0
301	0
unknown's avatar
unknown committed
1550
drop view v3;
1551
drop tables t1,t2;
1552 1553 1554 1555 1556 1557
create table t1(f1 int);
create view v1 as select f1 from t1;
select * from v1 where F1 = 1;
f1
drop view v1;
drop table t1;
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
create table t1(c1 int);
create table t2(c2 int);
insert into t1 values (1),(2),(3);
insert into t2 values (1);
SELECT c1 FROM t1 WHERE c1 IN (SELECT c2 FROM t2);
c1
1
SELECT c1 FROM t1 WHERE EXISTS (SELECT c2 FROM t2 WHERE c2 = c1);
c1
1
create view v1 as SELECT c1 FROM t1 WHERE c1 IN (SELECT c2 FROM t2);
create view v2 as SELECT c1 FROM t1 WHERE EXISTS (SELECT c2 FROM t2 WHERE c2 = c1);
select * from v1;
c1
1
select * from v2;
c1
1
1576 1577 1578
select * from (select c1 from v2) X;
c1
1
1579 1580
drop view v2, v1;
drop table t1, t2;
1581 1582 1583 1584 1585 1586 1587 1588
CREATE TABLE t1 (C1 INT, C2 INT);
CREATE TABLE t2 (C2 INT);
CREATE VIEW v1 AS SELECT C2 FROM t2;
CREATE VIEW v2 AS SELECT C1 FROM t1 LEFT OUTER JOIN v1 USING (C2);
SELECT * FROM v2;
C1
drop view v2, v1;
drop table t1, t2;
1589 1590 1591 1592 1593 1594 1595 1596 1597
create table t1 (col1 char(5),col2 int,col3 int);
insert into t1 values ('one',10,25), ('two',10,50), ('two',10,50), ('one',20,25), ('one',30,25);
create view v1 as select * from t1;
select col1,group_concat(col2,col3) from t1 group by col1;
col1	group_concat(col2,col3)
one	1025,2025,3025
two	1050,1050
select col1,group_concat(col2,col3) from v1 group by col1;
col1	group_concat(col2,col3)
unknown's avatar
unknown committed
1598
one	1025,2025,3025
1599 1600 1601
two	1050,1050
drop view v1;
drop table t1;
unknown's avatar
unknown committed
1602 1603 1604 1605 1606 1607 1608 1609
create table t1 (s1 int, s2 char);
create view v1 as select s1, s2 from t1;
select s2 from v1 vq1 where 2 = (select count(*) from v1 vq2 having vq1.s2 = vq2.s2);
ERROR 42S22: Unknown column 'vq2.s2' in 'having clause'
select s2 from v1 vq1 where 2 = (select count(*) aa from v1 vq2 having vq1.s2 = aa);
s2
drop view v1;
drop table t1;
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
CREATE TABLE t1 (a1 int);
CREATE TABLE t2 (a2 int);
INSERT INTO t1 VALUES (1), (2), (3), (4);
INSERT INTO t2 VALUES (1), (2), (3);
CREATE VIEW v1(a,b) AS SELECT a1,a2 FROM t1 JOIN t2 ON a1=a2 WHERE a1>1;
SELECT * FROM v1;
a	b
2	2
3	3
CREATE TABLE t3 SELECT * FROM v1;
SELECT * FROM t3;
a	b
2	2
3	3
DROP VIEW v1;
DROP TABLE t1,t2,t3;
create table t1 (a int);
create table t2 like t1;
create table t3 like t1;
create view v1 as select t1.a x, t2.a y from t1 join t2 where t1.a=t2.a;
insert into t3 select x from v1;
insert into t2 select x from v1;
drop view v1;
drop table t1,t2,t3;
1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
CREATE TABLE t1 (col1 int PRIMARY KEY, col2 varchar(10));
INSERT INTO t1 VALUES(1,'trudy');
INSERT INTO t1 VALUES(2,'peter');
INSERT INTO t1 VALUES(3,'sanja');
INSERT INTO t1 VALUES(4,'monty');
INSERT INTO t1 VALUES(5,'david');
INSERT INTO t1 VALUES(6,'kent');
INSERT INTO t1 VALUES(7,'carsten');
INSERT INTO t1 VALUES(8,'ranger');
INSERT INTO t1 VALUES(10,'matt');
CREATE TABLE t2 (col1 int, col2 int, col3 char(1));
INSERT INTO t2 VALUES (1,1,'y');
INSERT INTO t2 VALUES (1,2,'y');
INSERT INTO t2 VALUES (2,1,'n');
INSERT INTO t2 VALUES (3,1,'n');
INSERT INTO t2 VALUES (4,1,'y');
INSERT INTO t2 VALUES (4,2,'n');
INSERT INTO t2 VALUES (4,3,'n');
INSERT INTO t2 VALUES (6,1,'n');
INSERT INTO t2 VALUES (8,1,'y');
CREATE VIEW v1 AS SELECT * FROM t1;
SELECT a.col1,a.col2,b.col2,b.col3 
FROM t1 a LEFT JOIN t2 b ON a.col1=b.col1
WHERE b.col2 IS NULL OR 
b.col2=(SELECT MAX(col2) FROM t2 b WHERE b.col1=a.col1);
col1	col2	col2	col3
1	trudy	2	y
2	peter	1	n
3	sanja	1	n
4	monty	3	n
5	david	NULL	NULL
6	kent	1	n
7	carsten	NULL	NULL
8	ranger	1	y
10	matt	NULL	NULL
SELECT a.col1,a.col2,b.col2,b.col3 
FROM v1 a LEFT JOIN t2 b ON a.col1=b.col1
WHERE b.col2 IS NULL OR 
b.col2=(SELECT MAX(col2) FROM t2 b WHERE b.col1=a.col1);
col1	col2	col2	col3
1	trudy	2	y
2	peter	1	n
3	sanja	1	n
4	monty	3	n
5	david	NULL	NULL
6	kent	1	n
7	carsten	NULL	NULL
8	ranger	1	y
10	matt	NULL	NULL
CREATE VIEW v2 AS SELECT * FROM t2;
SELECT a.col1,a.col2,b.col2,b.col3
FROM v2 b RIGHT JOIN v1 a ON a.col1=b.col1
WHERE b.col2 IS NULL OR
b.col2=(SELECT MAX(col2) FROM v2 b WHERE b.col1=a.col1);
col1	col2	col2	col3
1	trudy	2	y
2	peter	1	n
3	sanja	1	n
4	monty	3	n
5	david	NULL	NULL
6	kent	1	n
7	carsten	NULL	NULL
8	ranger	1	y
10	matt	NULL	NULL
SELECT a.col1,a.col2,b.col2,b.col3
FROM v2 b RIGHT JOIN v1 a ON a.col1=b.col1
WHERE a.col1 IN (1,5,9) AND
(b.col2 IS NULL OR
b.col2=(SELECT MAX(col2) FROM v2 b WHERE b.col1=a.col1));
col1	col2	col2	col3
1	trudy	2	y
5	david	NULL	NULL
CREATE VIEW v3 AS SELECT * FROM t1 WHERE col1 IN (1,5,9);
SELECT a.col1,a.col2,b.col2,b.col3
FROM v2 b RIGHT JOIN v3 a ON a.col1=b.col1
WHERE b.col2 IS NULL OR
b.col2=(SELECT MAX(col2) FROM v2 b WHERE b.col1=a.col1);
col1	col2	col2	col3
1	trudy	2	y
5	david	NULL	NULL
DROP VIEW v1,v2,v3;
DROP TABLE t1,t2;
1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733
create table t1 as select 1 A union select 2 union select 3;
create table t2 as select * from t1;
create view v1 as select * from t1 where a in (select * from t2);
select * from v1 A, v1 B where A.a = B.a;
A	A
1	1
2	2
3	3
create table t3 as select a a,a b from t2;
create view v2 as select * from t3 where 
a in (select * from t1) or b in (select * from t2);
select * from v2 A, v2 B where A.a = B.b;
a	b	a	b
1	1	1	1
2	2	2	2
3	3	3	3
drop view v1, v2;
drop table t1, t2, t3;
unknown's avatar
unknown committed
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748
CREATE TABLE t1 (a int);
CREATE TABLE t2 (b int);
INSERT INTO t1 VALUES (1), (2), (3), (4);
INSERT INTO t2 VALUES (4), (2);
CREATE VIEW v1 AS SELECT * FROM t1,t2 WHERE t1.a=t2.b;
SELECT * FROM v1;
a	b
2	2
4	4
CREATE VIEW v2 AS SELECT * FROM v1;
SELECT * FROM v2;
a	b
2	2
4	4
DROP VIEW v2,v1;
unknown's avatar
unknown committed
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
DROP TABLE t1, t2;
create table t1 (a int);
create view v1 as select sum(a) from t1 group by a;
create procedure p1()
begin
select * from v1;
end//
call p1();
sum(a)
call p1();
sum(a)
drop procedure p1;
drop view v1;
drop table t1;
1763 1764 1765 1766 1767 1768
CREATE TABLE t1(a char(2) primary key, b char(2));
CREATE TABLE t2(a char(2), b char(2), index i(a));
INSERT INTO t1 VALUES ('a','1'), ('b','2');
INSERT INTO t2 VALUES ('a','5'), ('a','6'), ('b','5'), ('b','6');
CREATE VIEW v1 AS
SELECT t1.b as c, t2.b as d FROM t1,t2 WHERE t1.a=t2.a;
unknown's avatar
unknown committed
1769
SELECT d, c FROM v1 ORDER BY d,c;
1770 1771 1772 1773 1774 1775 1776
d	c
5	1
5	2
6	1
6	2
DROP VIEW v1;
DROP TABLE t1, t2;
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
create table t1 (s1 int);
create view  v1 as select sum(distinct s1) from t1;
select * from v1;
sum(distinct s1)
NULL
drop view v1;
create view  v1 as select avg(distinct s1) from t1;
select * from v1;
avg(distinct s1)
NULL
drop view v1;
drop table t1;
create view v1 as select cast(1 as decimal);
select * from v1;
cast(1 as decimal)
1792
1
1793
drop view v1;
1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807
create table t1(f1 int);
create table t2(f2 int);
insert into t1 values(1),(2),(3);
insert into t2 values(1),(2),(3);
create view v1 as select * from t1,t2 where f1=f2;
create table t3 (f1 int, f2 int);
insert into t3 select * from v1 order by 1;
select * from t3;
f1	f2
1	1
2	2
3	3
drop view v1;
drop table t1,t2,t3;
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
create view v1 as select '\\','\\shazam';
select * from v1;
\	\shazam
\	\shazam
drop view v1;
create view v1 as select '\'','\shazam';
select * from v1;
'	shazam
'	shazam
drop view v1;
create view v1 as select 'k','K';
select * from v1;
k	My_exp_K
k	K
drop view v1;
create table t1 (s1 int);
create view v1 as select s1, 's1' from t1;
select * from v1;
s1	My_exp_s1
drop view v1;
create view v1 as select 's1', s1 from t1;
select * from v1;
My_exp_s1	s1
drop view v1;
create view v1 as select 's1', s1, 1 as My_exp_s1 from t1;
select * from v1;
My_exp_1_s1	s1	My_exp_s1
drop view v1;
create view v1 as select 1 as My_exp_s1, 's1', s1  from t1;
select * from v1;
My_exp_s1	My_exp_1_s1	s1
drop view v1;
create view v1 as select 1 as s1, 's1', 's1' from t1;
select * from v1;
s1	My_exp_s1	My_exp_1_s1
drop view v1;
create view v1 as select 's1', 's1', 1 as s1 from t1;
select * from v1;
My_exp_1_s1	My_exp_s1	s1
drop view v1;
create view v1 as select s1, 's1', 's1' from t1;
select * from v1;
s1	My_exp_s1	My_exp_1_s1
drop view v1;
create view v1 as select 's1', 's1', s1 from t1;
select * from v1;
My_exp_1_s1	My_exp_s1	s1
drop view v1;
create view v1 as select 1 as s1, 's1', s1 from t1;
ERROR 42S21: Duplicate column name 's1'
create view v1 as select 's1', s1, 1 as s1 from t1;
ERROR 42S21: Duplicate column name 's1'
drop table t1;
create view v1(k, K) as select 1,2;
ERROR 42S21: Duplicate column name 'K'
1863 1864 1865 1866 1867
create view v1 as SELECT TIME_FORMAT(SEC_TO_TIME(3600),'%H:%i') as t;
select * from v1;
t
01:00
drop view v1;
1868 1869 1870 1871 1872
create table t1 (a timestamp default now());
create table t2 (b timestamp default now());
create view v1 as select a,b,t1.a < now() from t1,t2 where t1.a < now();
SHOW CREATE VIEW v1;
View	Create View
1873
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`a` AS `a`,`t2`.`b` AS `b`,(`t1`.`a` < now()) AS `t1.a < now()` from (`t1` join `t2`) where (`t1`.`a` < now())
1874 1875 1876 1877 1878 1879
drop view v1;
drop table t1, t2;
CREATE TABLE t1 ( a varchar(50) );
CREATE VIEW v1 AS SELECT * FROM t1 WHERE a = CURRENT_USER();
SHOW CREATE VIEW v1;
View	Create View
1880
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`a` AS `a` from `t1` where (`t1`.`a` = current_user())
1881 1882 1883 1884
DROP VIEW v1;
CREATE VIEW v1 AS SELECT * FROM t1 WHERE a = VERSION();
SHOW CREATE VIEW v1;
View	Create View
unknown's avatar
unknown committed
1885
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`a` AS `a` from `t1` where (`t1`.`a` = version())
1886 1887 1888 1889
DROP VIEW v1;
CREATE VIEW v1 AS SELECT * FROM t1 WHERE a = DATABASE();
SHOW CREATE VIEW v1;
View	Create View
1890
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`a` AS `a` from `t1` where (`t1`.`a` = database())
1891 1892
DROP VIEW v1;
DROP TABLE t1;
1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
CREATE TABLE t1 (col1 time);
CREATE TABLE t2 (col1 time);
CREATE VIEW v1 AS SELECT CONVERT_TZ(col1,'GMT','MET') FROM t1;
CREATE VIEW v2 AS SELECT CONVERT_TZ(col1,'GMT','MET') FROM t2;
CREATE VIEW v3 AS SELECT CONVERT_TZ(col1,'GMT','MET') FROM t1;
CREATE VIEW v4 AS SELECT CONVERT_TZ(col1,'GMT','MET') FROM t2;
CREATE VIEW v5 AS SELECT CONVERT_TZ(col1,'GMT','MET') FROM t1;
CREATE VIEW v6 AS SELECT CONVERT_TZ(col1,'GMT','MET') FROM t2;
DROP TABLE t1;
CHECK TABLE v1, v2, v3, v4, v5, v6;
Table	Op	Msg_type	Msg_text
1904
test.v1	check	error	View 'test.v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
1905
test.v2	check	status	OK
1906
test.v3	check	error	View 'test.v3' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
1907
test.v4	check	status	OK
1908
test.v5	check	error	View 'test.v5' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
1909 1910 1911
test.v6	check	status	OK
drop view v1, v2, v3, v4, v5, v6;
drop table t2;
unknown's avatar
unknown committed
1912 1913
drop function if exists f1;
drop function if exists f2;
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
CREATE TABLE t1 (col1 time);
CREATE TABLE t2 (col1 time);
CREATE TABLE t3 (col1 time);
create function f1 () returns int return (select max(col1) from t1);
create function f2 () returns int return (select max(col1) from t2);
CREATE VIEW v1 AS SELECT f1() FROM t3;
CREATE VIEW v2 AS SELECT f2() FROM t3;
CREATE VIEW v3 AS SELECT f1() FROM t3;
CREATE VIEW v4 AS SELECT f2() FROM t3;
CREATE VIEW v5 AS SELECT f1() FROM t3;
CREATE VIEW v6 AS SELECT f2() FROM t3;
drop function f1;
CHECK TABLE v1, v2, v3, v4, v5, v6;
Table	Op	Msg_type	Msg_text
1928
test.v1	check	error	View 'test.v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
1929
test.v2	check	status	OK
1930
test.v3	check	error	View 'test.v3' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
1931
test.v4	check	status	OK
1932
test.v5	check	error	View 'test.v5' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
1933 1934 1935 1936 1937
test.v6	check	status	OK
create function f1 () returns int return (select max(col1) from t1);
DROP TABLE t1;
CHECK TABLE v1, v2, v3, v4, v5, v6;
Table	Op	Msg_type	Msg_text
1938
test.v1	check	status	OK
1939
test.v2	check	status	OK
1940
test.v3	check	status	OK
1941
test.v4	check	status	OK
1942
test.v5	check	status	OK
1943 1944 1945 1946 1947
test.v6	check	status	OK
drop function f1;
drop function f2;
drop view v1, v2, v3, v4, v5, v6;
drop table t2,t3;
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
create table t1 (f1 date);
insert into t1 values ('2005-01-01'),('2005-02-02');
create view v1 as select * from t1;
select * from v1 where f1='2005.02.02';
f1
2005-02-02
select * from v1 where '2005.02.02'=f1;
f1
2005-02-02
drop view v1;
drop table t1;
unknown's avatar
unknown committed
1959 1960 1961 1962 1963 1964 1965 1966
CREATE VIEW v1 AS SELECT ENCRYPT("dhgdhgd");
SELECT * FROM v1;
drop view v1;
CREATE VIEW v1 AS SELECT SUBSTRING_INDEX("dkjhgd:kjhdjh", ":", 1);
SELECT * FROM v1;
SUBSTRING_INDEX("dkjhgd:kjhdjh", ":", 1)
dkjhgd
drop view v1;
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976
create table t1 (f59 int, f60 int, f61 int);
insert into t1 values (19,41,32);
create view v1 as select f59, f60 from t1 where f59 in  
(select f59 from t1);
update v1 set f60=2345;
ERROR HY000: The target table v1 of the UPDATE is not updatable
update t1 set f60=(select max(f60) from v1);
ERROR HY000: The definition of table 'v1' prevents operation UPDATE on table 't1'.
drop view v1;
drop table t1;
1977 1978 1979 1980
create table t1 (s1 int);
create view v1 as select var_samp(s1) from t1;
show create view v1;
View	Create View
unknown's avatar
unknown committed
1981
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select var_samp(`t1`.`s1`) AS `var_samp(s1)` from `t1`
1982 1983
drop view v1;
drop table t1;
1984
set sql_mode='strict_all_tables';
1985
CREATE TABLE t1 (col1 INT NOT NULL, col2 INT NOT NULL);
1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
CREATE VIEW v1 (vcol1) AS SELECT col1 FROM t1;
CREATE VIEW v2 (vcol1) AS SELECT col1 FROM t1 WHERE col2 > 2;
INSERT INTO t1 (col1) VALUES(12);
ERROR HY000: Field 'col2' doesn't have a default value
INSERT INTO v1 (vcol1) VALUES(12);
ERROR HY000: Field of view 'test.v1' underlying table doesn't have a default value
INSERT INTO v2 (vcol1) VALUES(12);
ERROR HY000: Field of view 'test.v2' underlying table doesn't have a default value
set sql_mode=default;
drop view v2,v1;
drop table t1;
1997 1998 1999 2000 2001 2002 2003 2004
create table t1 (f1 int);
insert into t1 values (1);
create view v1 as select f1 from t1;
select f1 as alias from v1;
alias
1
drop view v1;
drop table t1;
unknown's avatar
unknown committed
2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018
CREATE TABLE t1 (s1 int, s2 int);
INSERT  INTO t1 VALUES (1,2);
CREATE VIEW v1 AS SELECT s2 AS s1, s1 AS s2 FROM t1;
SELECT * FROM v1;
s1	s2
2	1
CREATE PROCEDURE p1 () SELECT * FROM v1;
CALL p1();
s1	s2
2	1
ALTER VIEW v1 AS SELECT s1 AS s1, s2 AS s2 FROM t1;
CALL p1();
s1	s2
1	2
unknown's avatar
unknown committed
2019 2020 2021 2022 2023
DROP VIEW v1;
CREATE VIEW v1 AS SELECT s2 AS s1, s1 AS s2 FROM t1;
CALL p1();
s1	s2
2	1
unknown's avatar
unknown committed
2024 2025 2026
DROP PROCEDURE p1;
DROP VIEW v1;
DROP TABLE t1;
2027 2028 2029 2030 2031 2032 2033 2034
create table t1 (f1 int, f2 int);
create view v1 as select f1 as f3, f2 as f1 from t1;
insert into t1 values (1,3),(2,1),(3,2);
select * from v1 order by f1;
f3	f1
2	1
3	2
1	3
unknown's avatar
unknown committed
2035
drop view v1;
2036
drop table t1;
2037
CREATE TABLE t1 (f1 char);
unknown's avatar
unknown committed
2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050
INSERT INTO t1 VALUES ('A');
CREATE VIEW  v1 AS SELECT * FROM t1;
INSERT INTO t1 VALUES('B');
SELECT * FROM v1;
f1
A
B
SELECT * FROM t1;
f1
A
B
DROP VIEW v1;
DROP TABLE t1;
2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064
CREATE TABLE t1 ( bug_table_seq   INTEGER NOT NULL);
CREATE OR REPLACE VIEW v1 AS SELECT * from t1;
DROP PROCEDURE IF EXISTS p1;
Warnings:
Note	1305	PROCEDURE p1 does not exist
CREATE PROCEDURE p1 ( )
BEGIN
DO (SELECT  @next := IFNULL(max(bug_table_seq),0) + 1 FROM v1);
INSERT INTO t1 VALUES (1);
END //
CALL p1();
DROP PROCEDURE p1;
DROP VIEW v1;
DROP TABLE t1;
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
create table t1 (f1 tinyint(1), f2 char(1), f3 varchar(1), f4 geometry, f5 datetime);
create view v1 as select * from t1;
desc v1;
Field	Type	Null	Key	Default	Extra
f1	tinyint(1)	YES		NULL	
f2	char(1)	YES		NULL	
f3	varchar(1)	YES		NULL	
f4	geometry	YES		NULL	
f5	datetime	YES		NULL	
drop view v1;
drop table t1;
2076 2077 2078 2079 2080 2081 2082 2083
create table t1(f1 datetime);
insert into t1 values('2005.01.01 12:0:0');
create view v1 as select f1, subtime(f1, '1:1:1') as sb from t1;
select * from v1;
f1	sb
2005-01-01 12:00:00	2005-01-01 10:58:59
drop view v1;
drop table t1;
unknown's avatar
unknown committed
2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106
CREATE TABLE t1 (
aid int PRIMARY KEY,
fn varchar(20) NOT NULL,
ln varchar(20) NOT NULL
);
CREATE TABLE t2 (
aid int NOT NULL,
pid int NOT NULL
);
INSERT INTO t1 VALUES(1,'a','b'), (2,'c','d');
INSERT INTO t2 values (1,1), (2,1), (2,2);
CREATE VIEW v1 AS SELECT t1.*,t2.pid FROM t1,t2 WHERE t1.aid = t2.aid;
SELECT pid,GROUP_CONCAT(CONCAT(fn,' ',ln) ORDER BY 1) FROM t1,t2 
WHERE t1.aid = t2.aid GROUP BY pid;
pid	GROUP_CONCAT(CONCAT(fn,' ',ln) ORDER BY 1)
1	a b,c d
2	c d
SELECT pid,GROUP_CONCAT(CONCAT(fn,' ',ln) ORDER BY 1) FROM v1 GROUP BY pid;
pid	GROUP_CONCAT(CONCAT(fn,' ',ln) ORDER BY 1)
1	a b,c d
2	c d
DROP VIEW v1;
DROP TABLE t1,t2;
unknown's avatar
unknown committed
2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120
CREATE TABLE t1 (id int PRIMARY KEY, f varchar(255));
CREATE VIEW v1 AS SELECT id, f FROM t1 WHERE id <= 2;
INSERT INTO t1 VALUES (2, 'foo2');
INSERT INTO t1 VALUES (1, 'foo1');
SELECT * FROM v1;
id	f
1	foo1
2	foo2
SELECT * FROM v1;
id	f
1	foo1
2	foo2
DROP VIEW v1;
DROP TABLE t1;
unknown's avatar
unknown committed
2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
CREATE TABLE t1 (pk int PRIMARY KEY, b int);
CREATE TABLE t2 (pk int PRIMARY KEY, fk int, INDEX idx(fk));
CREATE TABLE t3 (pk int PRIMARY KEY, fk int, INDEX idx(fk));
CREATE TABLE t4 (pk int PRIMARY KEY, fk int, INDEX idx(fk));
CREATE TABLE t5 (pk int PRIMARY KEY, fk int, INDEX idx(fk));
CREATE VIEW v1 AS
SELECT t1.pk as a FROM t1,t2,t3,t4,t5
WHERE t1.b IS NULL AND
t1.pk=t2.fk AND t2.pk=t3.fk AND t3.pk=t4.fk AND t4.pk=t5.fk;
SELECT a FROM v1;
a
DROP VIEW v1;
DROP TABLE t1,t2,t3,t4,t5;
2134 2135 2136 2137 2138
create view v1 as select timestampdiff(day,'1997-01-01 00:00:00','1997-01-02 00:00:00') as f1;
select * from v1;
f1
1
drop view v1;
unknown's avatar
unknown committed
2139 2140 2141 2142 2143 2144 2145 2146
create table t1(a int);
create procedure p1() create view v1 as select * from t1;
drop table t1;
call p1();
ERROR 42S02: Table 'test.t1' doesn't exist
call p1();
ERROR 42S02: Table 'test.t1' doesn't exist
drop procedure p1;
2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158
create table t1 (f1 int);
create table t2 (f1 int);
insert into t1 values (1);
insert into t2 values (2);
create view v1 as select * from t1 union select * from t2 union all select * from t2;
select * from v1;
f1
1
2
2
drop view v1;
drop table t1,t2;
unknown's avatar
unknown committed
2159 2160 2161 2162 2163 2164
CREATE TEMPORARY TABLE t1 (a int);
CREATE FUNCTION f1 () RETURNS int RETURN (SELECT COUNT(*) FROM t1);
CREATE VIEW v1 AS SELECT f1();
ERROR HY000: View's SELECT refers to a temporary table 't1'
DROP FUNCTION f1;
DROP TABLE t1;
2165 2166 2167 2168 2169 2170 2171 2172 2173
DROP TABLE IF EXISTS t1;
DROP VIEW  IF EXISTS v1;
CREATE TABLE t1 (f4 CHAR(5));
CREATE VIEW v1 AS SELECT * FROM t1;
DESCRIBE v1;
Field	Type	Null	Key	Default	Extra
f4	char(5)	YES		NULL	
ALTER TABLE t1 CHANGE COLUMN f4 f4x CHAR(5);
DESCRIBE v1;
2174
ERROR HY000: View 'test.v1' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them
2175 2176
DROP TABLE t1;
DROP VIEW v1;
2177 2178 2179 2180 2181 2182
create table t1 (f1 char);
create view v1 as select strcmp(f1,'a') from t1;
select * from v1;
strcmp(f1,'a')
drop view v1;
drop table t1;
2183 2184 2185 2186 2187 2188 2189 2190
create table t1 (f1 int, f2 int,f3 int);
insert into t1 values (1,10,20),(2,0,0);
create view v1 as select * from t1;
select if(sum(f1)>1,f2,f3) from v1 group by f1;
if(sum(f1)>1,f2,f3)
20
0
drop view v1;
2191
drop table t1;
2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217
create table t1 (
r_object_id char(16) NOT NULL,
group_name varchar(32) NOT NULL
) engine = InnoDB;
create table t2 (
r_object_id char(16) NOT NULL,
i_position int(11) NOT NULL, 
users_names varchar(32) default NULL
) Engine = InnoDB;
create view v1 as select r_object_id, group_name from t1;
create view v2 as select r_object_id, i_position, users_names from t2;
create unique index r_object_id on t1(r_object_id);
create index group_name on t1(group_name);
create unique index r_object_id_i_position on t2(r_object_id,i_position);
create index users_names on t2(users_names);
insert into t1 values('120001a080000542','tstgroup1');
insert into t2 values('120001a080000542',-1, 'guser01');
insert into t2 values('120001a080000542',-2, 'guser02');
select v1.r_object_id, v2.users_names from v1, v2
where (v1.group_name='tstgroup1') and v2.r_object_id=v1.r_object_id 
order by users_names;
r_object_id	users_names
120001a080000542	guser01
120001a080000542	guser02
drop view v1, v2;
drop table t1, t2;
unknown's avatar
unknown committed
2218 2219 2220 2221
create table t1 (s1 int);
create view abc as select * from t1 as abc;
drop table t1;
drop view abc;
2222 2223 2224 2225 2226 2227
create table t1(f1 char(1));
create view v1 as select * from t1;
select * from (select f1 as f2 from v1) v where v.f2='a';
f2
drop view v1;
drop table t1;
2228 2229 2230 2231 2232
create view v1 as SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET');
select * from v1;
CONVERT_TZ('2004-01-01 12:00:00','GMT','MET')
NULL
drop view v1;
2233 2234 2235 2236 2237
CREATE TABLE t1 (date DATE NOT NULL);
INSERT INTO  t1 VALUES ('2005-09-06');
CREATE VIEW v1 AS SELECT DAYNAME(date) FROM t1;
SHOW CREATE VIEW v1;
View	Create View
2238
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select dayname(`t1`.`date`) AS `DAYNAME(date)` from `t1`
2239 2240 2241
CREATE VIEW v2 AS SELECT DAYOFWEEK(date) FROM t1;
SHOW CREATE VIEW v2;
View	Create View
2242
v2	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v2` AS select dayofweek(`t1`.`date`) AS `DAYOFWEEK(date)` from `t1`
2243 2244 2245
CREATE VIEW v3 AS SELECT WEEKDAY(date) FROM t1;
SHOW CREATE VIEW v3;
View	Create View
2246
v3	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v3` AS select weekday(`t1`.`date`) AS `WEEKDAY(date)` from `t1`
2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275
SELECT DAYNAME('2005-09-06');
DAYNAME('2005-09-06')
Tuesday
SELECT DAYNAME(date) FROM t1;
DAYNAME(date)
Tuesday
SELECT * FROM v1;
DAYNAME(date)
Tuesday
SELECT DAYOFWEEK('2005-09-06');
DAYOFWEEK('2005-09-06')
3
SELECT DAYOFWEEK(date) FROM t1;
DAYOFWEEK(date)
3
SELECT * FROM v2;
DAYOFWEEK(date)
3
SELECT WEEKDAY('2005-09-06');
WEEKDAY('2005-09-06')
1
SELECT WEEKDAY(date) FROM t1;
WEEKDAY(date)
1
SELECT * FROM v3;
WEEKDAY(date)
1
DROP TABLE t1;
DROP VIEW  v1, v2, v3;
unknown's avatar
unknown committed
2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288
CREATE TABLE t1 ( a int, b int );
INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
CREATE VIEW v1 AS SELECT a,b FROM t1;
SELECT t1.a FROM t1 GROUP BY t1.a HAVING a > 1;
a
2
3
SELECT v1.a FROM v1 GROUP BY v1.a HAVING a > 1;
a
2
3
DROP VIEW v1;
DROP TABLE t1;
unknown's avatar
unknown committed
2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299
CREATE TABLE t1 ( a int, b int );
INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
CREATE VIEW v1 AS SELECT a,b FROM t1;
SELECT t1.a FROM t1 GROUP BY t1.a HAVING t1.a > 1;
a
2
3
SELECT v1.a FROM v1 GROUP BY v1.a HAVING v1.a > 1;
a
2
3
2300 2301 2302 2303 2304 2305 2306 2307 2308 2309
SELECT t_1.a FROM t1 AS t_1 GROUP BY t_1.a HAVING t_1.a IN (1,2,3);
a
1
2
3
SELECT v_1.a FROM v1 AS v_1 GROUP BY v_1.a HAVING v_1.a IN (1,2,3);
a
1
2
3
unknown's avatar
unknown committed
2310 2311
DROP VIEW v1;
DROP TABLE t1;
2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325
CREATE TABLE t1 (a INT, b INT, INDEX(a,b));
CREATE TABLE t2 LIKE t1;
CREATE TABLE t3 (a INT);
INSERT INTO t1 VALUES (1,1),(2,2),(3,3);
INSERT INTO t2 VALUES (1,1),(2,2),(3,3);
INSERT INTO t3 VALUES (1),(2),(3);
CREATE VIEW v1 AS SELECT t1.* FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b;
CREATE VIEW v2 AS SELECT t3.* FROM t1,t3 WHERE t1.a=t3.a;
EXPLAIN SELECT t1.* FROM t1 JOIN t2 WHERE t1.a=t2.a AND t1.b=t2.b AND t1.a=1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ref	a	a	5	const	1	Using where; Using index
1	SIMPLE	t2	ref	a	a	10	const,test.t1.b	2	Using where; Using index
EXPLAIN SELECT * FROM v1 WHERE a=1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
2326 2327
1	SIMPLE	t1	ref	a	a	5	const	1	Using where; Using index
1	SIMPLE	t2	ref	a	a	10	const,test.t1.b	2	Using where; Using index
2328 2329
EXPLAIN SELECT * FROM v2 WHERE a=1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
2330 2331
1	SIMPLE	t1	ref	a	a	5	const	1	Using where; Using index
1	SIMPLE	t3	ALL	NULL	NULL	NULL	NULL	3	Using where
2332 2333
DROP VIEW v1,v2;
DROP TABLE t1,t2,t3;
2334 2335 2336 2337 2338 2339 2340 2341
create table t1 (f1 int);
create view v1 as select t1.f1 as '123
456' from t1;
select * from v1;
123
456
drop view v1;
drop table t1;
2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353
create table t1 (f1 int, f2 int);
insert into t1 values(1,1),(1,2),(1,3);
create view v1 as select f1 ,group_concat(f2 order by f2 asc) from t1 group by f1;
create view v2 as select f1 ,group_concat(f2 order by f2 desc) from t1 group by f1;
select * from v1;
f1	group_concat(f2 order by f2 asc)
1	1,2,3
select * from v2;
f1	group_concat(f2 order by f2 desc)
1	3,2,1
drop view v1,v2;
drop table t1;
2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374
create table t1 (x int, y int);
create table t2 (x int, y int, z int);
create table t3 (x int, y int, z int);
create table t4 (x int, y int, z int);
create view v1 as
select t1.x
from (
(t1 join t2 on ((t1.y = t2.y))) 
join 
(t3 left join t4 on (t3.y = t4.y) and (t3.z = t4.z))
);
prepare stmt1 from "select count(*) from v1 where x = ?";
set @parm1=1;
execute stmt1 using @parm1;
count(*)
0
execute stmt1 using @parm1;
count(*)
0
drop view v1;
drop table t1,t2,t3,t4;
unknown's avatar
unknown committed
2375
CREATE TABLE t1(id INT);
unknown's avatar
unknown committed
2376 2377 2378
CREATE VIEW v1 AS SELECT id FROM t1;
OPTIMIZE TABLE v1;
Table	Op	Msg_type	Msg_text
2379 2380 2381
test.v1	optimize	error	'test.v1' is not BASE TABLE
Warnings:
Error	1347	'test.v1' is not BASE TABLE
unknown's avatar
unknown committed
2382 2383
ANALYZE TABLE v1;
Table	Op	Msg_type	Msg_text
2384 2385 2386
test.v1	analyze	error	'test.v1' is not BASE TABLE
Warnings:
Error	1347	'test.v1' is not BASE TABLE
unknown's avatar
unknown committed
2387 2388
REPAIR TABLE v1;
Table	Op	Msg_type	Msg_text
2389 2390 2391
test.v1	repair	error	'test.v1' is not BASE TABLE
Warnings:
Error	1347	'test.v1' is not BASE TABLE
unknown's avatar
unknown committed
2392 2393 2394
DROP TABLE t1;
OPTIMIZE TABLE v1;
Table	Op	Msg_type	Msg_text
2395
test.v1	optimize	error	'test.v1' is not BASE TABLE
unknown's avatar
unknown committed
2396
Warnings:
2397
Error	1347	'test.v1' is not BASE TABLE
2398 2399 2400 2401 2402 2403 2404 2405 2406 2407
ANALYZE TABLE v1;
Table	Op	Msg_type	Msg_text
test.v1	analyze	error	'test.v1' is not BASE TABLE
Warnings:
Error	1347	'test.v1' is not BASE TABLE
REPAIR TABLE v1;
Table	Op	Msg_type	Msg_text
test.v1	repair	error	'test.v1' is not BASE TABLE
Warnings:
Error	1347	'test.v1' is not BASE TABLE
unknown's avatar
unknown committed
2408
DROP VIEW v1;
2409 2410 2411 2412 2413 2414 2415 2416 2417 2418
create definer = current_user() sql security invoker view v1 as select 1;
show create view v1;
View	Create View
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY INVOKER VIEW `v1` AS select 1 AS `1`
drop view v1;
create definer = current_user sql security invoker view v1 as select 1;
show create view v1;
View	Create View
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY INVOKER VIEW `v1` AS select 1 AS `1`
drop view v1;
2419 2420 2421 2422 2423
create table t1 (id INT, primary key(id));
insert into t1 values (1),(2);
create view v1 as select * from t1;
explain select id from v1 order by id;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
2424
1	SIMPLE	t1	index	NULL	PRIMARY	4	NULL	2	Using index
2425 2426
drop view v1;
drop table t1;
2427 2428
create table t1(f1 int, f2 int);
insert into t1 values (null, 10), (null,2);
unknown's avatar
unknown committed
2429 2430 2431
select f1, sum(f2) from t1 group by f1;
f1	sum(f2)
NULL	12
2432 2433 2434 2435 2436 2437
create view v1 as select * from t1;
select f1, sum(f2) from v1 group by f1;
f1	sum(f2)
NULL	12
drop view v1;
drop table t1;
2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449
drop procedure if exists p1;
create procedure p1 () deterministic
begin
create view v1 as select 1;
end;
//
call p1();
show create view v1;
View	Create View
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select 1 AS `1`
drop view v1;
drop procedure p1;
2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464
CREATE VIEW v1 AS SELECT 42 AS Meaning;
DROP FUNCTION IF EXISTS f1;
CREATE FUNCTION f1() RETURNS INTEGER
BEGIN
DECLARE retn INTEGER;
SELECT Meaning FROM v1 INTO retn;
RETURN retn;
END
//
CREATE VIEW v2 AS SELECT f1();
select * from v2;
f1()
42
drop view v2,v1;
drop function f1;
2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480
create table t1 (id numeric, warehouse_id numeric);
create view v1 as select id from t1;
create view v2 as
select t1.warehouse_id, v1.id as receipt_id
from t1, v1 where t1.id = v1.id;
insert into t1 (id, warehouse_id) values(3, 2);
insert into t1 (id, warehouse_id) values(4, 2);
insert into t1 (id, warehouse_id) values(5, 1);
select v2.receipt_id as alias1, v2.receipt_id as alias2 from v2
order by v2.receipt_id;
alias1	alias2
3	3
4	4
5	5
drop view v2, v1;
drop table t1;
2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494
CREATE TABLE t1 (a int PRIMARY KEY, b int);
INSERT INTO t1 VALUES (2,20), (3,10), (1,10), (0,30), (5,10);
CREATE VIEW v1 AS SELECT * FROM t1;
SELECT MAX(a) FROM t1;
MAX(a)
5
SELECT MAX(a) FROM v1;
MAX(a)
5
EXPLAIN SELECT MAX(a) FROM t1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
EXPLAIN SELECT MAX(a) FROM v1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
2495
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506
SELECT MIN(a) FROM t1;
MIN(a)
0
SELECT MIN(a) FROM v1;
MIN(a)
0
EXPLAIN SELECT MIN(a) FROM t1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
EXPLAIN SELECT MIN(a) FROM v1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
2507
1	SIMPLE	NULL	NULL	NULL	NULL	NULL	NULL	NULL	Select tables optimized away
2508 2509
DROP VIEW v1;
DROP TABLE t1;
unknown's avatar
unknown committed
2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543
CREATE TABLE t1 (x varchar(10));
INSERT INTO t1 VALUES (null), ('foo'), ('bar'), (null);
CREATE VIEW v1 AS SELECT * FROM t1;
SELECT IF(x IS NULL, 'blank', 'not blank') FROM v1 GROUP BY x;
IF(x IS NULL, 'blank', 'not blank')
blank
not blank
not blank
SELECT IF(x IS NULL, 'blank', 'not blank') AS x FROM t1 GROUP BY x;
x
blank
not blank
not blank
Warnings:
Warning	1052	Column 'x' in group statement is ambiguous
SELECT IF(x IS NULL, 'blank', 'not blank') AS x FROM v1;
x
blank
not blank
not blank
blank
SELECT IF(x IS NULL, 'blank', 'not blank') AS y FROM v1 GROUP BY y;
y
blank
not blank
SELECT IF(x IS NULL, 'blank', 'not blank') AS x FROM v1 GROUP BY x;
x
blank
not blank
not blank
Warnings:
Warning	1052	Column 'x' in group statement is ambiguous
DROP VIEW v1;
DROP TABLE t1;
2544 2545 2546 2547 2548 2549 2550 2551 2552
drop table if exists t1; 
drop view if exists v1; 
create table t1 (id int); 
create view v1 as select * from t1; 
drop table t1; 
show create view v1; 
drop view v1;
//
View	Create View
2553
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `test`.`t1`.`id` AS `id` from `t1`
2554 2555 2556 2557
create table t1(f1 int, f2 int);
create view v1 as select ta.f1 as a, tb.f1 as b from t1 ta, t1 tb where ta.f1=tb
.f1 and ta.f2=tb.f2;
insert into t1 values(1,1),(2,2);
unknown's avatar
unknown committed
2558
create view v2 as select * from v1 where a > 1 with local check option;
2559 2560 2561 2562 2563 2564 2565 2566 2567
select * from v2;
a	b
2	2
update v2 set b=3 where a=2;
select * from v2;
a	b
3	3
drop view v2, v1;
drop table t1;
unknown's avatar
unknown committed
2568 2569 2570 2571 2572 2573 2574 2575 2576
CREATE TABLE t1 (a int);
INSERT INTO t1 VALUES (1), (2);
CREATE VIEW v1 AS SELECT SQRT(a) my_sqrt FROM t1;
SELECT my_sqrt FROM v1 ORDER BY my_sqrt;
my_sqrt
1
1.4142135623731
DROP VIEW v1;
DROP TABLE t1;
unknown's avatar
unknown committed
2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593
CREATE TABLE t1 (id int PRIMARY KEY);
CREATE TABLE t2 (id int PRIMARY KEY);
INSERT INTO t1 VALUES (1), (3);
INSERT INTO t2 VALUES (1), (2), (3);
CREATE VIEW v2 AS SELECT * FROM t2;
SELECT COUNT(*) FROM t1 LEFT JOIN t2 ON t1.id=t2.id;
COUNT(*)
2
SELECT * FROM t1 LEFT JOIN t2 ON t1.id=t2.id;
id	id
1	1
3	3
SELECT COUNT(*) FROM t1 LEFT JOIN v2 ON t1.id=v2.id;
COUNT(*)
2
DROP VIEW v2;
DROP TABLE t1, t2;
unknown's avatar
unknown committed
2594 2595 2596 2597 2598 2599 2600
CREATE TABLE t1 (id int NOT NULL PRIMARY KEY,
td date DEFAULT NULL, KEY idx(td));
INSERT INTO t1 VALUES 
(1, '2005-01-01'), (2, '2005-01-02'), (3, '2005-01-02'),
(4, '2005-01-03'), (5, '2005-01-04'), (6, '2005-01-05'),
(7, '2005-01-05'), (8, '2005-01-05'), (9, '2005-01-06');
CREATE VIEW v1 AS SELECT * FROM t1;
unknown's avatar
unknown committed
2601
SELECT * FROM t1 WHERE td BETWEEN CAST('2005.01.02' AS DATE) AND CAST('2005.01.04' AS DATE);
unknown's avatar
unknown committed
2602 2603 2604 2605 2606
id	td
2	2005-01-02
3	2005-01-02
4	2005-01-03
5	2005-01-04
unknown's avatar
unknown committed
2607
SELECT * FROM v1 WHERE td BETWEEN CAST('2005.01.02' AS DATE) AND CAST('2005.01.04' AS DATE);
unknown's avatar
unknown committed
2608 2609 2610 2611 2612 2613 2614
id	td
2	2005-01-02
3	2005-01-02
4	2005-01-03
5	2005-01-04
DROP VIEW v1;
DROP TABLE t1;
2615 2616 2617 2618 2619 2620
create table t1 (a int);
create view v1 as select * from t1;
create view v2 as select * from v1;
drop table t1;
rename table v2 to t1;
select * from v1;
unknown's avatar
unknown committed
2621
ERROR HY000: `test`.`v1` contains view recursion
2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637
drop view t1, v1;
create table t1 (a int);
create function f1() returns int
begin
declare mx int;
select max(a) from t1 into mx;
return mx;
end//
create view v1 as select f1() as a;
create view v2 as select * from v1;
drop table t1;
rename table v2 to t1;
select * from v1;
ERROR HY000: Recursive stored functions and triggers are not allowed.
drop function f1;
drop view t1, v1;
2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663
create table t1 (dt datetime);
insert into t1 values (20040101000000), (20050101000000), (20060101000000);
create view v1 as select convert_tz(dt, 'UTC', 'Europe/Moscow') as ldt from t1;
select * from v1;
ldt
2004-01-01 03:00:00
2005-01-01 03:00:00
2006-01-01 03:00:00
drop view v1;
create view v1 as select * from t1 where convert_tz(dt, 'UTC', 'Europe/Moscow') >= 20050101000000;
select * from v1;
dt
2005-01-01 00:00:00
2006-01-01 00:00:00
create view v2 as select * from v1 where dt < 20060101000000;
select * from v2;
dt
2005-01-01 00:00:00
drop view v2;
create view v2 as select convert_tz(dt, 'UTC', 'Europe/Moscow') as ldt from v1;
select * from v2;
ldt
2005-01-01 03:00:00
2006-01-01 03:00:00
drop view v1, v2;
drop table t1;
2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674
CREATE TABLE t1 (id int NOT NULL PRIMARY KEY, d datetime);
CREATE VIEW v1 AS
SELECT id, date(d) + INTERVAL TIME_TO_SEC(d) SECOND AS t, COUNT(*)
FROM t1 GROUP BY id, t;
SHOW CREATE VIEW v1;
View	Create View
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`id` AS `id`,(cast(`t1`.`d` as date) + interval time_to_sec(`t1`.`d`) second) AS `t`,count(0) AS `COUNT(*)` from `t1` group by `t1`.`id`,(cast(`t1`.`d` as date) + interval time_to_sec(`t1`.`d`) second)
SELECT * FROM v1;
id	t	COUNT(*)
DROP VIEW v1;
DROP TABLE t1;
2675 2676 2677 2678 2679 2680 2681 2682 2683
CREATE TABLE t1 (i INT, j BIGINT);
INSERT INTO t1 VALUES (1, 2), (2, 2), (3, 2);
CREATE VIEW v1 AS SELECT MIN(j) AS j FROM t1;
CREATE VIEW v2 AS SELECT MIN(i) FROM t1 WHERE j = ( SELECT * FROM v1 );
SELECT * FROM v2;
MIN(i)
1
DROP VIEW v2, v1;
DROP TABLE t1;
unknown's avatar
unknown committed
2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697
CREATE TABLE t1(
fName varchar(25) NOT NULL,
lName varchar(25) NOT NULL,
DOB date NOT NULL,
uID int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY);
INSERT INTO t1(fName, lName, DOB) VALUES
('Hank', 'Hill', '1964-09-29'),
('Tom', 'Adams', '1908-02-14'),
('Homer', 'Simpson', '1968-03-05');
CREATE VIEW v1 AS
SELECT (year(now())-year(DOB)) AS Age
FROM t1 HAVING Age < 75;
SHOW CREATE VIEW v1;
View	Create View
2698
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select (year(now()) - year(`t1`.`DOB`)) AS `Age` from `t1` having (`Age` < 75)
unknown's avatar
unknown committed
2699 2700
SELECT (year(now())-year(DOB)) AS Age FROM t1 HAVING Age < 75;
Age
unknown's avatar
unknown committed
2701 2702
43
39
unknown's avatar
unknown committed
2703 2704
SELECT * FROM v1;
Age
unknown's avatar
unknown committed
2705 2706
43
39
unknown's avatar
unknown committed
2707 2708
DROP VIEW v1;
DROP TABLE t1;
unknown's avatar
unknown committed
2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730
CREATE TABLE t1 (id int NOT NULL PRIMARY KEY, a char(6) DEFAULT 'xxx');
INSERT INTO t1(id) VALUES (1), (2), (3), (4);
INSERT INTO t1 VALUES (5,'yyy'), (6,'yyy');
SELECT * FROM t1;
id	a
1	xxx
2	xxx
3	xxx
4	xxx
5	yyy
6	yyy
CREATE VIEW v1(a, m) AS SELECT a, MIN(id) FROM t1 GROUP BY a;
SELECT * FROM v1;
a	m
xxx	1
yyy	5
CREATE TABLE t2 SELECT * FROM v1;
INSERT INTO t2(m) VALUES (0);
SELECT * FROM t2;
a	m
xxx	1
yyy	5
unknown's avatar
unknown committed
2731
xxx	0
unknown's avatar
unknown committed
2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749
DROP VIEW v1;
DROP TABLE t1,t2;
CREATE TABLE t1 (id int PRIMARY KEY, e ENUM('a','b') NOT NULL DEFAULT 'b');
INSERT INTO t1(id) VALUES (1), (2), (3);
INSERT INTO t1 VALUES (4,'a');
SELECT * FROM t1;
id	e
1	b
2	b
3	b
4	a
CREATE VIEW v1(m, e) AS SELECT MIN(id), e FROM t1 GROUP BY e;
CREATE TABLE t2 SELECT * FROM v1;
SELECT * FROM t2;
m	e
4	a
1	b
DROP VIEW v1;
2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764
DROP TABLE t1,t2;
CREATE TABLE t1 (a INT NOT NULL, b INT NULL DEFAULT NULL);
CREATE VIEW v1 AS SELECT a, b FROM t1;
INSERT INTO v1 (b) VALUES (2);
Warnings:
Warning	1423	Field of view 'test.v1' underlying table doesn't have a default value
SET SQL_MODE = STRICT_ALL_TABLES;
INSERT INTO v1 (b) VALUES (4);
ERROR HY000: Field of view 'test.v1' underlying table doesn't have a default value
SET SQL_MODE = '';
SELECT * FROM t1;
a	b
0	2
DROP VIEW v1;
DROP TABLE t1;
2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777
CREATE TABLE t1 (firstname text, surname text);
INSERT INTO t1 VALUES
("Bart","Simpson"),("Milhouse","van Houten"),("Montgomery","Burns");
CREATE VIEW v1 AS SELECT CONCAT(firstname," ",surname) AS name FROM t1;
SELECT CONCAT(LEFT(name,LENGTH(name)-INSTR(REVERSE(name)," ")),
LEFT(name,LENGTH(name)-INSTR(REVERSE(name)," "))) AS f1
FROM v1;
f1
BartBart
Milhouse vanMilhouse van
MontgomeryMontgomery
DROP VIEW v1;
DROP TABLE t1;
unknown's avatar
unknown committed
2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788
CREATE TABLE t1 (i int, j int);
CREATE VIEW v1 AS SELECT COALESCE(i,j) FROM t1;
DESCRIBE v1;
Field	Type	Null	Key	Default	Extra
COALESCE(i,j)	int(11)	YES		NULL	
CREATE TABLE t2 SELECT COALESCE(i,j) FROM t1;
DESCRIBE t2;
Field	Type	Null	Key	Default	Extra
COALESCE(i,j)	int(11)	YES		NULL	
DROP VIEW v1;
DROP TABLE t1,t2;
2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821
CREATE TABLE t1 (s varchar(10));
INSERT INTO t1 VALUES ('yadda'), ('yady');
SELECT TRIM(BOTH 'y' FROM s) FROM t1;
TRIM(BOTH 'y' FROM s)
adda
ad
CREATE VIEW v1 AS SELECT TRIM(BOTH 'y' FROM s) FROM t1;
SELECT * FROM v1;
TRIM(BOTH 'y' FROM s)
adda
ad
DROP VIEW v1;
SELECT TRIM(LEADING 'y' FROM s) FROM t1;
TRIM(LEADING 'y' FROM s)
adda
ady
CREATE VIEW v1 AS SELECT TRIM(LEADING 'y' FROM s) FROM t1;
SELECT * FROM v1;
TRIM(LEADING 'y' FROM s)
adda
ady
DROP VIEW v1;
SELECT TRIM(TRAILING 'y' FROM s) FROM t1;
TRIM(TRAILING 'y' FROM s)
yadda
yad
CREATE VIEW v1 AS SELECT TRIM(TRAILING 'y' FROM s) FROM t1;
SELECT * FROM v1;
TRIM(TRAILING 'y' FROM s)
yadda
yad
DROP VIEW v1;
DROP TABLE t1;
2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832
CREATE TABLE t1 (x INT, y INT);
CREATE ALGORITHM=TEMPTABLE SQL SECURITY INVOKER VIEW v1 AS SELECT x FROM t1;
SHOW CREATE VIEW v1;
View	Create View
v1	CREATE ALGORITHM=TEMPTABLE DEFINER=`root`@`localhost` SQL SECURITY INVOKER VIEW `v1` AS select `t1`.`x` AS `x` from `t1`
ALTER VIEW v1 AS SELECT x, y FROM t1;
SHOW CREATE VIEW v1;
View	Create View
v1	CREATE ALGORITHM=TEMPTABLE DEFINER=`root`@`localhost` SQL SECURITY INVOKER VIEW `v1` AS select `t1`.`x` AS `x`,`t1`.`y` AS `y` from `t1`
DROP VIEW v1;
DROP TABLE t1;
2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845
CREATE TABLE t1 (s1 char);
INSERT INTO t1 VALUES ('Z');
CREATE VIEW v1 AS SELECT s1 collate latin1_german1_ci AS col FROM t1;
CREATE VIEW v2 (col) AS SELECT s1 collate latin1_german1_ci FROM t1;
INSERT INTO v1 (col) VALUES ('b');
INSERT INTO v2 (col) VALUES ('c');
SELECT s1 FROM t1;
s1
Z
b
c
DROP VIEW v1, v2;
DROP TABLE t1;
2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864
CREATE TABLE t1 (id INT);
CREATE VIEW v1 AS SELECT id FROM t1;
SHOW TABLES;
Tables_in_test
t1
v1
DROP VIEW v2,v1;
ERROR 42S02: Unknown table 'v2'
SHOW TABLES;
Tables_in_test
t1
CREATE VIEW v1 AS SELECT id FROM t1;
DROP VIEW t1,v1;
ERROR HY000: 'test.t1' is not VIEW
SHOW TABLES;
Tables_in_test
t1
DROP TABLE t1;
DROP VIEW IF EXISTS v1;
2865
CREATE DATABASE bug21261DB;
unknown's avatar
unknown committed
2866
USE bug21261DB;
2867 2868 2869 2870 2871 2872 2873 2874 2875
CREATE TABLE t1 (x INT);
CREATE SQL SECURITY INVOKER VIEW v1 AS SELECT x FROM t1;
GRANT INSERT, UPDATE ON v1 TO 'user21261'@'localhost';
GRANT INSERT, UPDATE ON t1 TO 'user21261'@'localhost';
CREATE TABLE t2 (y INT);
GRANT SELECT ON t2 TO 'user21261'@'localhost';
INSERT INTO v1 (x) VALUES (5);
UPDATE v1 SET x=1;
GRANT SELECT ON v1 TO 'user21261'@'localhost';
unknown's avatar
unknown committed
2876
GRANT SELECT ON t1 TO 'user21261'@'localhost';
2877 2878 2879 2880 2881 2882 2883 2884 2885
UPDATE v1,t2 SET x=1 WHERE x=y;
SELECT * FROM t1;
x
1
REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'user21261'@'localhost';
DROP USER 'user21261'@'localhost';
DROP VIEW v1;
DROP TABLE t1;
DROP DATABASE bug21261DB;
unknown's avatar
unknown committed
2886
USE test;
2887 2888 2889 2890 2891 2892 2893
create table t1 (f1 datetime);
create view v1 as select * from t1 where f1 between now() and now() + interval 1 minute;
show create view v1;
View	Create View
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`f1` AS `f1` from `t1` where (`t1`.`f1` between now() and (now() + interval 1 minute))
drop view v1;
drop table t1;
unknown's avatar
unknown committed
2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904
DROP TABLE IF EXISTS t1;
DROP VIEW IF EXISTS v1;
DROP VIEW IF EXISTS v2;
CREATE TABLE t1(a INT, b INT);
CREATE DEFINER=1234567890abcdefGHIKL@localhost
VIEW v1 AS SELECT a FROM t1;
ERROR HY000: String '1234567890abcdefGHIKL' is too long for user name (should be no longer than 16)
CREATE DEFINER=some_user_name@1234567890abcdefghij1234567890abcdefghij1234567890abcdefghijQWERTY
VIEW v2 AS SELECT b FROM t1;
ERROR HY000: String '1234567890abcdefghij1234567890abcdefghij1234567890abcdefghijQWERTY' is too long for host name (should be no longer than 60)
DROP TABLE t1;
2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925
DROP FUNCTION IF EXISTS f1;
DROP FUNCTION IF EXISTS f2;
DROP VIEW IF EXISTS v1, v2;
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (i INT);
CREATE VIEW v1 AS SELECT * FROM t1;
CREATE FUNCTION f1() RETURNS INT
BEGIN
INSERT INTO v1 VALUES (0);
RETURN 0;
END |
SELECT f1();
f1()
0
CREATE ALGORITHM=TEMPTABLE VIEW v2 AS SELECT * FROM t1;
CREATE FUNCTION f2() RETURNS INT
BEGIN
INSERT INTO v2 VALUES (0);
RETURN 0;
END |
SELECT f2();
2926
ERROR HY000: The target table v2 of the INSERT is not insertable-into
2927 2928 2929 2930
DROP FUNCTION f1;
DROP FUNCTION f2;
DROP VIEW v1, v2;
DROP TABLE t1;
2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949
CREATE TABLE t1 (s1 int);
CREATE VIEW v1 AS SELECT * FROM t1;
EXPLAIN SELECT * FROM t1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	system	NULL	NULL	NULL	NULL	0	const row not found
EXPLAIN SELECT * FROM v1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	system	NULL	NULL	NULL	NULL	0	const row not found
INSERT INTO t1 VALUES (1), (3), (2);
EXPLAIN SELECT * FROM t1 t WHERE t.s1+1 < (SELECT MAX(t1.s1) FROM t1);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t	ALL	NULL	NULL	NULL	NULL	3	Using where
2	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	3	
EXPLAIN SELECT * FROM v1 t WHERE t.s1+1 < (SELECT MAX(t1.s1) FROM t1);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	Using where
2	SUBQUERY	t1	ALL	NULL	NULL	NULL	NULL	3	
DROP VIEW v1;
DROP TABLE t1;
2950 2951 2952 2953 2954 2955 2956
create table t1 (s1 int);
create view v1 as select s1 as a, s1 as b from t1;
insert into v1 values (1,1);
ERROR HY000: The target table v1 of the INSERT is not insertable-into
update v1 set a = 5;
drop view v1;
drop table t1;
unknown's avatar
unknown committed
2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970
CREATE TABLE t1(pk int PRIMARY KEY);
CREATE TABLE t2(pk int PRIMARY KEY, fk int, ver int, org int);
CREATE ALGORITHM=MERGE VIEW v1 AS 
SELECT t1.*
FROM t1 JOIN t2 
ON t2.fk = t1.pk AND 
t2.ver = (SELECT MAX(t.ver) FROM t2 t WHERE t.org = t2.org);
SHOW WARNINGS;
Level	Code	Message
SHOW CREATE VIEW v1;
View	Create View
v1	CREATE ALGORITHM=MERGE DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`pk` AS `pk` from (`t1` join `t2` on(((`t2`.`fk` = `t1`.`pk`) and (`t2`.`ver` = (select max(`t`.`ver`) AS `MAX(t.ver)` from `t2` `t` where (`t`.`org` = `t2`.`org`))))))
DROP VIEW v1;
DROP TABLE t1, t2;
unknown's avatar
unknown committed
2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982
DROP FUNCTION IF EXISTS f1;
DROP VIEW IF EXISTS v1;
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (i INT);
INSERT INTO t1 VALUES (1);
CREATE VIEW v1 AS SELECT MAX(i) FROM t1;
CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW
SET NEW.i = (SELECT * FROM v1) + 1;
INSERT INTO t1 VALUES (1);
CREATE FUNCTION f1() RETURNS INT RETURN (SELECT * FROM v1);
UPDATE t1 SET i= f1();
DROP FUNCTION f1;
unknown's avatar
unknown committed
2983 2984
DROP VIEW v1;
DROP TABLE t1;
2985 2986 2987 2988 2989 2990 2991 2992
CREATE TABLE t1(id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, val INT UNSIGNED NOT NULL);
CREATE VIEW v1 AS SELECT id, val FROM t1 WHERE val >= 1 AND val <= 5 WITH CHECK OPTION;
INSERT INTO v1 (val) VALUES (2);
INSERT INTO v1 (val) VALUES (4);
INSERT INTO v1 (val) VALUES (6);
ERROR HY000: CHECK OPTION failed 'test.v1'
UPDATE v1 SET val=6 WHERE id=2;
ERROR HY000: CHECK OPTION failed 'test.v1'
unknown's avatar
unknown committed
2993 2994
DROP VIEW v1;
DROP TABLE t1;
2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028
DROP VIEW IF EXISTS v1, v2;
DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (i INT AUTO_INCREMENT PRIMARY KEY, j INT);
CREATE VIEW v1 AS SELECT j FROM t1;
CREATE VIEW v2 AS SELECT * FROM t1;
INSERT INTO t1 (j) VALUES (1);
SELECT LAST_INSERT_ID();
LAST_INSERT_ID()
1
INSERT INTO v1 (j) VALUES (2);
# LAST_INSERT_ID() should not change.
SELECT LAST_INSERT_ID();
LAST_INSERT_ID()
1
INSERT INTO v2 (j) VALUES (3);
# LAST_INSERT_ID() should be updated.
SELECT LAST_INSERT_ID();
LAST_INSERT_ID()
3
INSERT INTO v1 (j) SELECT j FROM t1;
# LAST_INSERT_ID() should not change.
SELECT LAST_INSERT_ID();
LAST_INSERT_ID()
3
SELECT * FROM t1;
i	j
1	1
2	2
3	3
4	1
5	2
6	3
DROP VIEW v1, v2;
DROP TABLE t1;
unknown's avatar
unknown committed
3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039
CREATE VIEW v AS SELECT !0 * 5 AS x FROM DUAL;
SHOW CREATE VIEW v;
View	Create View
v	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` AS select ((not(0)) * 5) AS `x`
SELECT !0 * 5 AS x FROM DUAL;
x
5
SELECT * FROM v;
x
5
DROP VIEW v;
3040 3041 3042 3043 3044 3045 3046 3047 3048
DROP VIEW IF EXISTS v1;
CREATE VIEW v1 AS SELECT 'The\ZEnd';
SELECT * FROM v1;
TheEnd
TheEnd
SHOW CREATE VIEW v1;
View	Create View
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select _latin1'The\ZEnd' AS `TheEnd`
DROP VIEW v1;
unknown's avatar
unknown committed
3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149
CREATE TABLE t1 (mydate DATETIME);
INSERT INTO t1 VALUES
('2007-01-01'), ('2007-01-02'), ('2007-01-30'), ('2007-01-31');
CREATE VIEW v1 AS SELECT mydate from t1;
SELECT * FROM t1 WHERE mydate BETWEEN '2007-01-01' AND '2007-01-31';
mydate
2007-01-01 00:00:00
2007-01-02 00:00:00
2007-01-30 00:00:00
2007-01-31 00:00:00
SELECT * FROM v1 WHERE mydate BETWEEN '2007-01-01' AND '2007-01-31';
mydate
2007-01-01 00:00:00
2007-01-02 00:00:00
2007-01-30 00:00:00
2007-01-31 00:00:00
DROP VIEW v1;
DROP TABLE t1;
CREATE TABLE t1 (a int);
CREATE TABLE t2 (b int);
INSERT INTO t1 VALUES (1), (2);
INSERT INTO t2 VALUES (1), (2);
CREATE VIEW v1 AS
SELECT t2.b FROM t1,t2 WHERE t1.a = t2.b WITH CHECK OPTION;
SELECT * FROM v1;
b
1
2
UPDATE v1 SET b=3;
ERROR HY000: CHECK OPTION failed 'test.v1'
SELECT * FROM v1;
b
1
2
SELECT * FROM t1;
a
1
2
SELECT * FROM t2;
b
1
2
DROP VIEW v1;
DROP TABLE t1,t2;
create table t1(f1 int, f2 int);
insert into t1 values(1,2),(1,3),(1,1),(2,3),(2,1),(2,2);
select * from t1;
f1	f2
1	2
1	3
1	1
2	3
2	1
2	2
create view v1 as select * from t1 order by f2;
select * from v1;
f1	f2
1	1
2	1
1	2
2	2
1	3
2	3
explain extended select * from v1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	6	Using filesort
Warnings:
Note	1003	select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f2` AS `f2` from `test`.`t1` order by `test`.`t1`.`f2`
select * from v1 order by f1;
f1	f2
1	1
1	2
1	3
2	1
2	2
2	3
explain extended select * from v1 order by f1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	6	Using filesort
Warnings:
Note	1003	select `test`.`t1`.`f1` AS `f1`,`test`.`t1`.`f2` AS `f2` from `test`.`t1` order by `test`.`t1`.`f1`,`test`.`t1`.`f2`
drop view v1;
drop table t1;
CREATE TABLE t1 (
id int(11) NOT NULL PRIMARY KEY,
country varchar(32),
code int(11) default NULL
);
INSERT INTO t1 VALUES
(1,'ITALY',100),(2,'ITALY',200),(3,'FRANCE',100), (4,'ITALY',100);
CREATE VIEW v1 AS SELECT * FROM t1;
SELECT code, COUNT(DISTINCT country) FROM t1 GROUP BY code ORDER BY MAX(id);
code	COUNT(DISTINCT country)
200	1
100	2
SELECT code, COUNT(DISTINCT country) FROM v1 GROUP BY code ORDER BY MAX(id);
code	COUNT(DISTINCT country)
200	1
100	2
DROP VIEW v1;
DROP TABLE t1;
3150 3151 3152 3153 3154 3155 3156 3157 3158 3159
DROP VIEW IF EXISTS v1;
SELECT * FROM (SELECT 1) AS t;
1
1
CREATE VIEW v1 AS SELECT * FROM (SELECT 1) AS t;
ERROR HY000: View's SELECT contains a subquery in the FROM clause
# Previously the following would fail.
SELECT * FROM (SELECT 1) AS t;
1
1
3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284
drop view if exists view_24532_a;
drop view if exists view_24532_b;
drop table if exists table_24532;
create table table_24532 (
a int,
b bigint,
c int(4),
d bigint(48)
);
create view view_24532_a as
select
a IS TRUE,
a IS NOT TRUE,
a IS FALSE,
a IS NOT FALSE,
a IS UNKNOWN,
a IS NOT UNKNOWN,
a is NULL,
a IS NOT NULL,
ISNULL(a),
b IS TRUE,
b IS NOT TRUE,
b IS FALSE,
b IS NOT FALSE,
b IS UNKNOWN,
b IS NOT UNKNOWN,
b is NULL,
b IS NOT NULL,
ISNULL(b),
c IS TRUE,
c IS NOT TRUE,
c IS FALSE,
c IS NOT FALSE,
c IS UNKNOWN,
c IS NOT UNKNOWN,
c is NULL,
c IS NOT NULL,
ISNULL(c),
d IS TRUE,
d IS NOT TRUE,
d IS FALSE,
d IS NOT FALSE,
d IS UNKNOWN,
d IS NOT UNKNOWN,
d is NULL,
d IS NOT NULL,
ISNULL(d)
from table_24532;
describe view_24532_a;
Field	Type	Null	Key	Default	Extra
a IS TRUE	int(1)	NO		0	
a IS NOT TRUE	int(1)	NO		0	
a IS FALSE	int(1)	NO		0	
a IS NOT FALSE	int(1)	NO		0	
a IS UNKNOWN	int(1)	NO		0	
a IS NOT UNKNOWN	int(1)	NO		0	
a is NULL	int(1)	NO		0	
a IS NOT NULL	int(1)	NO		0	
ISNULL(a)	int(1)	NO		0	
b IS TRUE	int(1)	NO		0	
b IS NOT TRUE	int(1)	NO		0	
b IS FALSE	int(1)	NO		0	
b IS NOT FALSE	int(1)	NO		0	
b IS UNKNOWN	int(1)	NO		0	
b IS NOT UNKNOWN	int(1)	NO		0	
b is NULL	int(1)	NO		0	
b IS NOT NULL	int(1)	NO		0	
ISNULL(b)	int(1)	NO		0	
c IS TRUE	int(1)	NO		0	
c IS NOT TRUE	int(1)	NO		0	
c IS FALSE	int(1)	NO		0	
c IS NOT FALSE	int(1)	NO		0	
c IS UNKNOWN	int(1)	NO		0	
c IS NOT UNKNOWN	int(1)	NO		0	
c is NULL	int(1)	NO		0	
c IS NOT NULL	int(1)	NO		0	
ISNULL(c)	int(1)	NO		0	
d IS TRUE	int(1)	NO		0	
d IS NOT TRUE	int(1)	NO		0	
d IS FALSE	int(1)	NO		0	
d IS NOT FALSE	int(1)	NO		0	
d IS UNKNOWN	int(1)	NO		0	
d IS NOT UNKNOWN	int(1)	NO		0	
d is NULL	int(1)	NO		0	
d IS NOT NULL	int(1)	NO		0	
ISNULL(d)	int(1)	NO		0	
create view view_24532_b as
select
a IS TRUE,
if(ifnull(a, 0), 1, 0) as old_istrue,
a IS NOT TRUE,
if(ifnull(a, 0), 0, 1) as old_isnottrue,
a IS FALSE,
if(ifnull(a, 1), 0, 1) as old_isfalse,
a IS NOT FALSE,
if(ifnull(a, 1), 1, 0) as old_isnotfalse
from table_24532;
describe view_24532_b;
Field	Type	Null	Key	Default	Extra
a IS TRUE	int(1)	NO		0	
old_istrue	int(1)	NO		0	
a IS NOT TRUE	int(1)	NO		0	
old_isnottrue	int(1)	NO		0	
a IS FALSE	int(1)	NO		0	
old_isfalse	int(1)	NO		0	
a IS NOT FALSE	int(1)	NO		0	
old_isnotfalse	int(1)	NO		0	
show create view view_24532_b;
View	Create View
view_24532_b	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `view_24532_b` AS select (`table_24532`.`a` is true) AS `a IS TRUE`,if(ifnull(`table_24532`.`a`,0),1,0) AS `old_istrue`,(`table_24532`.`a` is not true) AS `a IS NOT TRUE`,if(ifnull(`table_24532`.`a`,0),0,1) AS `old_isnottrue`,(`table_24532`.`a` is false) AS `a IS FALSE`,if(ifnull(`table_24532`.`a`,1),0,1) AS `old_isfalse`,(`table_24532`.`a` is not false) AS `a IS NOT FALSE`,if(ifnull(`table_24532`.`a`,1),1,0) AS `old_isnotfalse` from `table_24532`
insert into table_24532 values (0, 0, 0, 0);
select * from view_24532_b;
a IS TRUE	old_istrue	a IS NOT TRUE	old_isnottrue	a IS FALSE	old_isfalse	a IS NOT FALSE	old_isnotfalse
0	0	1	1	1	1	0	0
update table_24532 set a=1;
select * from view_24532_b;
a IS TRUE	old_istrue	a IS NOT TRUE	old_isnottrue	a IS FALSE	old_isfalse	a IS NOT FALSE	old_isnotfalse
1	1	0	0	0	0	1	1
update table_24532 set a=NULL;
select * from view_24532_b;
a IS TRUE	old_istrue	a IS NOT TRUE	old_isnottrue	a IS FALSE	old_isfalse	a IS NOT FALSE	old_isnotfalse
0	0	1	1	0	0	1	1
drop view view_24532_a;
drop view view_24532_b;
drop table table_24532;
unknown's avatar
unknown committed
3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321
CREATE TABLE t1 (
lid int NOT NULL PRIMARY KEY,
name char(10) NOT NULL
);
INSERT INTO t1 (lid, name) VALUES
(1, 'YES'), (2, 'NO');
CREATE TABLE t2 (
id int NOT NULL PRIMARY KEY, 
gid int NOT NULL,
lid int NOT NULL,
dt date
);
INSERT INTO t2 (id, gid, lid, dt) VALUES
(1, 1, 1, '2007-01-01'),(2, 1, 2, '2007-01-02'),
(3, 2, 2, '2007-02-01'),(4, 2, 1, '2007-02-02');
SELECT DISTINCT t2.gid AS lgid,
(SELECT t1.name FROM t1, t2
WHERE t1.lid  = t2.lid AND t2.gid = lgid
ORDER BY t2.dt DESC LIMIT 1
) as clid
FROM t2;
lgid	clid
1	NO
2	YES
CREATE VIEW v1 AS
SELECT DISTINCT t2.gid AS lgid,
(SELECT t1.name FROM t1, t2
WHERE t1.lid  = t2.lid AND t2.gid = lgid
ORDER BY t2.dt DESC LIMIT 1
) as clid
FROM t2;
SELECT * FROM v1;
lgid	clid
1	NO
2	YES
DROP VIEW v1;
DROP table t1,t2;
unknown's avatar
unknown committed
3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (1),(2),(3);
CREATE VIEW v1 AS SELECT a FROM t1 ORDER BY a;
SELECT * FROM t1 UNION SELECT * FROM v1;
a
1
2
3
EXPLAIN SELECT * FROM t1 UNION SELECT * FROM v1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	
2	UNION	t1	ALL	NULL	NULL	NULL	NULL	3	
NULL	UNION RESULT	<union1,2>	ALL	NULL	NULL	NULL	NULL	NULL	
SELECT * FROM v1 UNION SELECT * FROM t1;
a
1
2
3
EXPLAIN SELECT * FROM v1 UNION SELECT * FROM t1;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	
2	UNION	t1	ALL	NULL	NULL	NULL	NULL	3	
NULL	UNION RESULT	<union1,2>	ALL	NULL	NULL	NULL	NULL	NULL	
SELECT * FROM t1 UNION SELECT * FROM v1 ORDER BY a;
a
1
2
3
EXPLAIN SELECT * FROM t1 UNION SELECT * FROM v1 ORDER BY a;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	3	
2	UNION	t1	ALL	NULL	NULL	NULL	NULL	3	
NULL	UNION RESULT	<union1,2>	ALL	NULL	NULL	NULL	NULL	NULL	Using filesort
DROP VIEW v1;
DROP TABLE t1;
3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369
CREATE VIEW v1 AS SELECT CAST( 1.23456789 AS DECIMAL( 7,5 ) ) AS col;
SELECT * FROM v1;
col
1.23457
DESCRIBE v1;
Field	Type	Null	Key	Default	Extra
col	decimal(7,5)	NO		0.00000	
DROP VIEW v1;
CREATE VIEW v1 AS SELECT CAST(1.23456789 AS DECIMAL(8,0)) AS col;
SHOW CREATE VIEW v1;
View	Create View
v1	CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select cast(1.23456789 as decimal(8,0)) AS `col`
DROP VIEW v1;
unknown's avatar
unknown committed
3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386
CREATE TABLE t1 (a INT);
CREATE TABLE t2 (b INT, c INT DEFAULT 0);
INSERT INTO t1 (a) VALUES (1), (2);
INSERT INTO t2 (b) VALUES (1), (2);
CREATE VIEW v1 AS SELECT t2.b,t2.c FROM t1, t2
WHERE t1.a=t2.b AND t2.b < 3 WITH CHECK OPTION;
SELECT * FROM v1;
b	c
1	0
2	0
UPDATE v1 SET c=1 WHERE b=1;
SELECT * FROM v1;
b	c
1	1
2	0
DROP VIEW v1;
DROP TABLE t1,t2;
3387 3388 3389 3390 3391 3392 3393 3394 3395 3396
CREATE TABLE t1 (id int);
CREATE TABLE t2 (id int, c int DEFAULT 0);
INSERT INTO t1 (id) VALUES (1);
INSERT INTO t2 (id) VALUES (1);
CREATE VIEW v1 AS 
SELECT t2.c FROM t1, t2 
WHERE t1.id=t2.id AND 1 IN (SELECT id FROM t1) WITH CHECK OPTION;
UPDATE v1 SET c=1;
DROP VIEW v1;
DROP TABLE t1,t2;
3397
End of 5.0 tests.