view.test 95.9 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
13
-- error ER_NO_SUCH_TABLE
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
14 15 16
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 ER_VIEW_SELECT_TMPTABLE
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 ER_VIEW_SELECT_VARIABLE
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
27
create view v1 (c,d) as select a,b+@@global.max_user_connections from t1;
28 29 30
-- error ER_VIEW_SELECT_VARIABLE
create view v1 (c,d) as select a,b from t1
  where a = @@global.max_user_connections;
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
31 32 33 34

# simple view
create view v1 (c) as select b+1 from t1;
select c from v1;
35
select is_updatable from information_schema.views where table_name='v1';
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
36

serg@serg.mylan's avatar
serg@serg.mylan committed
37
# temporary table should not hide table of view
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
38 39 40 41 42 43 44
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;
45
-- error ER_WRONG_OBJECT
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
46 47 48
show create view t1;
drop table t1;

serg@serg.mylan's avatar
serg@serg.mylan committed
49
# try to use fields from underlying table
50
-- error ER_BAD_FIELD_ERROR
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
51
select a from v1;
52
-- error ER_BAD_FIELD_ERROR
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
53
select v1.a from v1;
54
-- error ER_BAD_FIELD_ERROR
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
55
select b from v1;
56
-- error ER_BAD_FIELD_ERROR
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
57 58
select v1.b from v1;

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

serg@serg.mylan's avatar
serg@serg.mylan committed
66
# try to use underlying table fields in VIEW creation process
67
-- error ER_BAD_FIELD_ERROR
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
68
create view v3 (c) as select a+1 from v1;
69
-- error ER_BAD_FIELD_ERROR
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
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;
89
show full tables;
90
--replace_column 8 # 12 # 13 #
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
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
107
-- error ER_TABLE_EXISTS_ERROR
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
108 109 110 111 112 113 114
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;
115
-- error ER_NO_SUCH_TABLE
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
116 117 118 119 120 121 122 123 124 125 126 127
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
128
# try to drop nonexistent VIEW
129
-- error ER_BAD_TABLE_ERROR
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
130 131
drop view v100;

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

serg@serg.mylan's avatar
serg@serg.mylan committed
136
# try to drop VIEW with DROP TABLE
137
-- error ER_BAD_TABLE_ERROR
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
138 139
drop table v1;

serg@serg.mylan's avatar
serg@serg.mylan committed
140
# try to drop table with DROP VIEW
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
141 142 143 144 145 146 147 148 149 150 151 152 153

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;

154 155 156
select * from t1 natural left join v1;
select * from v2 natural left join t1;
select * from v2 natural left join v1;
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

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);
178
-- error ER_VIEW_NONUPD_CHECK
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
179
create view v1 as select distinct a from t1 WITH CHECK OPTION;
180 181 182
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
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
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
203
# LIMIT clause test
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
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 231
#
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;
232 233
select is_updatable from information_schema.views where table_name='v2';
select is_updatable from information_schema.views where table_name='v1';
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
234
# try to update expression
235
-- error ER_NONUPDATEABLE_COLUMN
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
236 237
update v1 set c=a+c;
# try to update VIEW with forced TEMPORARY TABLE algorithm
238
-- error ER_NON_UPDATABLE_TABLE
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
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
257
-- error ER_NONUPDATEABLE_COLUMN
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
258 259
update t2,v1 set v1.c=v1.a+v1.c where t2.x=v1.a;
# try to update VIEW with forced TEMPORARY TABLE algorithm
260
-- error ER_NON_UPDATABLE_TABLE
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
261 262 263 264 265 266 267 268 269
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
270
# MERGE VIEW with WHERE clause
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
#
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
295
-- error ER_NON_UPDATABLE_TABLE
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
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
314
-- error ER_NON_UPDATABLE_TABLE
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
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;
330
set updatable_views_with_limit=NO;
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
331 332 333
update v1 set x=x+1;
update v2 set x=x+1;
update v1 set x=x+1 limit 1;
334
-- error ER_NON_UPDATABLE_TABLE
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
335
update v2 set x=x+1 limit 1;
336
set updatable_views_with_limit=YES;
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
337 338
update v1 set x=x+1 limit 1;
update v2 set x=x+1 limit 1;
339 340
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
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
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
mats@romeo.(none)'s avatar
mats@romeo.(none) committed
356
-- error ER_NON_INSERTABLE_TABLE
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
357 358
insert into v3 values (-60,4,30);
# try insert to VIEW with expression in SELECT list
mats@romeo.(none)'s avatar
mats@romeo.(none) committed
359
-- error ER_NON_INSERTABLE_TABLE
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
360 361
insert into v4 values (-60,4,30);
# try insert to VIEW using temporary table algorithm
mats@romeo.(none)'s avatar
mats@romeo.(none) committed
362
-- error ER_NON_INSERTABLE_TABLE
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
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
mats@romeo.(none)'s avatar
mats@romeo.(none) committed
384
-- error ER_NON_INSERTABLE_TABLE
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
385 386
insert into v3 select c, b, a from t2;
# try insert to VIEW with expression in SELECT list
mats@romeo.(none)'s avatar
mats@romeo.(none) committed
387
-- error ER_NON_INSERTABLE_TABLE
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
388 389
insert into v4 select c, b, a from t2;
# try insert to VIEW using temporary table algorithm
mats@romeo.(none)'s avatar
mats@romeo.(none) committed
390
-- error ER_NON_INSERTABLE_TABLE
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
391 392 393 394 395
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;
396
drop table t1, t2;
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
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
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;
427
-- error ER_NON_UPDATABLE_TABLE
bell@sanja.is.com.ua's avatar
VIEW  
bell@sanja.is.com.ua committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
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;

445 446 447 448 449 450 451 452 453 454
#
# 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;
455 456 457 458 459 460 461 462 463

#
# 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;
464 465 466 467 468 469 470 471 472 473

#
# 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`;
474 475

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

#
# check of duplication of column names
#
492
-- error ER_DUP_FIELDNAME
493
create view v1 (a,a) as select 'a','a';
494

495
#
serg@serg.mylan's avatar
serg@serg.mylan committed
496
# updatablity should be transitive
497 498 499 500 501 502
#
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';
503
select is_updatable from information_schema.views;
504 505 506
select * from t1;
drop view v2, v1;
drop table t1;
bell@sanja.is.com.ua's avatar
Merge  
bell@sanja.is.com.ua committed
507

508 509 510 511 512
#
# 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;
513
--error ER_KEY_DOES_NOT_EXITS
514 515 516
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
517

518 519 520 521 522 523 524 525 526 527
#
# 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;
528 529

#
serg@serg.mylan's avatar
serg@serg.mylan committed
530
# Test of view updatability in prepared statement
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
#
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;
558 559 560 561

#
# error on preparation
#
562
-- error ER_NO_TABLES_USED
563 564
CREATE VIEW v02 AS SELECT * FROM DUAL;
SHOW TABLES;
565 566 567 568 569 570 571

#
# EXISTS with UNION VIEW
#
CREATE VIEW v1 AS SELECT EXISTS (SELECT 1 UNION SELECT 2);
select * from v1;
drop view v1;
572 573 574 575 576 577

#
# using VIEW where table is required
#
create table t1 (col1 int,col2 char(22));
create view v1 as select * from t1;
578
-- error ER_WRONG_OBJECT
579 580 581
create index i1 on v1 (col1);
drop view v1;
drop table t1;
582 583 584 585 586 587 588

#
# 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;
589 590 591 592 593 594 595 596 597 598 599 600

#
# 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;
601 602 603 604 605 606 607 608 609 610 611

#
# 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;
612 613 614 615 616 617 618 619 620 621 622

#
# 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;
623 624 625 626 627 628 629

#
# 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`;
630 631 632 633 634 635 636 637 638 639

#
# 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;
640 641 642 643 644 645 646 647 648 649 650

#
# 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;
651 652 653 654 655 656 657 658 659 660

#
# 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;
661 662 663 664 665 666 667 668 669 670 671 672 673
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;
674 675
drop view v1;
drop table t1;
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690

#
# 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;
691 692 693 694 695 696 697 698 699 700

#
# 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`;
701 702 703 704

#
# tested problem when function name length close to ALIGN_SIZE
#
705 706
create function a() returns int return 5;
create view v1 as select a();
707 708
select * from v1;
drop view v1;
709
drop function a;
710 711 712 713 714 715 716 717 718 719

#
# 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;
720 721 722 723 724 725 726 727 728 729

#
# 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;
730 731

#
serg@serg.mylan's avatar
serg@serg.mylan committed
732
# VIEW over dropped function
733 734
#
create function x1 () returns int return 5;
serg@serg.mylan's avatar
serg@serg.mylan committed
735
create table t1 (s1 int);
736 737
create view v1 as select x1() from t1;
drop function x1;
738
-- error ER_VIEW_INVALID
739
select * from v1;
740
--replace_column 8 # 12 # 13 #
741 742 743
show table status;
drop view v1;
drop table t1;
744 745

#
serg@serg.mylan's avatar
serg@serg.mylan committed
746
# VIEW with floating point (long number) as column
747 748 749 750
#
create view v1 as select 99999999999999999999999999999999999999999999999999999 as col1;
show create view v1;
drop view v1;
751 752 753 754

#
# VIEWs with national characters
#
755 756 757 758 759

SET @old_cs_client = @@character_set_client;
SET @old_cs_results = @@character_set_results;
SET @old_cs_connection = @@character_set_connection;

760 761 762 763 764 765 766
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ü;
767 768 769 770

SET character_set_client = @old_cs_client;
SET character_set_results = @old_cs_results;
SET character_set_connection = @old_cs_connection;
771 772 773 774 775 776 777 778 779 780

#
# 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;
781 782 783 784 785 786 787 788

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

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

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

#
# VIEW over SELECT with prohibited clauses
#
820
-- error ER_VIEW_SELECT_CLAUSE
821
create view v1 as select 5 into @w;
822
-- error ER_VIEW_SELECT_CLAUSE
823 824
create view v1 as select 5 into outfile 'ttt';
create table t1 (a int);
825
-- error ER_VIEW_SELECT_CLAUSE
826
create view v1 as select a from t1 procedure analyse();
827 828
-- error ER_VIEW_SELECT_DERIVED
create view v1 as select 1 from (select 1) as d1;
829
drop table t1;
830 831 832 833 834 835 836 837 838 839 840

#
# 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;
841

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

964 965 966 967 968
#
# HANDLER with VIEW
#
create table t1 (s1 int);
create view v1 as select * from t1;
969
-- error ER_WRONG_OBJECT
970 971 972
handler v1 open as xx;
drop view v1;
drop table t1;
973 974 975 976 977 978 979 980 981 982 983 984

#
# 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;
985
drop table t1, t2;
986 987 988 989 990 991 992 993 994 995 996 997

#
# 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;
998 999 1000 1001 1002 1003 1004 1005
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;
1006 1007
drop view v1;
drop table t1;
1008 1009 1010 1011 1012 1013 1014 1015 1016

#
# 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;
1017
-- error ER_TABLE_NOT_LOCKED
1018 1019 1020
select * from t2;
drop view v1;
drop table t1, t2;
bell@sanja.is.com.ua's avatar
merge  
bell@sanja.is.com.ua committed
1021

1022 1023 1024 1025 1026 1027 1028
#
# 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);
1029
-- error ER_VIEW_CHECK_FAILED
1030 1031 1032 1033 1034 1035 1036 1037
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;
1038
-- error ER_VIEW_CHECK_FAILED
1039 1040 1041 1042 1043 1044
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;
1045
select * from t1 order by a desc;
1046
# simple UPDATE test
1047
update v1 set a=-1 where a=0;
1048
-- error ER_VIEW_CHECK_FAILED
1049
update v1 set a=2 where a=1;
1050
select * from t1 order by a desc;
1051 1052 1053 1054 1055
# 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;
1056
select * from t1 order by a desc;
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
# 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);
1075
-- error ER_VIEW_CHECK_FAILED
1076
insert into v2 values (0);
1077
-- error ER_VIEW_CHECK_FAILED
1078 1079
insert into v3 values (0);
insert into v2 values (2);
1080
-- error ER_VIEW_CHECK_FAILED
1081 1082 1083 1084
insert into v3 values (2);
select * from t1;
drop view v3,v2,v1;
drop table t1;
1085 1086 1087 1088 1089 1090 1091

#
# 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;
1092
-- error ER_VIEW_CHECK_FAILED
1093 1094 1095 1096 1097
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;
1098 1099 1100 1101 1102 1103 1104

#
# 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;
1105
-- error ER_NO_SUCH_TABLE
1106
alter view v1 as select * from v2;
1107
-- error ER_NO_SUCH_TABLE
1108
alter view v1 as select * from v1;
1109
-- error ER_NO_SUCH_TABLE
1110
create or replace view v1 as select * from v2;
1111
-- error ER_NO_SUCH_TABLE
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
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;
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145

#
# 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);
1146
-- error ER_VIEW_CHECK_FAILED
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
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;
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170

#
# 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;
1171 1172 1173 1174 1175 1176 1177

#
# 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;
1178
-- error ER_VIEW_CHECK_FAILED
1179 1180 1181
insert into v2 values (0);
drop view v2, v1;
drop table t1;
1182 1183 1184 1185 1186 1187 1188

#
# 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
1189
-- error ER_VIEW_CHECK_FAILED
1190 1191 1192 1193 1194 1195 1196
insert ignore into v1 values (6);
#several values
insert ignore into v1 values (6),(3);
select * from t1;
drop view v1;
drop table t1;

1197 1198 1199 1200 1201 1202
#
# 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;
1203
-- error ER_VIEW_CHECK_FAILED
1204 1205 1206
insert into v1 values (0);
select * from v1;
select * from t1;
1207
drop trigger t1_bi;
1208 1209 1210
drop view v1;
drop table t1;

1211 1212 1213 1214 1215 1216
#
# 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;
1217
-- error ER_VIEW_CHECK_FAILED
1218 1219 1220 1221 1222
insert into v2 values (0);
select * from v2;
select * from t1;
drop view v2, v1;
drop table t1;
1223 1224 1225 1226 1227 1228 1229

#
# 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;
1230
-- error ER_VIEW_CHECK_FAILED
msvensson@pilot.mysql.com's avatar
msvensson@pilot.mysql.com committed
1231
load data infile '../../std_data/loaddata3.dat' into table v1 fields terminated by '' enclosed by '' ignore 1 lines;
1232 1233 1234
select * from t1;
select * from v1;
delete from t1;
msvensson@pilot.mysql.com's avatar
msvensson@pilot.mysql.com committed
1235
load data infile '../../std_data/loaddata3.dat' ignore into table v1 fields terminated by '' enclosed by '' ignore 1 lines;
1236 1237
select * from t1 order by a,b;
select * from v1 order by a,b;
1238 1239 1240 1241 1242
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;
1243
-- error ER_VIEW_CHECK_FAILED
msvensson@pilot.mysql.com's avatar
msvensson@pilot.mysql.com committed
1244
load data infile '../../std_data/loaddata2.dat' into table v1 fields terminated by ',' enclosed by '''';
1245 1246 1247
select concat('|',a,'|'), concat('|',b,'|') from t1;
select concat('|',a,'|'), concat('|',b,'|') from v1;
delete from t1;
msvensson@pilot.mysql.com's avatar
msvensson@pilot.mysql.com committed
1248
load data infile '../../std_data/loaddata2.dat' ignore into table v1 fields terminated by ',' enclosed by '''';
1249 1250 1251 1252 1253
select concat('|',a,'|'), concat('|',b,'|') from t1;
select concat('|',a,'|'), concat('|',b,'|') from v1;
drop view v1;
drop table t1;

1254 1255 1256 1257 1258
#
# 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);
mats@romeo.(none)'s avatar
mats@romeo.(none) committed
1259
-- error ER_NON_INSERTABLE_TABLE
1260 1261 1262
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);
mats@romeo.(none)'s avatar
mats@romeo.(none) committed
1263
-- error ER_NON_INSERTABLE_TABLE
1264 1265
insert into v3 values (30);
create view v4 as select * from v2 where 20 < (select (s1) from t1);
mats@romeo.(none)'s avatar
mats@romeo.(none) committed
1266
-- error ER_NON_INSERTABLE_TABLE
1267 1268 1269
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
1270

1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
#
# 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
1281

1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
#
# 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;
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322

#
# 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
1323
set updatable_views_with_limit=NO;
1324
-- error ER_NON_UPDATABLE_TABLE
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1325 1326
update v2 set a= 10 where a=200 limit 1;
set updatable_views_with_limit=DEFAULT;
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
# 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
1352
-- error ER_VIEW_NO_INSERT_FIELD_LIST
1353
insert into v3 values (1,2);
1354
-- error ER_VIEW_NO_INSERT_FIELD_LIST
1355 1356
insert into v3 select * from t2;
# inserting in several tables of join view
1357
-- error ER_VIEW_MULTIUPDATE
1358
insert into v3(a,b) values (1,2);
1359
-- error ER_VIEW_MULTIUPDATE
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
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
1370
-- error ER_VIEW_DELETE_MERGE_VIEW
1371
delete from v3;
1372
-- error ER_VIEW_DELETE_MERGE_VIEW
1373
delete v3,t1 from v3,t1;
1374
-- error ER_VIEW_DELETE_MERGE_VIEW
1375
delete t1,v3 from t1,v3;
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
# 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
1393
drop view v3;
1394
drop tables t1,t2;
1395

bell@sanja.is.com.ua's avatar
merge  
bell@sanja.is.com.ua committed
1396
#
1397
# View field names should be case insensitive
bell@sanja.is.com.ua's avatar
merge  
bell@sanja.is.com.ua committed
1398
#
1399 1400 1401 1402 1403
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
1404

1405
#
1406
# Resolving view fields in subqueries in VIEW (Bug#6394)
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417
#
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;
1418
select * from (select c1 from v2) X;
1419 1420
drop view v2, v1;
drop table t1, t2;
1421 1422

#
1423
# view over other view setup (Bug#7433)
1424 1425 1426 1427 1428 1429 1430 1431
#
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;
1432 1433

#
1434
# view and group_concat() (Bug#7116)
1435
#
1436 1437
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);
1438 1439 1440 1441 1442
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;
1443 1444

#
1445
# Item_ref resolved as view field (Bug#6894)
1446 1447 1448
#
create table t1 (s1 int, s2 char);
create view v1 as select s1, s2 from t1;
1449
-- error ER_BAD_FIELD_ERROR
1450 1451 1452 1453 1454
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;

1455
#
1456
# Test case for Bug#9398 CREATE TABLE with SELECT from a multi-table view
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
#
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;

#
1472
# Test for Bug#8703 insert into table select from view crashes
1473 1474 1475 1476 1477 1478 1479 1480 1481
#
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;
1482 1483

#
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
# 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');
1506 1507
INSERT INTO t2 VALUES (8,1,'y');

1508 1509 1510
CREATE VIEW v1 AS SELECT * FROM t1;

SELECT a.col1,a.col2,b.col2,b.col3
1511
  FROM t1 a LEFT JOIN t2 b ON a.col1=b.col1
1512
    WHERE b.col2 IS NULL OR
1513 1514
          b.col2=(SELECT MAX(col2) FROM t2 b WHERE b.col1=a.col1);

1515
SELECT a.col1,a.col2,b.col2,b.col3
1516
  FROM v1 a LEFT JOIN t2 b ON a.col1=b.col1
1517
    WHERE b.col2 IS NULL OR
1518 1519
          b.col2=(SELECT MAX(col2) FROM t2 b WHERE b.col1=a.col1);

1520
CREATE VIEW v2 AS SELECT * FROM t2;
1521 1522 1523 1524

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
1525
          b.col2=(SELECT MAX(col2) FROM v2 b WHERE b.col1=a.col1);
1526

1527
# Tests from the report for Bug#6107
1528 1529 1530 1531 1532

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
1533
          b.col2=(SELECT MAX(col2) FROM v2 b WHERE b.col1=a.col1));
1534 1535 1536 1537 1538 1539

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
1540 1541
          b.col2=(SELECT MAX(col2) FROM v2 b WHERE b.col1=a.col1);

1542 1543
DROP VIEW v1,v2,v3;
DROP TABLE t1,t2;
1544

serg@serg.mylan's avatar
serg@serg.mylan committed
1545
#
1546 1547
# Bug#8490 Select from views containing subqueries causes server to hang
#          forever.
serg@serg.mylan's avatar
serg@serg.mylan committed
1548
#
1549 1550 1551 1552 1553
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;
1554
create view v2 as select * from t3 where
1555 1556 1557 1558 1559
  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
1560
#
1561
# Test case for Bug#8528 select from view over multi-table view
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
#
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
1574 1575 1576

DROP TABLE t1, t2;
#
1577
# Correct restoring view name in SP table locking Bug#9758
serg@serg.mylan's avatar
serg@serg.mylan committed
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
#
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;

1593 1594 1595 1596 1597 1598 1599 1600 1601
#
# 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;
1602
SELECT d, c FROM v1 ORDER BY d,c;
1603 1604
DROP VIEW v1;
DROP TABLE t1, t2;
1605
#
1606
# using sum(distinct ) & avg(distinct ) in views (Bug#7015)
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
#
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;

#
1618
# using cast(... as decimal) in views (Bug#11387);
1619 1620 1621 1622
#
create view v1 as select cast(1 as decimal);
select * from v1;
drop view v1;
1623

1624
#
1625
# Bug#11298 insert into select from VIEW produces incorrect result when
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
#           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;
1637 1638

#
1639
# Generation unique names for columns, and correct names check (Bug#7448)
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
#
# 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
1682
-- error ER_DUP_FIELDNAME
1683
create view v1 as select 1 as s1, 's1', s1 from t1;
1684
-- error ER_DUP_FIELDNAME
1685 1686 1687
create view v1 as select 's1', s1, 1 as s1 from t1;
drop table t1;
# set names differ by case only
1688
-- error ER_DUP_FIELDNAME
1689
create view v1(k, K) as select 1,2;
bell@sanja.is.com.ua's avatar
merge  
bell@sanja.is.com.ua committed
1690

1691
#
1692
# using time_format in view (Bug#7521)
1693 1694 1695 1696
#
create view v1 as SELECT TIME_FORMAT(SEC_TO_TIME(3600),'%H:%i') as t;
select * from v1;
drop view v1;
1697

1698
#
1699
# evaluation constant functions in WHERE (Bug#4663)
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
#
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;
1718

1719
#
1720
# checking views after some view with error (Bug#11337)
1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
#
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;

1735 1736 1737 1738
--disable_warnings
drop function if exists f1;
drop function if exists f2;
--enable_warnings
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
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;
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
1759

1760
#
1761
# Bug#11325 Wrong date comparison in views
1762 1763 1764 1765 1766 1767 1768 1769
#
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
1770

1771
#
1772
# using encrypt & substring_index in view (Bug#7024)
1773 1774 1775 1776 1777 1778 1779 1780 1781
#
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;
1782

1783
#
1784
# hide underlying tables names in case of imposibility to update (Bug#10773)
1785 1786 1787
#
create table t1 (f59 int, f60 int, f61 int);
insert into t1 values (19,41,32);
1788
create view v1 as select f59, f60 from t1 where f59 in
1789
         (select f59 from t1);
1790
-- error ER_NON_UPDATABLE_TABLE
1791
update v1 set f60=2345;
1792
-- error ER_VIEW_PREVENT_UPDATE
1793 1794 1795 1796
update t1 set f60=(select max(f60) from v1);
drop view v1;
drop table t1;

1797
#
1798
# Using var_samp with view (Bug#10651)
1799 1800 1801 1802 1803 1804 1805
#
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;

1806

1807 1808
#
# Correct inserting data check (absence of default value) for view
1809
# underlying tables (Bug#6443)
1810 1811
#
set sql_mode='strict_all_tables';
1812
CREATE TABLE t1 (col1 INT NOT NULL, col2 INT NOT NULL);
1813 1814
CREATE VIEW v1 (vcol1) AS SELECT col1 FROM t1;
CREATE VIEW v2 (vcol1) AS SELECT col1 FROM t1 WHERE col2 > 2;
1815
-- error ER_NO_DEFAULT_FOR_FIELD
1816
INSERT INTO t1 (col1) VALUES(12);
1817
-- error ER_NO_DEFAULT_FOR_VIEW_FIELD
1818
INSERT INTO v1 (vcol1) VALUES(12);
1819
-- error ER_NO_DEFAULT_FOR_VIEW_FIELD
1820 1821 1822 1823
INSERT INTO v2 (vcol1) VALUES(12);
set sql_mode=default;
drop view v2,v1;
drop table t1;
1824

1825

1826 1827 1828 1829 1830 1831 1832 1833 1834
#
# 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
1835

1836

1837
#
1838
# Test for Bug#6120 SP cache to be invalidated when altering a view
1839 1840 1841 1842 1843 1844 1845 1846 1847 1848
#

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();
1849 1850 1851
DROP VIEW v1;
CREATE VIEW v1 AS SELECT s2 AS s1, s1 AS s2 FROM t1;
CALL p1();
1852 1853 1854 1855

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

1857

1858
#
1859
# Test for Bug#11709 View was ordered by wrong column
1860 1861 1862 1863 1864
#
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;
1865
drop view v1;
1866
drop table t1;
evgen@moonbone.local's avatar
Merge  
evgen@moonbone.local committed
1867

1868

1869
#
1870
# Test for Bug#11771 wrong query_id in SELECT * FROM <view>
1871
#
1872
CREATE TABLE t1 (f1 char);
1873 1874 1875 1876 1877 1878 1879 1880 1881
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;
1882

1883

1884
#
1885
# opening table in correct locking mode (Bug#9597)
1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
#
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;
1901

1902

1903
#
1904
# Bug#11760 Typo in Item_func_add_time::print() results in NULLs returned
1905 1906 1907 1908 1909 1910
#             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
1911 1912
drop table t1;

1913

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1914
#
1915
# Test for Bug#11412 query over a multitable view with GROUP_CONCAT
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930
#
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;

1931
SELECT pid,GROUP_CONCAT(CONCAT(fn,' ',ln) ORDER BY 1) FROM t1,t2
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1932 1933 1934 1935 1936
  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
1937

1938

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1939
#
1940
# Test for Bug#12382 SELECT * FROM view after INSERT command
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
#

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;

1954

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1955
#
1956 1957
# Test for Bug#12470 crash for a simple select from a view defined
#                    as a join over 5 tables
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1958 1959 1960 1961 1962 1963 1964 1965 1966 1967

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
1968

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1969 1970 1971 1972
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
1973

1974

1975
#
1976
# Bug#12298 Typo in function name results in erroneous view being created.
1977 1978 1979 1980
#
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
1981

bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1982
#
1983
# repeatable CREATE VIEW statement Bug#12468
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1984 1985 1986 1987
#
create table t1(a int);
create procedure p1() create view v1 as select * from t1;
drop table t1;
1988
-- error ER_NO_SUCH_TABLE
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1989
call p1();
1990
-- error ER_NO_SUCH_TABLE
bell@sanja.is.com.ua's avatar
bell@sanja.is.com.ua committed
1991 1992
call p1();
drop procedure p1;
1993

1994

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1995
#
1996
# Bug#10624 Views with multiple UNION and UNION ALL produce incorrect results
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
1997 1998 1999 2000 2001 2002 2003 2004 2005
#
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;
2006 2007 2008 2009


#
# Test for Bug#10970 view referring a temporary table indirectly
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2010 2011 2012 2013
#

CREATE TEMPORARY TABLE t1 (a int);
CREATE FUNCTION f1 () RETURNS int RETURN (SELECT COUNT(*) FROM t1);
2014
-- error ER_VIEW_SELECT_TMPTABLE
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2015 2016 2017 2018 2019
CREATE VIEW v1 AS SELECT f1();

DROP FUNCTION f1;
DROP TABLE t1;

2020

2021
#
2022
# Bug#12533 (crash on DESCRIBE <view> after renaming base table column)
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033
#
--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);
2034
--error ER_VIEW_INVALID
2035 2036 2037
DESCRIBE v1;
DROP TABLE t1;
DROP VIEW v1;
2038

2039 2040 2041

#
# Bug#12489 wrongly printed strcmp() function results in creation of broken
2042 2043 2044 2045 2046 2047
#            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;
2048

2049

2050
#
2051
# Bug#12922 if(sum(),...) with group from view returns wrong results
2052 2053 2054 2055 2056 2057 2058
#
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;
2059 2060 2061


# Bug#12941
2062
#
2063
--disable_warnings
2064 2065 2066 2067 2068 2069 2070
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,
2071
  i_position int(11) NOT NULL,
2072 2073
  users_names varchar(32) default NULL
) Engine = InnoDB;
2074
--enable_warnings
2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088

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
2089
where (v1.group_name='tstgroup1') and v2.r_object_id=v1.r_object_id
2090 2091 2092 2093
order by users_names;

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

2095

timour@mysql.com's avatar
timour@mysql.com committed
2096
#
2097
# Bug#6808 Views: CREATE VIEW v ... FROM t AS v fails
timour@mysql.com's avatar
timour@mysql.com committed
2098 2099
#

2100
create table t1 (s1 int);
timour@mysql.com's avatar
timour@mysql.com committed
2101 2102 2103
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
2104

2105

2106
#
2107
# Bug#12993 View column rename broken in subselect
2108 2109 2110 2111 2112 2113
#
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;
2114

2115

2116
#
2117
# Bug#11416 Server crash if using a view that uses function convert_tz
2118 2119 2120 2121
#
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
2122

2123

2124
#
2125
# Bugs#12963, #13000 wrong creation of VIEW with DAYNAME, DAYOFWEEK, and WEEKDAY
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153
#

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
2154

2155

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2156
#
2157
# Bug#13411 crash when using non-qualified view column in HAVING clause
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168
#

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;

2169

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2170
#
2171
# Bug#13410 failed name resolution for qualified view column in HAVING
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2172 2173 2174 2175 2176 2177 2178
#

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;
2179 2180
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
2181

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2182 2183
DROP VIEW v1;
DROP TABLE t1;
2184

2185

2186
#
2187
# Bug#13327 view wasn't using index for const condition
2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203
#

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;

2204

2205
#
2206
# Bug#13622 Wrong view .frm created if some field's alias contain \n
2207 2208 2209 2210 2211 2212 2213
#
create table t1 (f1 int);
create view v1 as select t1.f1 as '123
456' from t1;
select * from v1;
drop view v1;
drop table t1;
2214

2215 2216

# Bug#14466 lost sort order in GROUP_CONCAT() in a view
2217 2218 2219 2220 2221 2222 2223 2224 2225
#
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
2226

2227

sergefp@mysql.com's avatar
sergefp@mysql.com committed
2228
#
2229
# Bug#14026 Crash on second PS execution when using views
sergefp@mysql.com's avatar
sergefp@mysql.com committed
2230 2231 2232 2233 2234 2235 2236 2237 2238
#
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 (
2239 2240
  (t1 join t2 on ((t1.y = t2.y)))
  join
sergefp@mysql.com's avatar
sergefp@mysql.com committed
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250
  (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;
2251

2252

2253
#
2254
# Bug#14540 OPTIMIZE, ANALYZE, REPAIR applied to not a view
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2255 2256
#

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2257
CREATE TABLE t1(id INT);
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2258 2259 2260 2261 2262 2263 2264 2265
CREATE VIEW v1 AS SELECT id FROM t1;

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

DROP TABLE t1;
OPTIMIZE TABLE v1;
2266 2267
ANALYZE TABLE v1;
REPAIR TABLE v1;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2268 2269

DROP VIEW v1;
2270 2271 2272


#
2273
# Bug#14719 Views DEFINER grammar is incorrect
2274 2275 2276 2277 2278 2279 2280 2281 2282
#

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

2284

2285
#
2286
# Bug#14816 test_if_order_by_key() expected only Item_fields.
2287 2288 2289 2290 2291 2292 2293
#
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;
drop view v1;
drop table t1;
evgen@moonbone.local's avatar
Merge  
evgen@moonbone.local committed
2294

2295

2296
#
2297
# Bug#14850 Item_ref's values wasn't updated
2298 2299 2300
#
create table t1(f1 int, f2 int);
insert into t1 values (null, 10), (null,2);
2301
select f1, sum(f2) from t1 group by f1;
2302 2303 2304 2305
create view v1 as select * from t1;
select f1, sum(f2) from v1 group by f1;
drop view v1;
drop table t1;
2306

2307

2308
#
2309
# Bug#14885 incorrect SOURCE in view created in a procedure
2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325
# TODO: here SOURCE string must be shown when it will be possible
#
--disable_warnings
drop procedure if exists p1;
--enable_warnings
delimiter //;
create procedure p1 () deterministic
begin
create view v1 as select 1;
end;
//
delimiter ;//
call p1();
show create view v1;
drop view v1;
drop procedure p1;
2326

2327

2328
#
2329
# Bug#15096 using function with view for view creation
2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347
#
CREATE VIEW v1 AS SELECT 42 AS Meaning;
--disable_warnings
DROP FUNCTION IF EXISTS f1;
--enable_warnings
DELIMITER //;
CREATE FUNCTION f1() RETURNS INTEGER
BEGIN
  DECLARE retn INTEGER;
  SELECT Meaning FROM v1 INTO retn;
  RETURN retn;
END
//
DELIMITER ;//
CREATE VIEW v2 AS SELECT f1();
select * from v2;
drop view v2,v1;
drop function f1;
2348

2349

2350
#
2351
# Bug#14861 aliased column names are not preserved.
2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367
#
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;

drop view v2, v1;
drop table t1;
2368

2369

2370
#
2371
# Bug#16016 MIN/MAX optimization for views
2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392
#

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;
SELECT MAX(a) FROM v1;

EXPLAIN SELECT MAX(a) FROM t1;
EXPLAIN SELECT MAX(a) FROM v1;

SELECT MIN(a) FROM t1;
SELECT MIN(a) FROM v1;

EXPLAIN SELECT MIN(a) FROM t1;
EXPLAIN SELECT MIN(a) FROM v1;

DROP VIEW v1;
DROP TABLE t1;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2393

2394

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2395
#
2396 2397
# Bug#16382 grouping name is resolved against a view column name
#           which coincides with a select column name
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410

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;
SELECT IF(x IS NULL, 'blank', 'not blank') AS x FROM t1 GROUP BY x;
SELECT IF(x IS NULL, 'blank', 'not blank') AS x FROM v1;
SELECT IF(x IS NULL, 'blank', 'not blank') AS y FROM v1 GROUP BY y;
SELECT IF(x IS NULL, 'blank', 'not blank') AS x FROM v1 GROUP BY x;

DROP VIEW v1;
DROP TABLE t1;
2411

2412

2413
#
2414
# Bug#15943 mysql_next_result hangs on invalid SHOW CREATE VIEW
2415 2416 2417
#

delimiter //;
2418 2419 2420 2421 2422 2423
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;
2424 2425 2426
drop view v1;
//
delimiter ;//
2427

2428

2429 2430 2431 2432 2433 2434 2435
#
# Bug#17726 Not checked empty list caused endless loop
#
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);
malff/marcsql@weblab.(none)'s avatar
malff/marcsql@weblab.(none) committed
2436
create view v2 as select * from v1 where a > 1 with local check option;
2437 2438 2439 2440 2441
select * from v2;
update v2 set b=3 where a=2;
select * from v2;
drop view v2, v1;
drop table t1;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2442

2443

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2444
#
2445 2446
# Bug#18386 select from view over a table with ORDER BY view_col clause
#           given view_col is not an image of any column from the base table
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2447 2448 2449 2450 2451 2452 2453 2454 2455 2456

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;

DROP VIEW v1;
DROP TABLE t1;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2457

2458 2459 2460

#
# Bug#18237 invalid count optimization applied to an outer join with a view
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478
#

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;
SELECT * FROM t1 LEFT JOIN t2 ON t1.id=t2.id;

SELECT COUNT(*) FROM t1 LEFT JOIN v2 ON t1.id=v2.id;

DROP VIEW v2;

DROP TABLE t1, t2;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2479

2480

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2481
#
2482 2483
# Bug#16069 VIEW does return the same results as underlying SELECT
#           with WHERE condition containing BETWEEN over dates
2484
# Dates as strings should be casted to date type
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2485 2486 2487 2488

CREATE TABLE t1 (id int NOT NULL PRIMARY KEY,
                 td date DEFAULT NULL, KEY idx(td));

2489
INSERT INTO t1 VALUES
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2490 2491 2492 2493 2494 2495
 (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;

gkodinov@dl145s.mysql.com's avatar
gkodinov@dl145s.mysql.com committed
2496 2497
SELECT * FROM t1 WHERE td BETWEEN CAST('2005.01.02' AS DATE) AND CAST('2005.01.04' AS DATE);
SELECT * FROM v1 WHERE td BETWEEN CAST('2005.01.02' AS DATE) AND CAST('2005.01.04' AS DATE);
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2498 2499 2500

DROP VIEW v1;
DROP TABLE t1;
2501

2502

2503
#
2504
# Bug#14308 Recursive view definitions
2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532
#
# using view only
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;
-- error ER_VIEW_RECURSIVE
select * from v1;
drop view t1, v1;
# using SP function
create table t1 (a int);
delimiter //;
create function f1() returns int
begin
  declare mx int;
  select max(a) from t1 into mx;
  return mx;
end//
delimiter ;//
create view v1 as select f1() as a;
create view v2 as select * from v1;
drop table t1;
rename table v2 to t1;
-- error ER_SP_NO_RECURSION
select * from v1;
drop function f1;
drop view t1, v1;
2533

2534

2535
#
2536
# Bug#15153 CONVERT_TZ() is not allowed in all places in VIEWs
2537 2538 2539 2540 2541 2542 2543 2544 2545 2546
#
# Error was reported when one tried to use CONVERT_TZ() function
# select list of view which was processed using MERGE algorithm.
# (Also see additional test in timezone_grant.test)
create table t1 (dt datetime);
insert into t1 values (20040101000000), (20050101000000), (20060101000000);
# Let us test that convert_tz() can be used in view's select list
create view v1 as select convert_tz(dt, 'UTC', 'Europe/Moscow') as ldt from t1;
select * from v1;
drop view v1;
2547
# And in its where part
2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560
create view v1 as select * from t1 where convert_tz(dt, 'UTC', 'Europe/Moscow') >= 20050101000000;
select * from v1;
# Other interesting case - a view which uses convert_tz() function
# through other view.
create view v2 as select * from v1 where dt < 20060101000000;
select * from v2;
drop view v2;
# And even more interesting case when view uses convert_tz() both
# directly and indirectly
create view v2 as select convert_tz(dt, 'UTC', 'Europe/Moscow') as ldt from v1;
select * from v2;
drop view v1, v2;
drop table t1;
2561

2562

2563
#
2564 2565
# Bug#19490 usage of view specified by a query with GROUP BY
#           an expression containing non-constant interval
2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577

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

DROP VIEW v1;
DROP TABLE t1;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2578

2579

2580
#
2581
# Bug#19077 A nested materialized view is used before being populated.
2582 2583 2584 2585 2586 2587 2588 2589
#
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;
DROP VIEW v2, v1;
DROP TABLE t1;
2590

2591 2592 2593

#
# Bug#19573 VIEW with HAVING that refers an alias name
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2594 2595 2596 2597 2598 2599
#

CREATE TABLE t1(
  fName varchar(25) NOT NULL,
  lName varchar(25) NOT NULL,
  DOB date NOT NULL,
2600
  test_date date NOT NULL,
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2601
  uID int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY);
2602

2603 2604 2605 2606
INSERT INTO t1(fName, lName, DOB, test_date) VALUES
  ('Hank', 'Hill', '1964-09-29', '2007-01-01'),
  ('Tom', 'Adams', '1908-02-14', '2007-01-01'),
  ('Homer', 'Simpson', '1968-03-05', '2007-01-01');
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2607 2608

CREATE VIEW v1 AS
2609
  SELECT (year(test_date)-year(DOB)) AS Age
2610 2611
    FROM t1 HAVING Age < 75;
SHOW CREATE VIEW v1;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2612

2613
SELECT (year(test_date)-year(DOB)) AS Age FROM t1 HAVING Age < 75;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2614 2615 2616 2617 2618
SELECT * FROM v1;

DROP VIEW v1;
DROP TABLE t1;

2619

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2620
#
2621
# Bug#19089 wrong inherited dafault values in temp table views
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634
#

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;

CREATE VIEW v1(a, m) AS SELECT a, MIN(id) FROM t1 GROUP BY a;
SELECT * FROM v1;

CREATE TABLE t2 SELECT * FROM v1;
INSERT INTO t2(m) VALUES (0);
SELECT * FROM t2;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2635

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648
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;

CREATE VIEW v1(m, e) AS SELECT MIN(id), e FROM t1 GROUP BY e;
CREATE TABLE t2 SELECT * FROM v1;
SELECT * FROM t2;

DROP VIEW v1;
2649 2650
DROP TABLE t1,t2;

2651

2652
#
2653
# Bug#16110 insert permitted into view col w/o default value
2654 2655 2656 2657 2658 2659 2660
#
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);

SET SQL_MODE = STRICT_ALL_TABLES;
2661
--error ER_NO_DEFAULT_FOR_VIEW_FIELD
2662 2663 2664 2665 2666 2667 2668
INSERT INTO v1 (b) VALUES (4);
SET SQL_MODE = '';

SELECT * FROM t1;

DROP VIEW v1;
DROP TABLE t1;
2669

2670

2671
#
2672
# Bug#18243 expression over a view column that with the REVERSE function
2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685
#

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;

DROP VIEW v1;
DROP TABLE t1;
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
2686

2687

igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
2688
#
2689
# Bug#19714 wrong type of a view column specified by an expressions over ints
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
2690 2691 2692 2693 2694
#

CREATE TABLE t1 (i int, j int);
CREATE VIEW v1 AS SELECT COALESCE(i,j) FROM t1;
DESCRIBE v1;
2695
CREATE TABLE t2 SELECT COALESCE(i,j) FROM t1;
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
2696 2697 2698 2699
DESCRIBE t2;

DROP VIEW v1;
DROP TABLE t1,t2;
2700

2701

2702
#
2703
# Bug#17526 views with TRIM functions
2704 2705 2706 2707 2708 2709 2710 2711 2712 2713
#

CREATE TABLE t1 (s varchar(10));
INSERT INTO t1 VALUES ('yadda'), ('yady');

SELECT TRIM(BOTH 'y' FROM s) FROM t1;
CREATE VIEW v1 AS SELECT TRIM(BOTH 'y' FROM s) FROM t1;
SELECT * FROM v1;
DROP VIEW v1;

2714
SELECT TRIM(LEADING 'y' FROM s) FROM t1;
2715 2716 2717 2718
CREATE VIEW v1 AS SELECT TRIM(LEADING 'y' FROM s) FROM t1;
SELECT * FROM v1;
DROP VIEW v1;

2719
SELECT TRIM(TRAILING 'y' FROM s) FROM t1;
2720 2721 2722 2723 2724
CREATE VIEW v1 AS SELECT TRIM(TRAILING 'y' FROM s) FROM t1;
SELECT * FROM v1;
DROP VIEW v1;

DROP TABLE t1;
2725

2726

2727
#
2728
# Bug#21080 ALTER VIEW makes user restate SQL SECURITY mode, and ALGORITHM
2729 2730 2731 2732 2733 2734 2735 2736 2737 2738
#
CREATE TABLE t1 (x INT, y INT);
CREATE ALGORITHM=TEMPTABLE SQL SECURITY INVOKER VIEW v1 AS SELECT x FROM t1;
SHOW CREATE VIEW v1;

ALTER VIEW v1 AS SELECT x, y FROM t1;
SHOW CREATE VIEW v1;

DROP VIEW v1;
DROP TABLE t1;
2739 2740 2741 2742


# Bug#21086 server crashes when VIEW defined with a SELECT with COLLATE
#           clause is called
2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757
#
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;

# either of these statements will cause crash
INSERT INTO v1 (col) VALUES ('b');
INSERT INTO v2 (col) VALUES ('c');

SELECT s1 FROM t1;
DROP VIEW v1, v2;
DROP TABLE t1;
2758

2759

2760
#
2761
# Bug#11551 Asymmetric + undocumented behaviour of DROP VIEW and DROP TABLE
2762 2763 2764 2765 2766
#
CREATE TABLE t1 (id INT);
CREATE VIEW v1 AS SELECT id FROM t1;
SHOW TABLES;

2767
--error ER_BAD_TABLE_ERROR
2768 2769 2770 2771
DROP VIEW v2,v1;
SHOW TABLES;

CREATE VIEW v1 AS SELECT id FROM t1;
2772
--error ER_WRONG_OBJECT
2773 2774 2775 2776
DROP VIEW t1,v1;
SHOW TABLES;

DROP TABLE t1;
kostja@bodhi.local's avatar
kostja@bodhi.local committed
2777 2778 2779 2780
--disable_warnings
DROP VIEW IF EXISTS v1;
--enable_warnings

2781

kostja@bodhi.local's avatar
kostja@bodhi.local committed
2782
#
2783
# Bug#21261 Wrong access rights was required for an insert to a view
kostja@bodhi.local's avatar
kostja@bodhi.local committed
2784 2785 2786
#
CREATE DATABASE bug21261DB;
USE bug21261DB;
2787 2788
connect (root,localhost,root,,bug21261DB);
connection root;
2789

kostja@bodhi.local's avatar
kostja@bodhi.local committed
2790 2791 2792 2793 2794 2795 2796
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';

2797 2798
connect (user21261, localhost, user21261,, bug21261DB);
connection user21261;
kostja@bodhi.local's avatar
kostja@bodhi.local committed
2799 2800
INSERT INTO v1 (x) VALUES (5);
UPDATE v1 SET x=1;
2801
connection root;
kostja@bodhi.local's avatar
kostja@bodhi.local committed
2802 2803
GRANT SELECT ON v1 TO 'user21261'@'localhost';
GRANT SELECT ON t1 TO 'user21261'@'localhost';
2804
connection user21261;
kostja@bodhi.local's avatar
kostja@bodhi.local committed
2805
UPDATE v1,t2 SET x=1 WHERE x=y;
2806
connection root;
kostja@bodhi.local's avatar
kostja@bodhi.local committed
2807 2808 2809 2810 2811 2812
SELECT * FROM t1;
REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'user21261'@'localhost';
DROP USER 'user21261'@'localhost';
DROP VIEW v1;
DROP TABLE t1;
DROP DATABASE bug21261DB;
2813 2814

connection default;
kostja@bodhi.local's avatar
kostja@bodhi.local committed
2815
USE test;
2816 2817 2818
disconnect root;
disconnect user21261;

2819

kostja@bodhi.local's avatar
kostja@bodhi.local committed
2820
#
2821
# Bug#15950 NOW() optimized away in VIEWs
kostja@bodhi.local's avatar
kostja@bodhi.local committed
2822 2823 2824 2825 2826 2827
#
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;
drop view v1;
drop table t1;
2828 2829


2830
#
2831
# Test for Bug#16899 Possible buffer overflow in handling of DEFINER-clause.
2832 2833 2834 2835
#

# Prepare.

2836
--disable_warnings
2837
DROP TABLE IF EXISTS t1;
2838
DROP VIEW IF EXISTS v1;
2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854
DROP VIEW IF EXISTS v2;
--enable_warnings

CREATE TABLE t1(a INT, b INT);

--error ER_WRONG_STRING_LENGTH
CREATE DEFINER=1234567890abcdefGHIKL@localhost
  VIEW v1 AS SELECT a FROM t1;

--error ER_WRONG_STRING_LENGTH
CREATE DEFINER=some_user_name@1234567890abcdefghij1234567890abcdefghij1234567890abcdefghijQWERTY
  VIEW v2 AS SELECT b FROM t1;

# Cleanup.

DROP TABLE t1;
2855 2856


2857
#
2858
# Bug#17591 Updatable view not possible with trigger or stored function
2859 2860 2861 2862
#
# During prelocking phase we didn't update lock type of view tables,
# hence READ lock was always requested.
#
2863
--disable_warnings
2864 2865 2866 2867
DROP FUNCTION IF EXISTS f1;
DROP FUNCTION IF EXISTS f2;
DROP VIEW IF EXISTS v1, v2;
DROP TABLE IF EXISTS t1;
2868
--enable_warnings
2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893

CREATE TABLE t1 (i INT);

CREATE VIEW v1 AS SELECT * FROM t1;

delimiter |;
CREATE FUNCTION f1() RETURNS INT
BEGIN
  INSERT INTO v1 VALUES (0);
  RETURN 0;
END |
delimiter ;|

SELECT f1();

CREATE ALGORITHM=TEMPTABLE VIEW v2 AS SELECT * FROM t1;

delimiter |;
CREATE FUNCTION f2() RETURNS INT
BEGIN
  INSERT INTO v2 VALUES (0);
  RETURN 0;
END |
delimiter ;|

2894
--error ER_NON_INSERTABLE_TABLE
2895 2896 2897 2898 2899 2900 2901
SELECT f2();

DROP FUNCTION f1;
DROP FUNCTION f2;
DROP VIEW v1, v2;
DROP TABLE t1;

2902

2903
#
2904
# Bug#5500 wrong select_type in EXPLAIN output for queries over views
2905 2906
#

2907
CREATE TABLE t1 (s1 int);
2908 2909 2910 2911
CREATE VIEW v1 AS SELECT * FROM t1;

EXPLAIN SELECT * FROM t1;
EXPLAIN SELECT * FROM v1;
2912

2913 2914
INSERT INTO t1 VALUES (1), (3), (2);

2915 2916
EXPLAIN SELECT * FROM t1 t WHERE t.s1+1 < (SELECT MAX(t1.s1) FROM t1);
EXPLAIN SELECT * FROM v1 t WHERE t.s1+1 < (SELECT MAX(t1.s1) FROM t1);
2917 2918 2919

DROP VIEW v1;
DROP TABLE t1;
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2920

2921

2922
#
2923
# Bug#5505 Wrong error message on INSERT into a view
2924 2925 2926
#
create table t1 (s1 int);
create view v1 as select s1 as a, s1 as b from t1;
mats@romeo.(none)'s avatar
mats@romeo.(none) committed
2927
--error ER_NON_INSERTABLE_TABLE
2928
insert into v1 values (1,1);
2929 2930 2931 2932
update v1 set a = 5;
drop view v1;
drop table t1;

2933

igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2934
#
2935
# Bug#21646 view qith a subquery in ON expression
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2936 2937
#

2938
CREATE TABLE t1(pk int PRIMARY KEY);
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2939 2940
CREATE TABLE t2(pk int PRIMARY KEY, fk int, ver int, org int);

2941
CREATE ALGORITHM=MERGE VIEW v1 AS
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2942
SELECT t1.*
2943 2944
  FROM t1 JOIN t2
       ON t2.fk = t1.pk AND
igor@rurik.mysql.com's avatar
igor@rurik.mysql.com committed
2945 2946 2947 2948 2949 2950
          t2.ver = (SELECT MAX(t.ver) FROM t2 t WHERE t.org = t2.org);
SHOW WARNINGS;
SHOW CREATE VIEW v1;

DROP VIEW v1;
DROP TABLE t1, t2;
2951

2952

2953
#
2954
# Bug#19111 TRIGGERs selecting from a VIEW on the firing base table fail
2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984
#
# Allow to select from a view on a table being modified in a trigger
# and stored function, since plain select is allowed there.
#
--disable_warnings
DROP FUNCTION IF EXISTS f1;
DROP VIEW IF EXISTS v1;
DROP TABLE IF EXISTS t1;
--enable_warnings

CREATE TABLE t1 (i INT);
INSERT INTO t1 VALUES (1);

CREATE VIEW v1 AS SELECT MAX(i) FROM t1;

# Plain 'SET NEW.i = (SELECT MAX(i) FROM t1) + 1' works, so select
# from a view should work too.
CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW
  SET NEW.i = (SELECT * FROM v1) + 1;
INSERT INTO t1 VALUES (1);

# Plain 'RETURN (SELECT MAX(i) FROM t1)' works in INSERT, so select
# from a view should work too.
CREATE FUNCTION f1() RETURNS INT RETURN (SELECT * FROM v1);
UPDATE t1 SET i= f1();

DROP FUNCTION f1;
DROP VIEW v1;
DROP TABLE t1;

2985

2986
#
2987
# Bug#16813 (WITH CHECK OPTION doesn't work with UPDATE)
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);
2993
-- error ER_VIEW_CHECK_FAILED
2994
INSERT INTO v1 (val) VALUES (6);
2995
-- error ER_VIEW_CHECK_FAILED
2996 2997 2998 2999
UPDATE v1 SET val=6 WHERE id=2;
DROP VIEW v1;
DROP TABLE t1;

3000

3001
#
3002
# Bug#22584 last_insert_id not updated after inserting a record
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 3029 3030 3031 3032 3033 3034 3035 3036 3037
# through a updatable view
#
# We still do not update LAST_INSERT_ID if AUTO_INCREMENT column is
# not accessible through a view.  However, we do not reset the value
# of LAST_INSERT_ID, but keep it unchanged.
#
--disable_warnings
DROP VIEW IF EXISTS v1, v2;
DROP TABLE IF EXISTS t1;
--enable_warnings

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

INSERT INTO v1 (j) VALUES (2);
--echo # LAST_INSERT_ID() should not change.
SELECT LAST_INSERT_ID();

INSERT INTO v2 (j) VALUES (3);
--echo # LAST_INSERT_ID() should be updated.
SELECT LAST_INSERT_ID();

INSERT INTO v1 (j) SELECT j FROM t1;
--echo # LAST_INSERT_ID() should not change.
SELECT LAST_INSERT_ID();

SELECT * FROM t1;

DROP VIEW v1, v2;
DROP TABLE t1;

3038

malff/marcsql@weblab.(none)'s avatar
malff/marcsql@weblab.(none) committed
3039
#
3040
# Bug#25580 !0 as an operand in a select expression of a view
malff/marcsql@weblab.(none)'s avatar
malff/marcsql@weblab.(none) committed
3041 3042 3043 3044 3045 3046 3047 3048 3049 3050
#

CREATE VIEW v AS SELECT !0 * 5 AS x FROM DUAL;
SHOW CREATE VIEW v;

SELECT !0 * 5 AS x FROM DUAL;
SELECT * FROM v;

DROP VIEW v;

3051

3052
#
3053
# Bug#24293 '\Z' token is not handled correctly in views
3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065
#

--disable_warnings
DROP VIEW IF EXISTS v1;
--enable_warnings

CREATE VIEW v1 AS SELECT 'The\ZEnd';
SELECT * FROM v1;

SHOW CREATE VIEW v1;

DROP VIEW v1;
3066

3067

malff/marcsql@weblab.(none)'s avatar
malff/marcsql@weblab.(none) committed
3068
#
3069
# Bug#26124 BETWEEN over a view column of the DATETIME type
malff/marcsql@weblab.(none)'s avatar
malff/marcsql@weblab.(none) committed
3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083
#

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';
SELECT * FROM v1 WHERE mydate BETWEEN '2007-01-01' AND '2007-01-31';

DROP VIEW v1;
DROP TABLE t1;

3084

malff/marcsql@weblab.(none)'s avatar
malff/marcsql@weblab.(none) committed
3085
#
3086
# Bug#25931 update of a multi-table view with check option
malff/marcsql@weblab.(none)'s avatar
malff/marcsql@weblab.(none) committed
3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097
#

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;
3098
--error ER_VIEW_CHECK_FAILED
malff/marcsql@weblab.(none)'s avatar
malff/marcsql@weblab.(none) committed
3099 3100 3101 3102 3103 3104 3105 3106
UPDATE v1 SET b=3;
SELECT * FROM v1;
SELECT * FROM t1;
SELECT * FROM t2;

DROP VIEW v1;
DROP TABLE t1,t2;

3107

malff/marcsql@weblab.(none)'s avatar
malff/marcsql@weblab.(none) committed
3108
#
3109
# Bug#12122 Views with ORDER BY can't be resolved using MERGE algorithm.
malff/marcsql@weblab.(none)'s avatar
malff/marcsql@weblab.(none) committed
3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122
#
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;
create view v1 as select * from t1 order by f2;
select * from v1;
explain extended select * from v1;
select * from v1 order by f1;
explain extended select * from v1 order by f1;
drop view v1;
drop table t1;

#
3123
# Bug#26209 queries with GROUP BY and ORDER BY using views
malff/marcsql@weblab.(none)'s avatar
malff/marcsql@weblab.(none) committed
3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140
#

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);
SELECT code, COUNT(DISTINCT country) FROM v1 GROUP BY code ORDER BY MAX(id);

DROP VIEW v1;
DROP TABLE t1;
3141

3142

3143
#
3144
# Bug#25897 Some queries are no longer possible after a CREATE VIEW fails
3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157
#
--disable_warnings
DROP VIEW IF EXISTS v1;
--enable_warnings

let $query = SELECT * FROM (SELECT 1) AS t;

eval $query;
--error ER_VIEW_SELECT_DERIVED
eval CREATE VIEW v1 AS $query;
--echo # Previously the following would fail.
eval $query;

3158

malff/marcsql@weblab.(none)'s avatar
malff/marcsql@weblab.(none) committed
3159
#
3160
# Bug#24532 The return data type of IS TRUE is different from similar operations
malff/marcsql@weblab.(none)'s avatar
malff/marcsql@weblab.(none) committed
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
#

--disable_warnings
drop view if exists view_24532_a;
drop view if exists view_24532_b;
drop table if exists table_24532;
--enable_warnings

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;

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;

show create view view_24532_b;

insert into table_24532 values (0, 0, 0, 0);
select * from view_24532_b;
update table_24532 set a=1;
select * from view_24532_b;
update table_24532 set a=NULL;
select * from view_24532_b;

drop view view_24532_a;
drop view view_24532_b;
drop table table_24532;

3245

igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
3246
#
3247
# Bug#26560 view using subquery with a reference to an outer alias
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
3248 3249 3250 3251 3252 3253 3254 3255 3256 3257
#

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 (
3258
  id int NOT NULL PRIMARY KEY,
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
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
  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;

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;

DROP VIEW v1;
DROP table t1,t2;
3285

3286

gkodinov/kgeorge@magare.gmz's avatar
gkodinov/kgeorge@magare.gmz committed
3287
#
3288
# Bug#27786 Inconsistent Operation Performing UNION On View With ORDER BY
gkodinov/kgeorge@magare.gmz's avatar
gkodinov/kgeorge@magare.gmz committed
3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302
#
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;
EXPLAIN SELECT * FROM t1 UNION SELECT * FROM v1;
SELECT * FROM v1 UNION SELECT * FROM t1;
EXPLAIN SELECT * FROM v1 UNION SELECT * FROM t1;
SELECT * FROM t1 UNION SELECT * FROM v1 ORDER BY a;
EXPLAIN SELECT * FROM t1 UNION SELECT * FROM v1 ORDER BY a;

DROP VIEW v1;
DROP TABLE t1;

3303

3304
#
3305
# Bug#27921 View ignores precision for CAST()
3306 3307 3308 3309 3310 3311 3312 3313 3314 3315
#
CREATE VIEW v1 AS SELECT CAST( 1.23456789 AS DECIMAL( 7,5 ) ) AS col;
SELECT * FROM v1;
DESCRIBE v1;
DROP VIEW v1;

CREATE VIEW v1 AS SELECT CAST(1.23456789 AS DECIMAL(8,0)) AS col;
SHOW CREATE VIEW v1;
DROP VIEW v1;

3316

gshchepa/uchum@gleb.loc's avatar
gshchepa/uchum@gleb.loc committed
3317
#
3318 3319
# Bug#28716 CHECK OPTION expression is evaluated over expired record buffers
#           when VIEW is updated via temporary tables
gshchepa/uchum@gleb.loc's avatar
gshchepa/uchum@gleb.loc committed
3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332
#
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;
UPDATE v1 SET c=1 WHERE b=1;
SELECT * FROM v1;
DROP VIEW v1;
DROP TABLE t1,t2;

3333

igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
3334
#
3335 3336
# Bug#28561 update on multi-table view with CHECK OPTION and a subquery
#           in WHERE condition
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
3337 3338 3339 3340 3341 3342 3343
#

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

3344 3345
CREATE VIEW v1 AS
  SELECT t2.c FROM t1, t2
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
3346 3347 3348 3349 3350 3351 3352
    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;

3353

gshchepa/uchum@gleb.loc's avatar
gshchepa/uchum@gleb.loc committed
3354
#
3355 3356
# Bug#27827 CHECK OPTION ignores ON conditions when updating
#           a multi-table view with CHECK OPTION.
gshchepa/uchum@gleb.loc's avatar
gshchepa/uchum@gleb.loc committed
3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371
#

CREATE TABLE t1 (a1 INT, c INT DEFAULT 0);
CREATE TABLE t2 (a2 INT);
CREATE TABLE t3 (a3 INT);
CREATE TABLE t4 (a4 INT);
INSERT INTO t1 (a1) VALUES (1),(2);
INSERT INTO t2 (a2) VALUES (1),(2);
INSERT INTO t3 (a3) VALUES (1),(2);
INSERT INTO t4 (a4) VALUES (1),(2);

CREATE VIEW v1 AS
  SELECT t1.a1, t1.c FROM t1 JOIN t2 ON t1.a1=t2.a2 AND t1.c < 3
    WITH CHECK OPTION;
SELECT * FROM v1;
3372
--error ER_VIEW_CHECK_FAILED
gshchepa/uchum@gleb.loc's avatar
gshchepa/uchum@gleb.loc committed
3373 3374
UPDATE v1 SET c=3;
PREPARE t FROM 'UPDATE v1 SET c=3';
3375
--error ER_VIEW_CHECK_FAILED
gshchepa/uchum@gleb.loc's avatar
gshchepa/uchum@gleb.loc committed
3376
EXECUTE t;
3377
--error ER_VIEW_CHECK_FAILED
gshchepa/uchum@gleb.loc's avatar
gshchepa/uchum@gleb.loc committed
3378
EXECUTE t;
3379
--error ER_VIEW_CHECK_FAILED
gshchepa/uchum@gleb.loc's avatar
gshchepa/uchum@gleb.loc committed
3380 3381 3382 3383 3384 3385 3386 3387 3388 3389
INSERT INTO v1(a1, c) VALUES (3, 3);
UPDATE v1 SET c=1 WHERE a1=1;
SELECT * FROM v1;
SELECT * FROM t1;

CREATE VIEW v2 AS SELECT t1.a1, t1.c
  FROM (t1 JOIN t2 ON t1.a1=t2.a2 AND t1.c < 3)
  JOIN (t3 JOIN t4 ON t3.a3=t4.a4)
    ON t2.a2=t3.a3 WITH CHECK OPTION;
SELECT * FROM v2;
3390
--error ER_VIEW_CHECK_FAILED
gshchepa/uchum@gleb.loc's avatar
gshchepa/uchum@gleb.loc committed
3391 3392
UPDATE v2 SET c=3;
PREPARE t FROM 'UPDATE v2 SET c=3';
3393
--error ER_VIEW_CHECK_FAILED
gshchepa/uchum@gleb.loc's avatar
gshchepa/uchum@gleb.loc committed
3394
EXECUTE t;
3395
--error ER_VIEW_CHECK_FAILED
gshchepa/uchum@gleb.loc's avatar
gshchepa/uchum@gleb.loc committed
3396
EXECUTE t;
3397
--error ER_VIEW_CHECK_FAILED
gshchepa/uchum@gleb.loc's avatar
gshchepa/uchum@gleb.loc committed
3398 3399 3400 3401 3402 3403 3404 3405
INSERT INTO v2(a1, c) VALUES (3, 3);
UPDATE v2 SET c=2 WHERE a1=1;
SELECT * FROM v2;
SELECT * FROM t1;

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

3406

3407
#
3408 3409 3410
# Bug#29104 assertion abort for a query with a view column reference
#           in the GROUP BY list and a condition requiring the value
#           of another view column to be equal to a constant
3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430
#

CREATE TABLE t1 (a int, b int);
INSERT INTO t1 VALUES (1,2), (2,2), (1,3), (1,2);

CREATE VIEW v1 AS SELECT a, b+1 as b FROM t1;


SELECT b, SUM(a) FROM v1 WHERE b=3 GROUP BY b;
EXPLAIN SELECT b, SUM(a) FROM v1 WHERE b=3 GROUP BY b;

SELECT a, SUM(b) FROM v1 WHERE b=3 GROUP BY a;
EXPLAIN SELECT a, SUM(b) FROM v1 WHERE b=3 GROUP BY a;

SELECT a, SUM(b) FROM v1 WHERE a=1 GROUP BY a;
EXPLAIN SELECT a, SUM(b) FROM v1 WHERE a=1 GROUP BY a;

DROP VIEW v1;
DROP TABLE t1;

3431

igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
3432
#
3433 3434
# Bug#29392 SELECT over a multi-table view with ORDER BY
#           selecting the same view column with two different aliases
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457
#

CREATE TABLE t1 (
  person_id int NOT NULL PRIMARY KEY,
  username varchar(40) default NULL,
  status_flg char(1) NOT NULL default 'A'
);

CREATE TABLE t2 (
  person_role_id int NOT NULL auto_increment PRIMARY KEY,
  role_id int NOT NULL,
  person_id int NOT NULL,
  INDEX idx_person_id (person_id),
  INDEX idx_role_id (role_id)
);

CREATE TABLE t3 (
  role_id int NOT NULL auto_increment PRIMARY KEY,
  role_name varchar(100) default NULL,
  app_name varchar(40) NOT NULL,
  INDEX idx_app_name(app_name)
);

3458
CREATE VIEW v1 AS
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471
SELECT profile.person_id AS person_id
  FROM t1 profile, t2 userrole, t3 role
    WHERE userrole.person_id = profile.person_id AND
          role.role_id = userrole.role_id AND
          profile.status_flg = 'A'
  ORDER BY profile.person_id,role.app_name,role.role_name;

INSERT INTO  t1 VALUES
 (6,'Sw','A'), (-1136332546,'ols','e'), (0,'    *\n','0'),
 (-717462680,'ENTS Ta','0'), (-904346964,'ndard SQL\n','0');
INSERT INTO t2 VALUES
  (1,3,6),(2,4,7),(3,5,8),(4,6,9),(5,1,6),(6,1,7),(7,1,8),(8,1,9),(9,1,10);

3472
INSERT INTO t3 VALUES
igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
3473 3474 3475 3476 3477
  (1,'NUCANS_APP_USER','NUCANSAPP'),(2,'NUCANS_TRGAPP_USER','NUCANSAPP'),
  (3,'IA_INTAKE_COORDINATOR','IACANS'),(4,'IA_SCREENER','IACANS'),
  (5,'IA_SUPERVISOR','IACANS'),(6,'IA_READONLY','IACANS'),
  (7,'SOC_USER','SOCCANS'),(8,'CAYIT_USER','CAYITCANS'),
  (9,'RTOS_DCFSPOS_SUPERVISOR','RTOS');
3478

igor@olga.mysql.com's avatar
igor@olga.mysql.com committed
3479 3480 3481 3482 3483 3484
EXPLAIN SELECT t.person_id AS a, t.person_id AS b FROM v1 t WHERE t.person_id=6;
SELECT t.person_id AS a, t.person_id AS b FROM v1 t WHERE t.person_id=6;

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

3485

3486
#
3487 3488
# Bug#30020 Insufficient check led to a wrong info provided by the
#           information schema table.
3489 3490 3491 3492 3493
#
create table t1 (i int);
insert into t1 values (1), (2), (1), (3), (2), (4);
create view v1 as select distinct i from t1;
select * from v1;
3494
select table_name, is_updatable from information_schema.views
3495 3496 3497 3498
   where table_name = 'v1';
drop view v1;
drop table t1;

3499

gkodinov/kgeorge@macbook.gmz's avatar
gkodinov/kgeorge@macbook.gmz committed
3500
#
3501 3502
# Bug#28701 SELECTs from VIEWs completely ignore USE/FORCE KEY, allowing
#           invalid statements
gkodinov/kgeorge@macbook.gmz's avatar
gkodinov/kgeorge@macbook.gmz committed
3503 3504 3505 3506 3507
#

CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (1),(2);
CREATE VIEW v1 AS SELECT * FROM t1;
3508
--error ER_KEY_DOES_NOT_EXITS
gkodinov/kgeorge@macbook.gmz's avatar
gkodinov/kgeorge@macbook.gmz committed
3509
SELECT * FROM v1 USE KEY(non_existant);
3510
--error ER_KEY_DOES_NOT_EXITS
gkodinov/kgeorge@macbook.gmz's avatar
gkodinov/kgeorge@macbook.gmz committed
3511
SELECT * FROM v1 FORCE KEY(non_existant);
3512
--error ER_KEY_DOES_NOT_EXITS
gkodinov/kgeorge@macbook.gmz's avatar
gkodinov/kgeorge@macbook.gmz committed
3513 3514 3515 3516 3517 3518
SELECT * FROM v1 IGNORE KEY(non_existant);

DROP VIEW v1;
DROP TABLE t1;


3519
#
3520
# Bug#28702 VIEWs defined with USE/FORCE KEY ignore that request
3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539
#
CREATE TABLE t1 (a INT NOT NULL AUTO_INCREMENT, b INT NOT NULL DEFAULT 0,
                 PRIMARY KEY(a), KEY (b));
INSERT INTO t1 VALUES (),(),(),(),(),(),(),(),(),(),(),(),(),(),();
CREATE VIEW v1 AS SELECT * FROM t1 FORCE KEY (PRIMARY,b) ORDER BY a;
SHOW CREATE VIEW v1;
EXPLAIN SELECT * FROM v1;
CREATE VIEW v2 AS SELECT * FROM t1 USE KEY () ORDER BY a;
SHOW CREATE VIEW v2;
EXPLAIN SELECT * FROM v2;
CREATE VIEW v3 AS SELECT * FROM t1 IGNORE KEY (b) ORDER BY a;
SHOW CREATE VIEW v3;
EXPLAIN SELECT * FROM v3;

DROP VIEW v1;
DROP VIEW v2;
DROP VIEW v3;
DROP TABLE t1;

3540

3541
--echo #
3542 3543
--echo # Bug#29477 Not all fields of the target table were checked to have
--echo #           a default value when inserting into a view.
3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554
--echo #
create table t1(f1 int, f2 int not null);
create view v1 as select f1 from t1;
insert into v1 values(1);
set @old_mode=@@sql_mode;
set @@sql_mode=traditional;
--error ER_NO_DEFAULT_FOR_VIEW_FIELD
insert into v1 values(1);
set @@sql_mode=@old_mode;
drop view v1;
drop table t1;
3555

3556

3557
#
3558 3559
# Bug#33389 Selecting from a view into a table from within SP or trigger
#           crashes server
3560 3561 3562 3563
#

create table t1 (a int, key(a));
create table t2 (c int);
3564

3565
create view v1 as select a b from t1;
3566
create view v2 as select 1 a from t2, v1 where c in
3567
                  (select 1 from t1 where b = a);
3568

3569 3570
insert into t1 values (1), (1);
insert into t2 values (1), (1);
3571

3572
prepare stmt from "select * from v2 where a = 1";
3573
execute stmt;
3574 3575 3576 3577

drop view v1, v2;
drop table t1, t2;

3578

3579
#
3580
# Bug#33049 Assert while running test-as3ap test(mysql-bench suite)
3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591
#

CREATE TABLE t1 (a INT);
CREATE VIEW v1 AS SELECT p.a AS a FROM t1 p, t1 q;

INSERT INTO t1 VALUES (1), (1);
SELECT MAX(a), COUNT(DISTINCT a) FROM v1 GROUP BY a;

DROP VIEW v1;
DROP TABLE t1;

3592 3593 3594
###########################################################################

--echo # -----------------------------------------------------------------
3595
--echo # -- Bug#34337 Server crash when Altering a view using a table name.
3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623
--echo # -----------------------------------------------------------------
--echo

--disable_warnings
DROP TABLE IF EXISTS t1;
--enable_warnings

--echo

CREATE TABLE t1(c1 INT);

--echo

SELECT * FROM t1;

--error ER_WRONG_OBJECT
ALTER ALGORITHM=TEMPTABLE SQL SECURITY INVOKER VIEW t1 (c2) AS SELECT (1);

--echo

DROP TABLE t1;

--echo
--echo # -- End of test case for Bug#34337.
--echo

###########################################################################

gshchepa/uchum@host.loc's avatar
gshchepa/uchum@host.loc committed
3624
--echo # -----------------------------------------------------------------
3625 3626
--echo # -- Bug#35193 VIEW query is rewritten without "FROM DUAL",
--echo # --           causing syntax error
gshchepa/uchum@host.loc's avatar
gshchepa/uchum@host.loc committed
3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646
--echo # -----------------------------------------------------------------
--echo

CREATE VIEW v1 AS SELECT 1 FROM DUAL WHERE 1;

--echo

SELECT * FROM v1;
SHOW CREATE TABLE v1;

--echo

DROP VIEW v1;

--echo
--echo # -- End of test case for Bug#35193.
--echo

###########################################################################

3647
#
3648 3649
# Bug#39040 valgrind errors/crash when creating views with binlog logging
#           enabled
3650 3651 3652 3653 3654
#
# Bug is visible only when running in valgrind with binary logging.
CREATE VIEW v1 AS SELECT 1;
DROP VIEW v1;

3655

3656
#
3657
# Bug#33461 SELECT ... FROM <view> USE INDEX (...) throws an error
3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681
#

CREATE TABLE t1 (c1 INT PRIMARY KEY, c2 INT, INDEX (c2));
INSERT INTO t1 VALUES (1,1), (2,2), (3,3);
SELECT * FROM t1 USE INDEX (PRIMARY) WHERE c1=2;
SELECT * FROM t1 USE INDEX (c2) WHERE c2=2;

CREATE VIEW v1 AS SELECT c1, c2 FROM t1;
SHOW INDEX FROM v1;
--error ER_KEY_DOES_NOT_EXITS
SELECT * FROM v1 USE INDEX (PRIMARY) WHERE c1=2;
--error ER_KEY_DOES_NOT_EXITS
SELECT * FROM v1 FORCE INDEX (PRIMARY) WHERE c1=2;
--error ER_KEY_DOES_NOT_EXITS
SELECT * FROM v1 IGNORE INDEX (PRIMARY) WHERE c1=2;
--error ER_KEY_DOES_NOT_EXITS
SELECT * FROM v1 USE INDEX (c2) WHERE c2=2;
--error ER_KEY_DOES_NOT_EXITS
SELECT * FROM v1 FORCE INDEX (c2) WHERE c2=2;
--error ER_KEY_DOES_NOT_EXITS
SELECT * FROM v1 IGNORE INDEX (c2) WHERE c2=2;

DROP VIEW v1;
DROP TABLE t1;
3682

3683 3684 3685
--echo # -----------------------------------------------------------------
--echo # -- End of 5.0 tests.
--echo # -----------------------------------------------------------------
3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704

#
# Bug#21370 View renaming lacks tablename_to_filename encoding
#
--disable_warnings
DROP DATABASE IF EXISTS `d-1`;
--enable_warnings
CREATE DATABASE `d-1`;
USE `d-1`;
CREATE TABLE `t-1` (c1 INT);
CREATE VIEW  `v-1` AS SELECT c1 FROM `t-1`;
SHOW TABLES;
RENAME TABLE `t-1` TO `t-2`;
RENAME TABLE `v-1` TO `v-2`;
SHOW TABLES;
DROP TABLE `t-2`;
DROP VIEW  `v-2`;
DROP DATABASE `d-1`;
USE test;
3705

3706 3707
--echo
--echo #
3708
--echo # Bug#26676 VIEW using old table schema in a session.
3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756
--echo #
--echo

--disable_warnings
DROP VIEW IF EXISTS v1;
DROP TABLE IF EXISTS t1;
--enable_warnings

CREATE TABLE t1(c1 INT, c2 INT);
INSERT INTO t1 VALUES (1, 2), (3, 4);

--echo

SELECT * FROM t1;

--echo

CREATE VIEW v1 AS SELECT * FROM t1;

--echo

SELECT * FROM v1;

--echo

ALTER TABLE t1 ADD COLUMN c3 INT AFTER c2;

--echo

SELECT * FROM t1;

--echo

SELECT * FROM v1;

--echo

SHOW CREATE VIEW v1;

--echo

DROP VIEW v1;
DROP TABLE t1;

--echo
--echo # End of test case for Bug#26676.
--echo

3757 3758 3759
###########################################################################

--echo # -----------------------------------------------------------------
3760
--echo # -- Bug#32538 View definition picks up character set, but not collation
3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815
--echo # -----------------------------------------------------------------
--echo

--disable_warnings
DROP VIEW IF EXISTS v1;
--enable_warnings

--echo

SET collation_connection = latin1_general_ci;
CREATE VIEW v1 AS SELECT _latin1 'text1' AS c1, 'text2' AS c2;

--echo

SELECT COLLATION(c1), COLLATION(c2) FROM v1;

--echo

SHOW CREATE VIEW v1;

--echo

--error ER_CANT_AGGREGATE_2COLLATIONS
SELECT * FROM v1 WHERE c1 = 'text1';

--echo

SELECT * FROM v1 WHERE c2 = 'text2';

--echo

use test;
SET names latin1;

--echo

SELECT COLLATION(c1), COLLATION(c2) FROM v1;

--echo

SELECT * FROM v1 WHERE c1 = 'text1';

--echo

--error ER_CANT_AGGREGATE_2COLLATIONS
SELECT * FROM v1 WHERE c2 = 'text2';

--echo

DROP VIEW v1;

--echo
--echo # -- End of test case for Bug#32538.
--echo

3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833
#
# Bug#34587 Creating a view inside a stored procedure leads to a server crash
#

--disable_warnings
drop view if exists a;
drop procedure if exists p;
--enable_warnings

delimiter |;
create procedure p()
begin
  declare continue handler for sqlexception begin end;
  create view a as select 1;
end|
delimiter ;|
call p();
call p();
3834
drop view a;
3835 3836
drop procedure p;

3837 3838 3839 3840 3841
###########################################################################

--echo # -----------------------------------------------------------------
--echo # -- End of 5.1 tests.
--echo # -----------------------------------------------------------------