view.test 58.8 KB
Newer Older
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
1
--disable_warnings
2
drop table if exists t1,t2,t3,t4,t9,`t1a``b`,v1,v2,v3,v4,v5,v6;
3
drop view if exists t1,t2,`t1a``b`,v1,v2,v3,v4,v5,v6;
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
4 5 6 7 8 9 10 11
drop database if exists mysqltest;
--enable_warnings
use test;

#
# some basic test of views and its functionality
#

serg@serg.mylan's avatar
serg@serg.mylan committed
12
# create view on nonexistent table
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
13 14 15 16
-- error 1146
create view v1 (c,d) as select a,b from t1;

create temporary table t1 (a int, b int);
serg@serg.mylan's avatar
serg@serg.mylan committed
17
# view on temporary table
18
-- error 1352
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
19 20 21
create view v1 (c) as select b+1 from t1;
drop table t1;

serg@serg.mylan's avatar
serg@serg.mylan committed
22
create table t1 (a int, b int);
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
23 24
insert into t1 values (1,2), (1,3), (2,4), (2,5), (3,10);

serg@serg.mylan's avatar
serg@serg.mylan committed
25
# view with variable
26
-- error 1351
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
27 28 29 30 31 32
create view v1 (c,d) as select a,b+@@global.max_user_connections from t1;

# simple view
create view v1 (c) as select b+1 from t1;
select c from v1;

serg@serg.mylan's avatar
serg@serg.mylan committed
33
# temporary table should not hide table of view
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
34 35 36 37 38 39 40
create temporary table t1 (a int, b int);
# this is empty
select * from t1;
# but this based on normal t1
select c from v1;
show create table v1;
show create view v1;
41
-- error 1347
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
42 43 44
show create view t1;
drop table t1;

serg@serg.mylan's avatar
serg@serg.mylan committed
45
# try to use fields from underlying table
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
46 47 48 49 50 51 52 53 54
-- error 1054
select a from v1;
-- error 1054
select v1.a from v1;
-- error 1054
select b from v1;
-- error 1054
select v1.b from v1;

serg@serg.mylan's avatar
serg@serg.mylan committed
55
# view with different algorithms (explain output differs)
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
56 57
explain extended select c from v1;
create algorithm=temptable view v2 (c) as select b+1 from t1;
58
show create view v2;
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
59 60 61
select c from v2;
explain extended select c from v2;

serg@serg.mylan's avatar
serg@serg.mylan committed
62
# try to use underlying table fields in VIEW creation process
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
-- error 1054
create view v3 (c) as select a+1 from v1;
-- error 1054
create view v3 (c) as select b+1 from v1;


# VIEW on VIEW test with mixing different algorithms on different order
create view v3 (c) as select c+1 from v1;
select c from v3;
explain extended select c from v3;
create algorithm=temptable view v4 (c) as select c+1 from v2;
select c from v4;
explain extended select c from v4;
create view v5 (c) as select c+1 from v2;
select c from v5;
explain extended select c from v5;
create algorithm=temptable view v6 (c) as select c+1 from v1;
select c from v6;
explain extended select c from v6;

# show table/table status test
show tables;
85
show full tables;
86
--replace_column 8 # 12 # 13 #
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
show table status;

drop view v1,v2,v3,v4,v5,v6;

#
# alter/create view test
#

# view with subqueries of different types
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;
select * from v2;

# try to create VIEW with name of existing VIEW
-- error 1050
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;

# 'or replace' should work in this case
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;

# try to ALTER unexisting VIEW
drop view v2;
-- error 1146
alter view v2 as select c, d from v1;

# 'or replace' on unexisting view
create or replace view v2 as select c, d from v1;

# alter view on existing view
alter view v1 (c,d) as select a,max(b) from t1 group by a;

# check that created view works
select * from v1;
select * from v2;

serg@serg.mylan's avatar
serg@serg.mylan committed
124
# try to drop nonexistent VIEW
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
125 126 127
-- error 1051
drop view v100;

serg@serg.mylan's avatar
serg@serg.mylan committed
128
# try to drop table with DROP VIEW
129
-- error 1347
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
130 131
drop view t1;

serg@serg.mylan's avatar
serg@serg.mylan committed
132
# try to drop VIEW with DROP TABLE
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
133 134 135
-- error 1051
drop table v1;

serg@serg.mylan's avatar
serg@serg.mylan committed
136
# try to drop table with DROP VIEW
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
137 138 139 140 141 142 143 144 145 146 147 148 149

drop view v1,v2;
drop table t1;

#
# outer left join with merged views
#
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;

150
--disable_parsing # WL #2486 should enable these tests
151 152 153 154
select * from t1 natural left join v1;
select * from v2 natural left join t1;
select * from v2 natural left join v1;
--enable_parsing
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

drop view v1, v2;
drop table t1;


#
# DISTINCT option for VIEW
#
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;
explain select * from v1;
select * from t1;
drop view v1;
drop table t1;

#
# syntax compatibility
#
create table t1 (a int);
monty@mishka.local's avatar
monty@mishka.local committed
176
-- error 1368
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
177
create view v1 as select distinct a from t1 WITH CHECK OPTION;
178 179 180
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;
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
drop view v3 RESTRICT;
drop view v2 CASCADE;
drop view v1;
drop table t1;

#
# aliases
#
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;
create algorithm=temptable view v2 (c) as select b+1 from t1;
select test.c from v2 test;
select test1.* from v1 test1, v2 test2 where test1.c=test2.c;
select test2.* from v1 test1, v2 test2 where test1.c=test2.c;
drop table t1;
drop view v1,v2;

#
serg@serg.mylan's avatar
serg@serg.mylan committed
201
# LIMIT clause test
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
#
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;
explain select * from v1;
drop view v1;
drop table t1;

#
# CREATE ... SELECT view test
#
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;
select * from t2;
drop view v1;
drop table t1,t2;

#
# simple view + simple update
#
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;
# try to update expression
231
-- error 1348
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
update v1 set c=a+c;
# try to update VIEW with forced TEMPORARY TABLE algorithm
-- error 1288
update v2 set a=a+c;
# updatable field of updateable view
update v1 set a=a+c;
select * from v1;
select * from t1;
drop table t1;
drop view v1,v2;

#
# simple view + simple multi-update
#
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;
# try to update expression
253
-- error 1348
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
254 255 256 257 258 259 260 261 262 263 264 265
update t2,v1 set v1.c=v1.a+v1.c where t2.x=v1.a;
# try to update VIEW with forced TEMPORARY TABLE algorithm
-- error 1288
update t2,v2 set v2.a=v2.v2.a+c where t2.x=v2.a;
# updatable field of updateable view
update t2,v1 set v1.a=v1.a+v1.c where t2.x=v1.a;
select * from v1;
select * from t1;
drop table t1,t2;
drop view v1,v2;

#
serg@serg.mylan's avatar
serg@serg.mylan committed
266
# MERGE VIEW with WHERE clause
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
#
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;
# simple select and explaint to be sure that it is MERGE
select * from v1;
explain extended select * from v1;
# update test
update v1 set c=c+1;
select * from t1;
# join of such VIEWs test
create view v2 (c) as select b from t1 where a>=3;
select * from v1, v2;
drop view v1, v2;
drop table t1;

#
# simple view + simple delete
#
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;
# try to update VIEW with forced TEMPORARY TABLE algorithm
-- error 1288
delete from v2 where c < 4;
# updatable field of updateable view
delete from v1 where c < 4;
select * from v1;
select * from t1;
drop table t1;
drop view v1,v2;

#
# simple view + simple multi-delete
#
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;
# try to update VIEW with forced TEMPORARY TABLE algorithm
-- error 1288
delete v2 from t2,v2 where t2.x=v2.a;
# updatable field of updateable view
delete v1 from t2,v1 where t2.x=v1.a;
select * from v1;
select * from t1;
drop table t1,t2;
drop view v1,v2;

#
# key presence check
#
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;
326
set updatable_views_with_limit=NO;
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
327 328 329 330 331
update v1 set x=x+1;
update v2 set x=x+1;
update v1 set x=x+1 limit 1;
-- error 1288
update v2 set x=x+1 limit 1;
332
set updatable_views_with_limit=YES;
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
333 334
update v1 set x=x+1 limit 1;
update v2 set x=x+1 limit 1;
335 336
set updatable_views_with_limit=DEFAULT;
show variables like "updatable_views_with_limit";
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
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 382 383 384 385 386 387 388 389 390 391
select * from t1;
drop table t1;
drop view v1,v2;

#
# simple insert
#
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;
# try insert to VIEW with fields duplicate
-- error 1288
insert into v3 values (-60,4,30);
# try insert to VIEW with expression in SELECT list
-- error 1288
insert into v4 values (-60,4,30);
# try insert to VIEW using temporary table algorithm
-- error 1288
insert into v5 values (-60,4,30);
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;
drop table t1;
drop view v1,v2,v3,v4,v5;

#
# insert ... select
#
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;
# try insert to VIEW with fields duplicate
-- error 1288
insert into v3 select c, b, a from t2;
# try insert to VIEW with expression in SELECT list
-- error 1288
insert into v4 select c, b, a from t2;
# try insert to VIEW using temporary table algorithm
-- error 1288
insert into v5 select c, b, a from t2;
insert into v1 select c, b, a from t2;
insert into v1 (z,y,x) select a+20,b+2,-100 from t2;
insert into v2 select b+1, a+10 from t2;
select * from t1;
392
drop table t1, t2;
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
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
drop view v1,v2,v3,v4,v5;

#
# outer join based on VIEW with WHERE clause
#
create table t1 (a int, primary key(a));
insert into t1 values (1), (2), (3);
create view v1 (x) as select a from t1 where a > 1;
select t1.a, v1.x from t1 left join v1 on (t1.a= v1.x);
drop table t1;
drop view v1;

#
# merging WHERE condition on VIEW on VIEW
#
create table t1 (a int, primary key(a));
insert into t1 values (1), (2), (3), (200);
create view v1 (x) as select a from t1 where a > 1;
create view v2 (y) as select x from v1 where x < 100;
select * from v2;
drop table t1;
drop view v1,v2;

#
# VIEW on non-updatable view
#
create table t1 (a int, primary key(a));
insert into t1 values (1), (2), (3), (200);
create ALGORITHM=TEMPTABLE view v1 (x) as select a from t1;
create view v2 (y) as select x from v1;
-- error 1288
update v2 set y=10 where y=2;
drop table t1;
drop view v1,v2;

#
# auto_increment field out of VIEW
#
create table t1 (a int not null auto_increment, b int not null, primary key(a), unique(b));
create view v1 (x) as select b from t1;
insert into v1 values (1);
select last_insert_id();
insert into t1 (b) values (2);
select last_insert_id();
select * from t1;
drop view v1;
drop table t1;

441 442 443 444 445 446 447 448 449 450
#
# VIEW fields quoting
#
set sql_mode='ansi';
create table t1 ("a*b" int);
create view v1 as select "a*b" from t1;
show create view v1;
drop view v1;
drop table t1;
set sql_mode=default;
451 452 453 454 455 456 457 458 459

#
# VIEW without tables
#
create table t1 (t_column int);
create view v1 as select 'a';
select * from v1, t1;
drop view v1;
drop table t1;
460 461 462 463 464 465 466 467 468 469

#
# quote mark inside table name
#
create table `t1a``b` (col1 char(2));
create view v1 as select * from `t1a``b`;
select * from v1;
describe v1;
drop view v1;
drop table `t1a``b`;
470 471

#
serg@serg.mylan's avatar
serg@serg.mylan committed
472
# Changing of underlying table
473 474 475 476 477
#
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));
478
-- error 1356
479 480
insert into v1 values('a','aa');
drop table t1;
481
-- error 1356
482 483
select * from v1;
drop view v1;
484 485 486 487 488 489

#
# check of duplication of column names
#
-- error 1060
create view v1 (a,a) as select 'a','a';
490

491 492 493
#
# SP variables inside view test
#
494 495 496 497 498 499 500 501 502
--disable_warnings
drop procedure if exists p1;
--enable_warnings
delimiter //;
create procedure p1 () begin declare v int; create view v1 as select v; end;//
delimiter ;//
-- error 1351
call p1();
drop procedure p1;
503 504

#
serg@serg.mylan's avatar
serg@serg.mylan committed
505
# updatablity should be transitive
506 507 508 509 510 511 512 513 514
#
create table t1 (col1 int,col2 char(22));
insert into t1 values(5,'Hello, world of views');
create view v1 as select * from t1;
create view v2 as select * from v1;
update v2 set col2='Hello, view world';
select * from t1;
drop view v2, v1;
drop table t1;
bell@sanja.is.com.ua's avatar
Merge  
bell@sanja.is.com.ua committed
515

516 517 518 519 520 521 522 523 524
#
# check 'use index' on view with temporary table
#
create table t1 (a int, b int);
create view v1 as select a, sum(b) from t1 group by a;
-- error 1072
select b from v1 use index (some_index) where b=1;
drop view v1;
drop table t1;
bell@sanja.is.com.ua's avatar
merge  
bell@sanja.is.com.ua committed
525

526 527 528 529 530 531 532 533 534 535
#
# using VIEW fields several times in query resolved via temporary tables
#
create table t1 (col1 char(5),col2 char(5));
create view v1 (col1,col2) as select col1,col2 from t1;
insert into v1 values('s1','p1'),('s1','p2'),('s1','p3'),('s1','p4'),('s2','p1'),('s3','p2'),('s4','p4');
select distinct first.col2 from t1 first where first.col2 in (select second.col2 from t1 second where second.col1<>first.col1);
select distinct first.col2 from v1 first where first.col2 in (select second.col2 from t1 second where second.col1<>first.col1);
drop view v1;
drop table t1;
536 537

#
serg@serg.mylan's avatar
serg@serg.mylan committed
538
# Test of view updatability in prepared statement
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
#
create table t1 (a int);
create view v1 as select a from t1;
insert into t1 values (1);

#update
SET @v0 = '2';
PREPARE stmt FROM 'UPDATE v1 SET a = ?';
EXECUTE stmt USING @v0;
DEALLOCATE PREPARE stmt;

#insert without field list
SET @v0 = '3';
PREPARE stmt FROM 'insert into v1 values (?)';
EXECUTE stmt USING @v0;
DEALLOCATE PREPARE stmt;

#insert with field list
SET @v0 = '4';
PREPARE stmt FROM 'insert into v1 (a) values (?)';
EXECUTE stmt USING @v0;
DEALLOCATE PREPARE stmt;

select * from t1;

drop view v1;
drop table t1;
566 567 568 569 570 571 572

#
# error on preparation
#
-- error 1096
CREATE VIEW v02 AS SELECT * FROM DUAL;
SHOW TABLES;
573 574 575 576 577 578 579

#
# EXISTS with UNION VIEW
#
CREATE VIEW v1 AS SELECT EXISTS (SELECT 1 UNION SELECT 2);
select * from v1;
drop view v1;
580 581 582 583 584 585

#
# using VIEW where table is required
#
create table t1 (col1 int,col2 char(22));
create view v1 as select * from t1;
586
-- error 1347
587 588 589
create index i1 on v1 (col1);
drop view v1;
drop table t1;
590 591 592 593 594 595 596

#
# connection_id(), pi(), current_user(), version() representation test
#
CREATE VIEW v1 (f1,f2,f3,f4) AS SELECT connection_id(), pi(), current_user(), version();
SHOW CREATE VIEW v1;
drop view v1;
597 598 599 600 601 602 603 604 605 606 607 608

#
# VIEW built over UNION
#
create table t1 (s1 int);
create table t2 (s2 int);
insert into t1 values (1), (2);
insert into t2 values (2), (3);
create view v1 as select * from t1,t2 union all select * from t1,t2;
select * from v1;
drop view v1;
drop tables t1, t2;
609 610 611 612 613 614 615 616 617 618 619

#
# Aggregate functions in view list
#
create table t1 (col1 int);
insert into t1 values (1);
create view v1 as select count(*) from t1;
insert into t1 values (null);
select * from v1;
drop view v1;
drop table t1;
620 621 622 623 624 625 626 627 628 629 630

#
# Showing VIEW with VIEWs in subquery
#
create table t1 (a int);
create table t2 (a int);
create view v1 as select a from t1;
create view v2 as select a from t2 where a in (select a from v1);
show create view v2;
drop view v2, v1;
drop table t1, t2;
631 632 633 634 635 636 637

#
# SHOW VIEW view with name with spaces
#
CREATE VIEW `v 1` AS select 5 AS `5`;
show create view `v 1`;
drop view `v 1`;
638 639 640 641 642 643 644 645 646 647

#
# Removing database with .frm archives
#
create database mysqltest;
create table mysqltest.t1 (a int, b int);
create view mysqltest.v1 as select a from mysqltest.t1;
alter view mysqltest.v1 as select b from mysqltest.t1;
alter view mysqltest.v1 as select a from mysqltest.t1;
drop database mysqltest;
648 649 650 651 652 653 654 655 656 657 658

#
# VIEW with full text
#
CREATE TABLE t1 (c1 int not null auto_increment primary key, c2 varchar(20), fulltext(c2));
insert into t1 (c2) VALUES ('real Beer'),('Water'),('Kossu'),('Coca-Cola'),('Vodka'),('Wine'),('almost real Beer');
select * from t1 WHERE match (c2) against ('Beer');
CREATE VIEW v1 AS SELECT  * from t1 WHERE match (c2) against ('Beer');
select * from v1;
drop view v1;
drop table t1;
659 660 661 662 663 664 665 666 667 668

#
# distinct in temporary table with a VIEW
#
create table t1 (a int);
insert into t1 values (1),(1),(2),(2),(3),(3);
create view v1 as select a from t1;
select distinct a from v1;
select distinct a from v1 limit 2;
select distinct a from t1 limit 2;
669 670 671 672 673 674 675 676 677 678 679 680 681
prepare stmt1 from "select distinct a from v1 limit 2";
execute stmt1;
execute stmt1;
deallocate prepare stmt1;
drop view v1;
drop table t1;

#
# aggregate function of aggregate function
#
create table t1 (tg_column bigint);
create view v1 as select count(tg_column) as vg_column from t1;
select avg(vg_column) from v1;
682 683
drop view v1;
drop table t1;
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698

#
# VIEW of VIEW with column renaming
#
create table t1 (col1 bigint not null, primary key (col1));
create table t2 (col1 bigint not null, key (col1));
create view v1 as select * from t1;
create view v2 as select * from t2;
insert into v1 values (1);
insert into v2 values (1);
create view v3 (a,b) as select v1.col1 as a, v2.col1 as b from v1, v2 where v1.col1 = v2.col1;
select * from v3;
show create view v3;
drop view v3, v2, v1;
drop table t2, t1;
699 700 701 702 703 704 705 706 707 708

#
# VIEW based on functions with  complex names
#
create function `f``1` () returns int return 5;
create view v1 as select test.`f``1` ();
show create view v1;
select * from v1;
drop view v1;
drop function `f``1`;
709 710 711 712 713 714 715 716 717

#
# tested problem when function name length close to ALIGN_SIZE
#
create function x () returns int return 5;
create view v1 as select x ();
select * from v1;
drop view v1;
drop function x;
718 719 720 721 722 723 724 725 726 727

#
# VIEW with collation
#
create table t2 (col1 char collate latin1_german2_ci);
create view v2 as select col1 collate latin1_german1_ci from t2;
show create view v2;
show create view v2;
drop view v2;
drop table t2;
728 729 730 731 732 733 734 735 736 737

#
# order by refers on integer field
#
create table t1 (a int);
insert into t1 values (1), (2);
create view v1 as select 5 from t1 order by 1;
select * from v1;
drop view v1;
drop table t1;
738 739

#
serg@serg.mylan's avatar
serg@serg.mylan committed
740
# VIEW over dropped function
741 742
#
create function x1 () returns int return 5;
serg@serg.mylan's avatar
serg@serg.mylan committed
743
create table t1 (s1 int);
744 745
create view v1 as select x1() from t1;
drop function x1;
746
-- error 1356
747
select * from v1;
748
--replace_column 8 # 12 # 13 #
749 750 751
show table status;
drop view v1;
drop table t1;
752 753

#
serg@serg.mylan's avatar
serg@serg.mylan committed
754
# VIEW with floating point (long number) as column
755 756 757 758
#
create view v1 as select 99999999999999999999999999999999999999999999999999999 as col1;
show create view v1;
drop view v1;
759 760 761 762 763 764 765 766 767 768

#
# VIEWs with national characters
#
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;
769 770 771 772 773 774 775 776 777 778

#
# problem with used_tables() of outer reference resolved in VIEW
#
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);
drop view v1;
drop table t1;
779 780 781 782 783 784 785 786

#
# view with cast operation
#
create view v1 as select cast(1 as char(3));
show create view v1;
select * from v1;
drop view v1;
787

788 789 790 791 792
#
# renaming views
#
create table t1 (a int);
create view v1 as select a from t1;
monty@mysql.com's avatar
monty@mysql.com committed
793 794
create view v3 as select a from t1;
create database mysqltest;
795
-- error 1450
monty@mysql.com's avatar
monty@mysql.com committed
796
rename table v1 to mysqltest.v1;
797
rename table v1 to v2;
monty@mysql.com's avatar
monty@mysql.com committed
798 799
--error 1050
rename table v3 to v1, v2 to t1;
800
drop table t1;
monty@mysql.com's avatar
monty@mysql.com committed
801 802
drop view v2,v3;
drop database mysqltest;
803

804
#
serg@serg.mylan's avatar
serg@serg.mylan committed
805
# bug handling from VIEWs
806 807 808 809 810 811 812 813
#
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;
-- error 1242
select * from v4;
drop view v4, v3, v2, v1;
814 815 816 817

#
# VIEW over SELECT with prohibited clauses
#
monty@mysql.com's avatar
monty@mysql.com committed
818
-- error 1350
819
create view v1 as select 5 into @w;
monty@mysql.com's avatar
monty@mysql.com committed
820
-- error 1350
821 822
create view v1 as select 5 into outfile 'ttt';
create table t1 (a int);
monty@mysql.com's avatar
monty@mysql.com committed
823
-- error 1350
824 825
create view v1 as select a from t1 procedure analyse();
drop table t1;
826 827 828 829 830 831 832 833 834 835 836

#
# INSERT into VIEW with ON DUPLICATE
#
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;
drop view v1;
drop table t1;
837

838 839 840 841 842 843 844
#
# test of updating and fetching from the same table check
#
create table t1 (col1 int);
create table t2 (col1 int);
create view v1 as select * from t1;
create view v2 as select * from v1;
845
create view v3 as select v2.col1 from v2,t2 where v2.col1 = t2.col1;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
846
-- error 1443
847
update v2 set col1 = (select max(col1) from v1);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
848
-- error 1443
849 850 851
update v2 set col1 = (select max(col1) from t1);
-- error 1093
update v2 set col1 = (select max(col1) from v2);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
852
-- error 1443
853
update v2,t2 set v2.col1 = (select max(col1) from v1) where v2.col1 = t2.col1;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
854
-- error 1443
855 856 857
update t1,t2 set t1.col1 = (select max(col1) from v1) where t1.col1 = t2.col1;
-- error 1093
update v1,t2 set v1.col1 = (select max(col1) from v1) where v1.col1 = t2.col1;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
858
-- error 1443
859
update t2,v2 set v2.col1 = (select max(col1) from v1) where v2.col1 = t2.col1;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
860
-- error 1443
861
update t2,t1 set t1.col1 = (select max(col1) from v1) where t1.col1 = t2.col1;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
862
-- error 1443
863
update t2,v1 set v1.col1 = (select max(col1) from v1) where v1.col1 = t2.col1;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
864
-- error 1443
865 866 867
update v2,t2 set v2.col1 = (select max(col1) from t1) where v2.col1 = t2.col1;
-- error 1093
update t1,t2 set t1.col1 = (select max(col1) from t1) where t1.col1 = t2.col1;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
868
-- error 1443
869 870 871 872 873 874 875 876 877
update v1,t2 set v1.col1 = (select max(col1) from t1) where v1.col1 = t2.col1;
-- error 1093
update t2,v2 set v2.col1 = (select max(col1) from t1) where v2.col1 = t2.col1;
-- error 1093
update t2,t1 set t1.col1 = (select max(col1) from t1) where t1.col1 = t2.col1;
-- error 1093
update t2,v1 set v1.col1 = (select max(col1) from t1) where v1.col1 = t2.col1;
-- error 1093
update v2,t2 set v2.col1 = (select max(col1) from v2) where v2.col1 = t2.col1;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
878
-- error 1443
879
update t1,t2 set t1.col1 = (select max(col1) from v2) where t1.col1 = t2.col1;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
880
-- error 1443
881
update v1,t2 set v1.col1 = (select max(col1) from v2) where v1.col1 = t2.col1;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
882
-- error 1443
883
update t2,v2 set v2.col1 = (select max(col1) from v2) where v2.col1 = t2.col1;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
884
-- error 1443
885
update t2,t1 set t1.col1 = (select max(col1) from v2) where t1.col1 = t2.col1;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
886
-- error 1443
887
update t2,v1 set v1.col1 = (select max(col1) from v2) where v1.col1 = t2.col1;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
888
-- error 1443
889
update v3 set v3.col1 = (select max(col1) from v1);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
890
-- error 1443
891
update v3 set v3.col1 = (select max(col1) from t1);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
892
-- error 1443
893 894 895
update v3 set v3.col1 = (select max(col1) from v2);
-- error 1093
update v3 set v3.col1 = (select max(col1) from v3);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
896
-- error 1443
897
delete from v2 where col1 = (select max(col1) from v1);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
898
-- error 1443
899 900 901
delete from v2 where col1 = (select max(col1) from t1);
-- error 1093
delete from v2 where col1 = (select max(col1) from v2);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
902
-- error 1443
903
delete v2 from v2,t2 where (select max(col1) from v1) > 0 and v2.col1 = t2.col1;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
904
-- error 1443
905 906 907
delete t1 from t1,t2 where (select max(col1) from v1) > 0 and t1.col1 = t2.col1;
-- error 1093
delete v1 from v1,t2 where (select max(col1) from v1) > 0 and v1.col1 = t2.col1;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
908
-- error 1443
909 910 911
delete v2 from v2,t2 where (select max(col1) from t1) > 0 and v2.col1 = t2.col1;
-- error 1093
delete t1 from t1,t2 where (select max(col1) from t1) > 0 and t1.col1 = t2.col1;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
912
-- error 1443
913 914 915
delete v1 from v1,t2 where (select max(col1) from t1) > 0 and v1.col1 = t2.col1;
-- error 1093
delete v2 from v2,t2 where (select max(col1) from v2) > 0 and v2.col1 = t2.col1;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
916
-- error 1443
917
delete t1 from t1,t2 where (select max(col1) from v2) > 0 and t1.col1 = t2.col1;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
918
-- error 1443
919
delete v1 from v1,t2 where (select max(col1) from v2) > 0 and v1.col1 = t2.col1;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
920
-- error 1443
921
insert into v2 values ((select max(col1) from v1));
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
922
-- error 1443
923
insert into t1 values ((select max(col1) from v1));
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
924
-- error 1443
925
insert into v2 values ((select max(col1) from v1));
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
926
-- error 1443
927 928 929
insert into v2 values ((select max(col1) from t1));
-- error 1093
insert into t1 values ((select max(col1) from t1));
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
930
-- error 1443
931 932 933
insert into v2 values ((select max(col1) from t1));
-- error 1093
insert into v2 values ((select max(col1) from v2));
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
934
-- error 1443
935 936 937
insert into t1 values ((select max(col1) from v2));
-- error 1093
insert into v2 values ((select max(col1) from v2));
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
938
-- error 1443
939
insert into v3 (col1) values ((select max(col1) from v1));
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
940
-- error 1443
941
insert into v3 (col1) values ((select max(col1) from t1));
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
942
-- error 1443
943 944
insert into v3 (col1) values ((select max(col1) from v2));
#check with TZ tables in list
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
945
-- error 1443
946 947 948 949 950 951 952 953 954 955 956
insert into v3 (col1) values ((select CONVERT_TZ('20050101000000','UTC','MET') from v2));
insert into v3 (col1) values ((select CONVERT_TZ('20050101000000','UTC','MET') from t2));
-- error 1048
insert into mysql.time_zone values ('', (select CONVERT_TZ('20050101000000','UTC','MET') from t2));
# temporary table algorithm view should be equal to subquery in the from clause
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;

drop view v4,v3,v2,v1;
957
drop table t1,t2;
bell@sanja.is.com.ua's avatar
Merge  
bell@sanja.is.com.ua committed
958

959 960 961 962 963
#
# HANDLER with VIEW
#
create table t1 (s1 int);
create view v1 as select * from t1;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
964
-- error 1347
965 966 967
handler v1 open as xx;
drop view v1;
drop table t1;
968 969 970 971 972 973 974 975 976 977 978 979

#
# view with WHERE in nested join
#
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;
select * from t1 left join (t2 as t, t2) on t2.a=t1.a;
drop view v1;
980
drop table t1, t2;
981 982 983 984 985 986 987 988 989 990 991 992

#
# Collation with view update
#
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;
update v1 set s1='b';
select * from v1;
update v1,t1 set v1.s1='c' where t1.s1=v1.s1;
select * from v1;
993 994 995 996 997 998 999 1000
prepare stmt1 from "update v1,t1 set v1.s1=? where t1.s1=v1.s1";
set @arg='d';
execute stmt1 using @arg;
select * from v1;
set @arg='e';
execute stmt1 using @arg;
select * from v1;
deallocate prepare stmt1;
1001 1002
drop view v1;
drop table t1;
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015

#
# test view with LOCK TABLES (work around)
#
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;
-- error 1100
select * from t2;
drop view v1;
drop table t1, t2;
bell@sanja.is.com.ua's avatar
merge  
bell@sanja.is.com.ua committed
1016

1017 1018 1019 1020 1021 1022 1023
#
# WITH CHECK OPTION insert/update test
#
create table t1 (a int);
create view v1 as select * from t1 where a < 2 with check option;
# simple insert
insert into v1 values(1);
monty@mishka.local's avatar
monty@mishka.local committed
1024
-- error 1369
1025 1026 1027 1028 1029 1030 1031 1032
insert into v1 values(3);
# simple insert with ignore
insert ignore into v1 values (2),(3),(0);
select * from t1;
# prepare data for next check
delete from t1;
# INSERT SELECT test
insert into v1 SELECT 1;
monty@mishka.local's avatar
monty@mishka.local committed
1033
-- error 1369
1034 1035 1036 1037 1038 1039 1040 1041 1042
insert into v1 SELECT 3;
# prepare data for next check
create table t2 (a int);
insert into t2 values (2),(3),(0);
# INSERT SELECT with ignore test
insert ignore into v1 SELECT a from t2;
select * from t1;
#simple UPDATE test
update v1 set a=-1 where a=0;
monty@mishka.local's avatar
monty@mishka.local committed
1043
-- error 1369
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
update v1 set a=2 where a=1;
select * from t1;
# prepare data for next check
update v1 set a=0 where a=0;
insert into t2 values (1);
# multiupdate test
update v1,t2 set v1.a=v1.a-1 where v1.a=t2.a;
select * from t1;
# prepare data for next check
update v1 set a=a+1;
# multiupdate with ignore test
update ignore v1,t2 set v1.a=v1.a+1 where v1.a=t2.a;
select * from t1;

drop view v1;
drop table t1, t2;

#
# CASCADED/LOCAL CHECK OPTION test
#
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);
monty@mishka.local's avatar
monty@mishka.local committed
1070
-- error 1369
1071
insert into v2 values (0);
monty@mishka.local's avatar
monty@mishka.local committed
1072
-- error 1369
1073 1074
insert into v3 values (0);
insert into v2 values (2);
monty@mishka.local's avatar
monty@mishka.local committed
1075
-- error 1369
1076 1077 1078 1079
insert into v3 values (2);
select * from t1;
drop view v3,v2,v1;
drop table t1;
1080 1081 1082 1083 1084 1085 1086

#
# CHECK OPTION with INSERT ... ON DUPLICATE KEY UPDATE
#
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;
monty@mishka.local's avatar
monty@mishka.local committed
1087
-- error 1369
1088 1089 1090 1091 1092
insert into v1 values (1) on duplicate key update a=2;
insert ignore into v1 values (1) on duplicate key update a=2;
select * from t1;
drop view v1;
drop table t1;
1093 1094 1095 1096 1097 1098 1099 1100 1101

#
# check cyclic referencing protection on altering view
#
create table t1 (s1 int);
create view v1 as select * from t1;
create view v2 as select * from v1;
-- error 1146
alter view v1 as select * from v2;
timour@mysql.com's avatar
timour@mysql.com committed
1102
-- error 1146
1103 1104 1105
alter view v1 as select * from v1;
-- error 1146
create or replace view v1 as select * from v2;
timour@mysql.com's avatar
timour@mysql.com committed
1106
-- error 1146
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
create or replace view v1 as select * from v1;
drop view v2,v1;
drop table t1;

#
# check altering differ options
#
create table t1 (a int);
create view v1 as select * from t1;
show create view v1;
alter algorithm=undefined view v1 as select * from t1 with check option;
show create view v1;
alter algorithm=merge view v1 as select * from t1 with cascaded check option;
show create view v1;
alter algorithm=temptable view v1 as select * from t1;
show create view v1;
drop view v1;
drop table t1;
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154

#
# updating view with subquery in the WHERE clause
#
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;
update v2 set s1 = 0;
select * from v2;
select * from t2;
# check it with check option
alter view v2 as select * from t2 where s1 in (select s1 from t1) with check option;
insert into v2 values (5);
-- error 1369
update v2 set s1 = 1;
insert into t1 values (1);
update v2 set s1 = 1;
select * from v2;
select * from t2;
# scheck how VIEWs with subqueries work with prepared statements
prepare stmt1 from "select * from v2;";
execute stmt1;
insert into t1 values (0);
execute stmt1;
deallocate prepare stmt1;
drop view v2;
drop table t1, t2;
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165

#
# test of substring_index with view
#
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;
select substring_index(t,':',2) from v1;
drop view v1;
drop table t1;
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176

#
# test of cascaded check option for whiew without WHERE clause
#
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;
-- error 1369
insert into v2 values (0);
drop view v2, v1;
drop table t1;
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191

#
# inserting single value with check option failed always get error
#
create table t1 (s1 int);
create view v1 as select * from t1 where s1 < 5 with check option;
#single value
-- error 1369
insert ignore into v1 values (6);
#several values
insert ignore into v1 values (6),(3);
select * from t1;
drop view v1;
drop table t1;

1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
#
# changing value by trigger and CHECK OPTION
#
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;
-- error 1369
insert into v1 values (0);
select * from v1;
select * from t1;
1202
drop trigger t1_bi;
1203 1204 1205
drop view v1;
drop table t1;

1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
#
# CASCADED should be used for all underlaying VIEWs
#
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;
-- error 1369
insert into v2 values (0);
select * from v2;
select * from t1;
drop view v2, v1;
drop table t1;
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 1243 1244 1245 1246 1247 1248

#
# LOAD DATA with view and CHECK OPTION
#
# fixed length fields
create table t1 (a int, b char(10));
create view v1 as select * from t1 where a != 0 with check option;
-- error 1369
load data infile '../../std_data/loaddata3.dat' into table v1 fields terminated by '' enclosed by '' ignore 1 lines;
select * from t1;
select * from v1;
delete from t1;
load data infile '../../std_data/loaddata3.dat' ignore into table v1 fields terminated by '' enclosed by '' ignore 1 lines;
select * from t1;
select * from v1;
drop view v1;
drop table t1;
# variable length fields
create table t1 (a text, b text);
create view v1 as select * from t1 where a <> 'Field A' with check option;
-- error 1369
load data infile '../../std_data/loaddata2.dat' into table v1 fields terminated by ',' enclosed by '''';
select concat('|',a,'|'), concat('|',b,'|') from t1;
select concat('|',a,'|'), concat('|',b,'|') from v1;
delete from t1;
load data infile '../../std_data/loaddata2.dat' ignore into table v1 fields terminated by ',' enclosed by '''';
select concat('|',a,'|'), concat('|',b,'|') from t1;
select concat('|',a,'|'), concat('|',b,'|') from v1;
drop view v1;
drop table t1;

1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
#
# Trys update table from which we select using views and subqueries
#
create table t1 (s1 smallint);
create view v1 as select * from t1 where 20 < (select (s1) from t1);
-- error 1288
insert into v1 values (30);
create view v2 as select * from t1;
create view v3 as select * from t1 where 20 < (select (s1) from v2);
-- error 1288
insert into v3 values (30);
create view v4 as select * from v2 where 20 < (select (s1) from t1);
1261
-- error 1288
1262 1263 1264
insert into v4 values (30);
drop view v4, v3, v2, v1;
drop table t1;
bell@sanja.is.com.ua's avatar
merge  
bell@sanja.is.com.ua committed
1265

1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
#
# CHECK TABLE with VIEW
#
create table t1 (a int);
create view v1 as select * from t1;
check table t1,v1;
check table v1,t1;
drop table t1;
check table v1;
drop view v1;
bell@sanja.is.com.ua's avatar
merge  
bell@sanja.is.com.ua committed
1276

1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
#
# merge of VIEW with several tables
#
create table t1 (a int);
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);
# view over tables
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);
explain extended select * from t3 left join v3 on (t3.a = v3.a);
# view over views
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);
explain extended select * from t3 left join v4 on (t3.a = v4.a);
# PS with view over views
prepare stmt1 from "select * from t3 left join v4 on (t3.a = v4.a);";
execute stmt1;
execute stmt1;
deallocate prepare stmt1;
drop view v4,v3,v2,v1;
drop tables t1,t2,t3;
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317

#
# updating of join view
#
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);
# legal view for update
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;
select * from t2;
# view without primary key
create view v2 (a,b) as select t1.b as a, t2.a as b from t1, t2;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1318
set updatable_views_with_limit=NO;
1319
-- error 1288
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1320 1321
update v2 set a= 10 where a=200 limit 1;
set updatable_views_with_limit=DEFAULT;
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
# just view selects
select * from v3;
select * from v2;
# prepare statement with updating join view
set @a= 10;
set @b= 100;
prepare stmt1 from "update v3 set a= ? where a=?";
execute stmt1 using @a,@b;
select * from v3;
set @a= 300;
set @b= 10;
execute stmt1 using @a,@b;
select * from v3;
deallocate prepare stmt1;
drop view v3,v2;
drop tables t1,t2;

#
# inserting/deleting join view
#
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;
# inserting into join view without field list
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1347
-- error 1394
1348
insert into v3 values (1,2);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1349
-- error 1394
1350 1351
insert into v3 select * from t2;
# inserting in several tables of join view
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1352
-- error 1393
1353
insert into v3(a,b) values (1,2);
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1354
-- error 1393
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
insert into v3(a,b) select * from t2;
# correct inserts into join view
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;
insert into v3(a) values (1) on duplicate key update a=a+10000+VALUES(a);
select * from t1;
select * from t2;
# try delete from join view
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1365
-- error 1395
1366
delete from v3;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1367
-- error 1395
1368
delete v3,t1 from v3,t1;
1369 1370
-- error 1395
delete t1,v3 from t1,v3;
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
# delete from t1 just to reduce result set size
delete from t1;
# prepare statement with insert join view
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;

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1388
drop view v3;
1389
drop tables t1,t2;
1390

bell@sanja.is.com.ua's avatar
merge  
bell@sanja.is.com.ua committed
1391
#
1392
# View field names should be case insensitive 
bell@sanja.is.com.ua's avatar
merge  
bell@sanja.is.com.ua committed
1393
#
1394 1395 1396 1397 1398
create table t1(f1 int);
create view v1 as select f1 from t1;
select * from v1 where F1 = 1;
drop view v1;
drop table t1;
bell@sanja.is.com.ua's avatar
merge  
bell@sanja.is.com.ua committed
1399

1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
#
# Resolving view fields in subqueries in VIEW (Bug #6394)
#
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);
SELECT c1 FROM t1 WHERE EXISTS (SELECT c2 FROM t2 WHERE c2 = c1);
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;
select * from v2;
1413
select * from (select c1 from v2) X;
1414 1415
drop view v2, v1;
drop table t1, t2;
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426

#
# view over other view setup (BUG#7433)
#
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;
drop view v2, v1;
drop table t1, t2;
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437

#
# view and group_concat() (BUG#7116)
#
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;
select col1,group_concat(col2,col3) from v1 group by col1;
drop view v1;
drop table t1;
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449

#
# Item_ref resolved as view field (BUG#6894)
#
create table t1 (s1 int, s2 char);
create view v1 as select s1, s2 from t1;
-- error 1054
select s2 from v1 vq1 where 2 = (select count(*) from v1 vq2 having vq1.s2 = vq2.s2);
select s2 from v1 vq1 where 2 = (select count(*) aa from v1 vq2 having vq1.s2 = aa);
drop view v1;
drop table t1;

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
#
# Test case for bug #9398 CREATE TABLE with SELECT from a multi-table view
#
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;
CREATE TABLE t3 SELECT * FROM v1;
SELECT * FROM t3;

DROP VIEW v1;
DROP TABLE t1,t2,t3;

#
# Test for BUG#8703 "insert into table select from view crashes"
#
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;
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538

#
# Test for BUG #6106: query over a view using subquery for the underlying table
#

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);

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);

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); 

# Tests from the report for bug #6107

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)); 

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); 
 
DROP VIEW v1,v2,v3;
DROP TABLE t1,t2;
1539

serg@serg.mylan's avatar
serg@serg.mylan committed
1540
#
1541 1542
# BUG#8490 Select from views containing subqueries causes server to hang 
# forever.
serg@serg.mylan's avatar
serg@serg.mylan committed
1543
#
1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
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;
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;
drop view v1, v2;
drop table t1, t2, t3;

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
#
# Test case for bug #8528: select from view over multi-table view
#
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;
CREATE VIEW v2 AS SELECT * FROM v1;
SELECT * FROM v2;

DROP VIEW v2,v1;
serg@serg.mylan's avatar
serg@serg.mylan committed
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587

DROP TABLE t1, t2;
#
# Correct restoring view name in SP table locking BUG#9758
#
create table t1 (a int);
create view v1 as select sum(a) from t1 group by a;
delimiter //;
create procedure p1()
begin
select * from v1;
end//
delimiter ;//
call p1();
call p1();
drop procedure p1;
drop view v1;
drop table t1;

1588 1589 1590 1591 1592 1593 1594 1595 1596
#
# Bug#7422 "order by" doesn't work
#
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;
1597
SELECT d, c FROM v1 ORDER BY d,c;
1598 1599
DROP VIEW v1;
DROP TABLE t1, t2;
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
#
# using sum(distinct ) & avg(distinct ) in views (BUG#7015)
#
create table t1 (s1 int);
create view  v1 as select sum(distinct s1) from t1;
select * from v1;
drop view v1;
create view  v1 as select avg(distinct s1) from t1;
select * from v1;
drop view v1;
drop table t1;

#
# using cast(... as decimal) in views (BUG#11387);
#
create view v1 as select cast(1 as decimal);
select * from v1;
drop view v1;
1618

1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
#
# Bug#11298 insert into select from VIEW produces incorrect result when 
#           using ORDER BY
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;
drop view v1;
drop table t1,t2,t3;
1632 1633 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

#
# Generation unique names for columns, and correct names check (BUG#7448)
#
# names with ' and \
create view v1 as select '\\','\\shazam';
select * from v1;
drop view v1;
create view v1 as select '\'','\shazam';
select * from v1;
drop view v1;
# autogenerated names differ by case only
create view v1 as select 'k','K';
select * from v1;
drop view v1;
create table t1 (s1 int);
# same autogenerated names
create view v1 as select s1, 's1' from t1;
select * from v1;
drop view v1;
create view v1 as select 's1', s1 from t1;
select * from v1;
drop view v1;
# set name as one of expected autogenerated
create view v1 as select 's1', s1, 1 as My_exp_s1 from t1;
select * from v1;
drop view v1;
create view v1 as select 1 as My_exp_s1, 's1', s1  from t1;
select * from v1;
drop view v1;
# set name conflict with autogenerated names
create view v1 as select 1 as s1, 's1', 's1' from t1;
select * from v1;
drop view v1;
create view v1 as select 's1', 's1', 1 as s1 from t1;
select * from v1;
drop view v1;
# underlying field name conflict with autogenerated names
create view v1 as select s1, 's1', 's1' from t1;
select * from v1;
drop view v1;
create view v1 as select 's1', 's1', s1 from t1;
select * from v1;
drop view v1;
# underlying field name conflict with set name
-- error 1060
create view v1 as select 1 as s1, 's1', s1 from t1;
-- error 1060
create view v1 as select 's1', s1, 1 as s1 from t1;
drop table t1;
# set names differ by case only
-- error 1060
create view v1(k, K) as select 1,2;
bell@sanja.is.com.ua's avatar
merge  
bell@sanja.is.com.ua committed
1685

1686 1687 1688 1689 1690 1691
#
# using time_format in view (BUG#7521)
#
create view v1 as SELECT TIME_FORMAT(SEC_TO_TIME(3600),'%H:%i') as t;
select * from v1;
drop view v1;
1692

1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712
#
# evaluation constant functions in WHERE (BUG#4663)
#
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;
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;
DROP VIEW v1;
CREATE VIEW v1 AS SELECT * FROM t1 WHERE a = VERSION();
SHOW CREATE VIEW v1;
DROP VIEW v1;
CREATE VIEW v1 AS SELECT * FROM t1 WHERE a = DATABASE();
SHOW CREATE VIEW v1;
DROP VIEW v1;
DROP TABLE t1;
1713

1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729
#
# checking views after some view with error (BUG#11337)
#
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;
drop view v1, v2, v3, v4, v5, v6;
drop table t2;

1730 1731 1732 1733
--disable_warnings
drop function if exists f1;
drop function if exists f2;
--enable_warnings
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
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;
create function f1 () returns int return (select max(col1) from t1);
DROP TABLE t1;
# following will show underlying table until BUG#11555 fix
CHECK TABLE v1, v2, v3, v4, v5, v6;
drop function f1;
drop function f2;
drop view v1, v2, v3, v4, v5, v6;
drop table t2,t3;
bell@sanja.is.com.ua's avatar
Merge  
bell@sanja.is.com.ua committed
1755

1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
#
# bug #11325 Wrong date comparison in views
#
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';
select * from v1 where '2005.02.02'=f1;
drop view v1;
drop table t1;
evgen@moonbone.local's avatar
Merge  
evgen@moonbone.local committed
1766

1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777
#
# using encrypt & substring_index in view (BUG#7024)
#
CREATE VIEW v1 AS SELECT ENCRYPT("dhgdhgd");
disable_result_log;
SELECT * FROM v1;
enable_result_log;
drop view v1;
CREATE VIEW v1 AS SELECT SUBSTRING_INDEX("dkjhgd:kjhdjh", ":", 1);
SELECT * FROM v1;
drop view v1;
1778

1779 1780 1781 1782 1783 1784 1785 1786 1787
#
# hide underlying tables names in case of imposibility to update (BUG#10773)
#
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);
-- error 1288
update v1 set f60=2345;
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1788
-- error 1443
1789 1790 1791 1792
update t1 set f60=(select max(f60) from v1);
drop view v1;
drop table t1;

1793 1794 1795 1796 1797 1798 1799 1800 1801
#
# Using var_samp with view (BUG#10651)
#
create table t1 (s1 int);
create view v1 as select var_samp(s1) from t1;
show create view v1;
drop view v1;
drop table t1;

1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818
#
# Correct inserting data check (absence of default value) for view
# underlying tables (BUG#6443)
#
set sql_mode='strict_all_tables';
CREATE TABLE t1 (col1 INT NOT NULL, col2 INT NOT NULL) ENGINE = INNODB;
CREATE VIEW v1 (vcol1) AS SELECT col1 FROM t1;
CREATE VIEW v2 (vcol1) AS SELECT col1 FROM t1 WHERE col2 > 2;
-- error 1364
INSERT INTO t1 (col1) VALUES(12);
-- error 1423
INSERT INTO v1 (vcol1) VALUES(12);
-- error 1423
INSERT INTO v2 (vcol1) VALUES(12);
set sql_mode=default;
drop view v2,v1;
drop table t1;
1819

1820 1821 1822 1823 1824 1825 1826 1827 1828
#
# Bug#11399 Use an alias in a select statement on a view
#
create table t1 (f1 int);
insert into t1 values (1);
create view v1 as select f1 from t1;
select f1 as alias from v1;
drop view v1;
drop table t1;
evgen@moonbone.local's avatar
Merge  
evgen@moonbone.local committed
1829

1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841
#
# Test for bug #6120: SP cache to be invalidated when altering a view
#

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;
CREATE PROCEDURE p1 () SELECT * FROM v1;
CALL p1();
ALTER VIEW v1 AS SELECT s1 AS s1, s2 AS s2 FROM t1;
CALL p1();
1842 1843 1844
DROP VIEW v1;
CREATE VIEW v1 AS SELECT s2 AS s1, s1 AS s2 FROM t1;
CALL p1();
1845 1846 1847 1848

DROP PROCEDURE p1;
DROP VIEW v1;
DROP TABLE t1;
1849

1850 1851 1852 1853 1854 1855 1856
#
# Test for bug #11709 View was ordered by wrong column
#
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;
1857
drop view v1;
1858
drop table t1;
evgen@moonbone.local's avatar
Merge  
evgen@moonbone.local committed
1859

1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872
#
# Test for bug #11771: wrong query_id in SELECT * FROM <view>
#
CREATE TABLE t1 (f1 char) ENGINE = innodb;
INSERT INTO t1 VALUES ('A');
CREATE VIEW  v1 AS SELECT * FROM t1;

INSERT INTO t1 VALUES('B');
SELECT * FROM v1;
SELECT * FROM t1;

DROP VIEW v1;
DROP TABLE t1;
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890

#
# opening table in correct locking mode (BUG#9597)
#
CREATE TABLE t1 ( bug_table_seq   INTEGER NOT NULL);
CREATE OR REPLACE VIEW v1 AS SELECT * from t1;
DROP PROCEDURE IF EXISTS p1;
delimiter //;
CREATE PROCEDURE p1 ( )
BEGIN
        DO (SELECT  @next := IFNULL(max(bug_table_seq),0) + 1 FROM v1);
        INSERT INTO t1 VALUES (1);
END //
delimiter ;//
CALL p1();
DROP PROCEDURE p1;
DROP VIEW v1;
DROP TABLE t1;
1891

1892 1893 1894 1895 1896 1897 1898 1899
#
# Bug #11335 View redefines column types
#
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;
drop view v1;
drop table t1;
1900

1901 1902 1903 1904 1905 1906 1907 1908
#
# Bug #11760 Typo in Item_func_add_time::print() results in NULLs returned
#             subtime() in view
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;
drop view v1;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933
drop table t1;

#
# Test for bug #11412: query over a multitable view with GROUP_CONCAT
#
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;
SELECT pid,GROUP_CONCAT(CONCAT(fn,' ',ln) ORDER BY 1) FROM v1 GROUP BY pid;

DROP VIEW v1;
DROP TABLE t1,t2;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949

#
# Test for bug #12382: SELECT * FROM view after INSERT command
#

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;
SELECT * FROM v1;

DROP VIEW v1;
DROP TABLE t1;

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
#
# Test for bug #12470: crash for a simple select from a view defined
#                      as a join over 5 tables

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;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1963

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1964 1965 1966 1967
SELECT a FROM v1;

DROP VIEW v1;
DROP TABLE t1,t2,t3,t4,t5;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1968

1969 1970 1971 1972 1973 1974
#
# Bug #12298 Typo in  function name results in erroneous view being created.
#
create view v1 as select timestampdiff(day,'1997-01-01 00:00:00','1997-01-02 00:00:00') as f1;
select * from v1;
drop view v1;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1975

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
#
# repeatable CREATE VIEW statement BUG#12468
#
create table t1(a int);
create procedure p1() create view v1 as select * from t1;
drop table t1;
-- error 1146
call p1();
-- error 1146
call p1();
drop procedure p1;
1987

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
#
# Bug #10624 Views with multiple UNION and UNION ALL produce incorrect results
#
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;
drop view v1;
drop table t1,t2;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
#
# Test for bug #10970: view referring a temporary table indirectly
#                     

CREATE TEMPORARY TABLE t1 (a int);
CREATE FUNCTION f1 () RETURNS int RETURN (SELECT COUNT(*) FROM t1);
-- error 1352
CREATE VIEW v1 AS SELECT f1();

DROP FUNCTION f1;
DROP TABLE t1;

2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027
#
# BUG #12533 (crash on DESCRIBE <view> after renaming base table column)
#
--disable_warnings
DROP TABLE IF EXISTS t1;
DROP VIEW  IF EXISTS v1;
--enable_warnings

CREATE TABLE t1 (f4 CHAR(5));
CREATE VIEW v1 AS SELECT * FROM t1;
DESCRIBE v1;

ALTER TABLE t1 CHANGE COLUMN f4 f4x CHAR(5);
--error 1356
DESCRIBE v1;
DROP TABLE t1;
DROP VIEW v1;
2028 2029 2030 2031 2032 2033 2034 2035 2036

# 
# Bug #12489 wrongly printed strcmp() function results in creation of broken
#            view
create table t1 (f1 char);
create view v1 as select strcmp(f1,'a') from t1;
select * from v1;
drop view v1;
drop table t1;
2037 2038

#
2039 2040 2041 2042 2043 2044 2045 2046
# Bug #12922 if(sum(),...) with group from view returns wrong results 
#
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;
drop view v1;
drop table t1;
2047 2048
# BUG#12941
#
2049
--disable_warnings
2050 2051 2052 2053 2054 2055 2056 2057 2058 2059
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;
2060
--enable_warnings
2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079

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;

drop view v1, v2;
drop table t1, t2;
timour@mysql.com's avatar
timour@mysql.com committed
2080

2081 2082 2083
#
# DEFINER information check
#
2084
-- error ER_MALFORMED_DEFINER
2085
create definer=some_user@`` sql security invoker view v1 as select 1;
2086 2087 2088
create definer=some_user@localhost sql security invoker view v1 as select 1;
show create view v1;
drop view v1;
2089

timour@mysql.com's avatar
timour@mysql.com committed
2090 2091 2092 2093 2094 2095 2096 2097
#
# Bug #6808 - Views: CREATE VIEW v ... FROM t AS v fails
#

create table t1 (s1 int); 
create view abc as select * from t1 as abc;
drop table t1;
drop view abc;
evgen@moonbone.local's avatar
Merge  
evgen@moonbone.local committed
2098

2099 2100 2101 2102 2103 2104 2105 2106
#
# Bug #12993 View column rename broken in subselect
#
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';
drop view v1;
drop table t1;
2107

2108 2109 2110 2111 2112 2113
#
# Bug #11416 Server crash if using a view that uses function convert_tz
#
create view v1 as SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET');
select * from v1;
drop view v1;
evgen@moonbone.local's avatar
Merge  
evgen@moonbone.local committed
2114

2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144
#
# Bugs #12963, #13000: wrong creation of VIEW with DAYNAME, DAYOFWEEK, and WEEKDAY
#

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;

CREATE VIEW v2 AS SELECT DAYOFWEEK(date) FROM t1;
SHOW CREATE VIEW v2;

CREATE VIEW v3 AS SELECT WEEKDAY(date) FROM t1;
SHOW CREATE VIEW v3;

SELECT DAYNAME('2005-09-06');
SELECT DAYNAME(date) FROM t1;
SELECT * FROM v1;

SELECT DAYOFWEEK('2005-09-06');
SELECT DAYOFWEEK(date) FROM t1;
SELECT * FROM v2;

SELECT WEEKDAY('2005-09-06');
SELECT WEEKDAY(date) FROM t1;
SELECT * FROM v3;

DROP TABLE t1;
DROP VIEW  v1, v2, v3;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158

#
# Bug #13411: crash when using non-qualified view column in HAVING clause 
#

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;
SELECT v1.a FROM v1 GROUP BY v1.a HAVING a > 1;

DROP VIEW v1;
DROP TABLE t1;

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2159 2160 2161 2162 2163 2164 2165 2166 2167
#
# Bug #13410: failed name resolution for qualified view column in HAVING 
#

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;
SELECT v1.a FROM v1 GROUP BY v1.a HAVING v1.a > 1;
2168 2169
SELECT t_1.a FROM t1 AS t_1 GROUP BY t_1.a HAVING t_1.a IN (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);
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2170

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2171 2172
DROP VIEW v1;
DROP TABLE t1;
2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191

#
# Bug #13327 view wasn't using index for const condition
#

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;
EXPLAIN SELECT * FROM v1 WHERE a=1;
EXPLAIN SELECT * FROM v2 WHERE a=1;
DROP VIEW v1,v2;
DROP TABLE t1,t2,t3;

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2192
#
2193 2194 2195 2196 2197 2198 2199 2200 2201 2202
# Bug #14466 lost sort order in GROUP_CONCAT() in a view
#
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;
select * from v2;
drop view v1,v2;
drop table t1;
sergefp@mysql.com's avatar
sergefp@mysql.com committed
2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226

#
# BUG#14026 Crash on second PS execution when using views
#
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;
execute stmt1 using @parm1;
drop view v1;
drop table t1,t2,t3,t4;
2227 2228

#
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2229 2230 2231
# Bug #14540: OPTIMIZE, ANALYZE, REPAIR applied to not a view
#

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2232
CREATE TABLE t1(id INT);
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2233 2234 2235 2236 2237 2238 2239 2240 2241 2242
CREATE VIEW v1 AS SELECT id FROM t1;

OPTIMIZE TABLE v1;
ANALYZE TABLE v1;
REPAIR TABLE v1;

DROP TABLE t1;
OPTIMIZE TABLE v1;

DROP VIEW v1;
2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255


#
# BUG#14719: Views DEFINER grammar is incorrect
#

create definer = current_user() sql security invoker view v1 as select 1;
show create view v1;
drop view v1;

create definer = current_user sql security invoker view v1 as select 1;
show create view v1;
drop view v1;