2
# Test of update statement that uses many tables.
5
source include/have_log_bin.inc;
8
drop table if exists t1,t2,t3;
9
drop database if exists mysqltest;
12
create table t1(id1 int not null auto_increment primary key, t char(12));
13
create table t2(id2 int not null, t char(12));
14
create table t3(id3 int not null, t char(12), index(id3));
20
eval insert into t1(t) values ('$1');
23
eval insert into t2(id2,t) values ($1,'$2');
27
eval insert into t3(id3,t) values ($1,'$2');
36
select count(*) from t1 where id1 > 95;
37
select count(*) from t2 where id2 > 95;
38
select count(*) from t3 where id3 > 95;
40
update t1,t2,t3 set t1.t="aaa", t2.t="bbb", t3.t="cc" where t1.id1 = t2.id2 and t2.id2 = t3.id3 and t1.id1 > 90;
41
select count(*) from t1 where t = "aaa";
42
select count(*) from t1 where id1 > 90;
43
select count(*) from t2 where t = "bbb";
44
select count(*) from t2 where id2 > 90;
45
select count(*) from t3 where t = "cc";
46
select count(*) from t3 where id3 > 90;
47
delete t1.*, t2.*, t3.* from t1,t2,t3 where t1.id1 = t2.id2 and t2.id2 = t3.id3 and t1.id1 > 95;
49
check table t1, t2, t3;
51
select count(*) from t1 where id1 > 95;
52
select count(*) from t2 where id2 > 95;
53
select count(*) from t3 where id3 > 95;
55
delete t1, t2, t3 from t1,t2,t3 where t1.id1 = t2.id2 and t2.id2 = t3.id3 and t1.id1 > 5;
56
select count(*) from t1 where id1 > 5;
57
select count(*) from t2 where id2 > 5;
58
select count(*) from t3 where id3 > 5;
60
delete from t1, t2, t3 using t1,t2,t3 where t1.id1 = t2.id2 and t2.id2 = t3.id3 and t1.id1 > 0;
62
# These queries will force a scan of the table
63
select count(*) from t1 where id1;
64
select count(*) from t2 where id2;
65
select count(*) from t3 where id3;
68
create table t1(id1 int not null primary key, t varchar(100)) pack_keys = 1;
69
create table t2(id2 int not null, t varchar(100), index(id2)) pack_keys = 1;
75
eval insert into t1 values ($1,'aaaaaaaaaaaaaaaaaaaa');
78
eval insert into t2(id2,t) values ($1,'bbbbbbbbbbbbbbbbb');
84
delete t1 from t1,t2 where t1.id1 = t2.id2 and t1.id1 > 500;
88
id int NOT NULL default '0',
89
name varchar(10) default NULL,
92
INSERT INTO t1 VALUES (1,'aaa'),(2,'aaa'),(3,'aaa');
94
id int NOT NULL default '0',
95
name varchar(10) default NULL,
98
INSERT INTO t2 VALUES (2,'bbb'),(3,'bbb'),(4,'bbb');
100
id int NOT NULL default '0',
101
mydate datetime default NULL,
104
INSERT INTO t3 VALUES (1,'2002-02-04 00:00:00'),(3,'2002-05-12 00:00:00'),(5,'2002-05-12 00:00:00'),(6,'2002-06-22
105
00:00:00'),(7,'2002-07-22 00:00:00');
106
delete t1,t2,t3 from t1,t2,t3 where to_days(now())-to_days(t3.mydate)>=30 and t3.id=t1.id and t3.id=t2.id;
110
CREATE TABLE IF NOT EXISTS `t1` (
111
`id` int NOT NULL auto_increment,
117
CREATE TABLE IF NOT EXISTS `t2` (
118
`ID` int NOT NULL auto_increment,
119
`ParId` int default NULL,
123
KEY `IX_ParId_t2` (`ParId`),
124
FOREIGN KEY (`ParId`) REFERENCES `t1` (`id`)
127
INSERT INTO t1(tst,tst1) VALUES("MySQL","MySQL AB"), ("MSSQL","Microsoft"), ("ORACLE","ORACLE");
129
INSERT INTO t2(ParId) VALUES(1), (2), (3);
133
UPDATE t2, t1 SET t2.tst = t1.tst, t2.tst1 = t1.tst1 WHERE t2.ParId = t1.Id;
138
create table t1 (n numeric(10));
139
create table t2 (n numeric(10));
140
insert into t2 values (1),(2),(4),(8),(16),(32);
141
select * from t2 left outer join t1 using (n);
142
delete t1,t2 from t2 left outer join t1 using (n);
143
select * from t2 left outer join t1 using (n);
150
create table t1 (n int not null primary key, d int);
151
create table t2 (n int not null primary key, d int);
152
insert into t1 values(1,1);
153
insert into t2 values(1,10),(2,20);
154
LOCK TABLES t1 write, t2 read;
156
DELETE t1.*, t2.* FROM t1,t2 where t1.n=t2.n;
158
UPDATE t1,t2 SET t1.d=t2.d,t2.d=30 WHERE t1.n=t2.n;
159
UPDATE t1,t2 SET t1.d=t2.d WHERE t1.n=t2.n;
161
LOCK TABLES t1 write, t2 write;
162
UPDATE t1,t2 SET t1.d=t2.d WHERE t1.n=t2.n;
164
DELETE t1.*, t2.* FROM t1,t2 where t1.n=t2.n;
171
# Test safe updates and timestamps
173
set sql_safe_updates=1;
174
create table t1 (n int, d int);
175
create table t2 (n int, d int);
176
insert into t1 values(1,1);
177
insert into t2 values(1,10),(2,20);
179
UPDATE t1,t2 SET t1.d=t2.d WHERE t1.n=t2.n;
180
set sql_safe_updates=0;
182
set timestamp=1038401397;
183
create table t1 (n int not null primary key, d int, t timestamp);
184
create table t2 (n int not null primary key, d int, t timestamp);
185
insert into t1 values(1,1,NULL);
186
insert into t2 values(1,10,NULL),(2,20,NULL);
187
set timestamp=1038000000;
188
UPDATE t1,t2 SET t1.d=t2.d WHERE t1.n=t2.n;
189
select n,d,unix_timestamp(t) from t1;
190
select n,d,unix_timestamp(t) from t2;
192
UPDATE t1,t2 SET 1=2 WHERE t1.n=t2.n;
195
set sql_safe_updates=0;
196
create table t1 (n int not null primary key, d int);
197
create table t2 (n int not null primary key, d int);
198
insert into t1 values(1,1), (3,3);
199
insert into t2 values(1,10),(2,20);
200
UPDATE t2 left outer join t1 on t1.n=t2.n SET t1.d=t2.d;
204
create table t1 (n int, d int);
205
create table t2 (n int, d int);
206
insert into t1 values(1,1),(1,2);
207
insert into t2 values(1,10),(2,20);
208
UPDATE t1,t2 SET t1.d=t2.d,t2.d=30 WHERE t1.n=t2.n;
212
create table t1 (n int, d int);
213
create table t2 (n int, d int);
214
insert into t1 values(1,1),(3,2);
215
insert into t2 values(1,10),(1,20);
216
UPDATE t1,t2 SET t1.d=t2.d,t2.d=30 WHERE t1.n=t2.n;
219
UPDATE t1 a ,t2 b SET a.d=b.d,b.d=30 WHERE a.n=b.n;
222
DELETE a, b FROM t1 a,t2 b where a.n=b.n;
227
CREATE TABLE t1 ( broj int NOT NULL default '0', naziv char(25) NOT NULL default 'NEPOZNAT', PRIMARY KEY (broj)) ENGINE=MyISAM;
228
INSERT INTO t1 VALUES (1,'jedan'),(2,'dva'),(3,'tri'),(4,'xxxxxxxxxx'),(5,'a'),(10,''),(11,''),(12,''),(13,'');
229
CREATE TABLE t2 ( broj int NOT NULL default '0', naziv char(25) NOT NULL default 'NEPOZNAT', PRIMARY KEY (broj)) ENGINE=MyISAM;
230
INSERT INTO t2 VALUES (1,'jedan'),(2,'dva'),(3,'tri'),(4,'xxxxxxxxxx'),(5,'a');
231
CREATE TABLE t3 ( broj int NOT NULL default '0', naziv char(25) NOT NULL default 'NEPOZNAT', PRIMARY KEY (broj)) ENGINE=MyISAM;
232
INSERT INTO t3 VALUES (1,'jedan'),(2,'dva');
233
update t1,t2 set t1.naziv="aaaa" where t1.broj=t2.broj;
234
update t1,t2,t3 set t1.naziv="bbbb", t2.naziv="aaaa" where t1.broj=t2.broj and t2.broj=t3.broj;
238
# Test multi update with different join methods
241
CREATE TABLE t1 (a int not null primary key, b int not null, key (b));
242
CREATE TABLE t2 (a int not null primary key, b int not null, key (b));
243
INSERT INTO t1 values (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9);
244
INSERT INTO t2 values (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9);
246
# Full join, without key
247
update t1,t2 set t1.a=t1.a+100;
251
update t1,t2 set t1.a=t1.a+100 where t1.a=101;
255
update t1,t2 set t1.b=t1.b+10 where t1.b=2;
259
update t1,t2 set t1.b=t1.b+2,t2.b=t1.b+10 where t1.b between 3 and 5 and t2.a=t1.a-100;
263
# test for non-updating table which is also used in sub-select
265
update t1,t2 set t1.b=t2.b, t1.a=t2.a where t1.a=t2.a and not exists (select * from t2 where t2.a > 10);
268
CREATE TABLE t3 ( KEY1 varchar(50) NOT NULL default '', PARAM_CORR_DISTANCE_RUSH double default NULL, PARAM_CORR_DISTANCE_GEM double default NULL, PARAM_AVG_TARE double default NULL, PARAM_AVG_NB_DAYS double default NULL, PARAM_DEFAULT_PROP_GEM_SRVC varchar(50) default NULL, PARAM_DEFAULT_PROP_GEM_NO_ETIK varchar(50) default NULL, PARAM_SCENARIO_COSTS varchar(50) default NULL, PARAM_DEFAULT_WAGON_COST double default NULL, tmp int default NULL, PRIMARY KEY (KEY1)) ENGINE=MyISAM;
269
INSERT INTO t3 VALUES ('A',1,1,22,3.2,'R','R','BASE2',0.24,NULL);
270
create table t1 (A varchar(1));
271
insert into t1 values ("A") ,("B"),("C"),("D");
272
create table t2(Z varchar(15));
273
insert into t2(Z) select concat(a.a,b.a,c.a,d.a) from t1 as a, t1 as b, t1 as c, t1 as d;
274
update t2,t3 set Z =param_scenario_costs;
276
create table t1 (a int, b int);
277
create table t2 (a int, b int);
278
insert into t1 values (1,1),(2,1),(3,1);
279
insert into t2 values (1,1), (3,1);
280
update t1 left join t2 on t1.a=t2.a set t1.b=2, t2.b=2 where t1.b=1 and t2.b=1 or t2.a is NULL;
281
select t1.a, t1.b,t2.a, t2.b from t1 left join t2 on t1.a=t2.a where t1.b=1 and t2.b=1 or t2.a is NULL;
285
# Test reuse of same table
288
create table t1 (a int not null auto_increment primary key, b int not null);
289
insert into t1 (b) values (1),(2),(3),(4);
290
update t1, t1 as t2 set t1.b=t2.b+1 where t1.a=t2.a;
294
# Test multi-update and multi-delete with impossible where
296
create table t1(id1 int, field char(5));
297
create table t2(id2 int, field char(5));
299
insert into t1 values (1, 'a'), (2, 'aa');
300
insert into t2 values (1, 'b'), (2, 'bb');
305
update t2 inner join t1 on t1.id1=t2.id2
306
set t2.field=t1.field
308
update t2, t1 set t2.field=t1.field
309
where t1.id1=t2.id2 and 0=1;
311
delete t1, t2 from t2 inner join t1 on t1.id1=t2.id2
313
delete t1, t2 from t2,t1
314
where t1.id1=t2.id2 and 0=1;
319
# Test for bug #1820.
322
create table t1 ( a int not null, b int not null) ;
324
insert into t1 values (1,1),(2,2),(3,3),(4,4);
329
eval insert into t1 select a+@d,b+@d from t1;
335
alter table t1 add index i1(a);
336
delete from t1 where a > 2000000;
337
create table t2 like t1;
338
insert into t2 select * from t1;
340
select 't2 rows before small delete', count(*) from t1;
341
delete t1,t2 from t1,t2 where t1.b=t2.a and t1.a < 2;
342
select 't2 rows after small delete', count(*) from t2;
343
select 't1 rows after small delete', count(*) from t1;
345
## Try deleting many rows
347
delete t1,t2 from t1,t2 where t1.b=t2.a and t1.a < 100*1000;
348
select 't2 rows after big delete', count(*) from t2;
349
select 't1 rows after big delete', count(*) from t1;
354
# Test alias (this is not correct in 4.0)
357
CREATE TABLE t1 ( a int );
358
CREATE TABLE t2 ( a int );
359
DELETE t1 FROM t1, t2 AS t3;
360
DELETE t4 FROM t1, t1 AS t4;
361
DELETE t3 FROM t1 AS t3, t1 AS t4;
363
DELETE t1 FROM t1 AS t3, t2 AS t4;
364
INSERT INTO t1 values (1),(2);
365
INSERT INTO t2 values (1),(2);
366
DELETE t1 FROM t1 AS t2, t2 AS t1 where t1.a=t2.a and t1.a=1;
369
DELETE t2 FROM t1 AS t2, t2 AS t1 where t1.a=t2.a and t1.a=2;
375
# Test update with const tables
377
create table `t1` (`p_id` int NOT NULL auto_increment, `p_code` varchar(20) NOT NULL default '', `p_active` int NOT NULL default '1', PRIMARY KEY (`p_id`) );
378
create table `t2` (`c2_id` int NULL auto_increment, `c2_p_id` int NOT NULL default '0', `c2_note` text NOT NULL, `c2_active` int NOT NULL default '1', PRIMARY KEY (`c2_id`), KEY `c2_p_id` (`c2_p_id`) );
379
insert into t1 values (0,'A01-Comp',1);
380
insert into t1 values (0,'B01-Comp',1);
381
insert into t2 values (0,1,'A Note',1);
382
update t1 left join t2 on p_id = c2_p_id set c2_note = 'asdf-1' where p_id = 2;
388
# multi delete wrong table check
390
create table t1 (a int, primary key (a));
391
create table t2 (a int, primary key (a));
392
create table t3 (a int, primary key (a));
393
delete t1,t3 from t1,t2 where t1.a=t2.a and t2.a=(select t3.a from t3 where t1.a=t3.a);
394
drop table t1, t2, t3;
397
# multi* unique updating table check
399
create table t1 (col1 int);
400
create table t2 (col1 int);
401
update t1,t2 set t1.col1 = (select max(col1) from t1) where t1.col1 = t2.col1;
402
delete t1 from t1,t2 where t1.col1 < (select max(col1) from t1) and t1.col1 = t2.col1;
405
# Test for BUG#5837 - delete with outer join and const tables
408
aclid bigint not null primary key,
413
refid bigint not null primary key,
414
aclid bigint, index idx_acl(aclid)
417
insert into t2 values(1,null);
418
delete t2, t1 from t2 left join t1 on (t2.aclid=t1.aclid) where t2.refid='1';
422
# Bug#19225: unchecked error leads to server crash
424
create table t1(a int);
425
create table t2(a int);
427
delete from t1,t2 using t1,t2 where t1.a=(select a from t1);
432
# Test for bug #1980.
435
create table t1 ( c char(8) not null ) engine=innodb;
438
insert into t1 values ('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9');
439
insert into t1 values ('A'),('B'),('C'),('D'),('E'),('F');
441
alter table t1 add b char(8) not null;
442
alter table t1 add a char(8) not null;
443
alter table t1 add primary key (a,b,c);
444
update t1 set a=c, b=c;
446
create table t2 like t1;
447
insert into t2 select * from t1;
449
delete t1,t2 from t2,t1 where t1.a<'B' and t2.b=t1.b;
454
create table t1 ( c char(8) not null ) engine=innodb;
457
insert into t1 values ('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9');
458
insert into t1 values ('A'),('B'),('C'),('D'),('E'),('F');
460
alter table t1 add b char(8) not null;
461
alter table t1 add a char(8) not null;
462
alter table t1 add primary key (a,b,c);
463
update t1 set a=c, b=c;
465
create table t2 like t1;
466
insert into t2 select * from t1;
468
delete t1,t2 from t2,t1 where t1.a<'B' and t2.b=t1.b;
473
# Test alter table and a concurrent multi update
474
# (This will force update to reopen tables)
477
create table t1 (a int, b int);
478
insert into t1 values (1, 2), (2, 3), (3, 4);
479
create table t2 (a int);
480
insert into t2 values (10), (20), (30);
481
create table v1 as select a as b, a/10 as a from t2;
483
connect (locker,localhost,root,,test);
487
connect (changer,localhost,root,,test);
489
send alter table t1 add column c int default 100 after a;
491
connect (updater,localhost,root,,test);
494
send update t1, v1 set t1.b=t1.a+t1.b+v1.b where t1.a=v1.a;
511
# Test multi updates and deletes using primary key and without.
513
create table t1 (i1 int, i2 int, i3 int);
514
create table t2 (id int, c1 varchar(20), c2 varchar(20));
515
insert into t1 values (1,5,10),(3,7,12),(4,5,2),(9,10,15),(2,2,2);
516
insert into t2 values (9,"abc","def"),(5,"opq","lmn"),(2,"test t","t test");
517
select * from t1 order by i1;
519
update t1,t2 set t1.i2=15, t2.c2="ppc" where t1.i1=t2.id;
520
select * from t1 order by i1;
521
select * from t2 order by id;
522
delete t1.*,t2.* from t1,t2 where t1.i2=t2.id;
523
select * from t1 order by i1;
524
select * from t2 order by id;
526
create table t1 (i1 int auto_increment not null, i2 int, i3 int, primary key (i1));
527
create table t2 (id int auto_increment not null, c1 varchar(20), c2 varchar(20), primary key(id));
528
insert into t1 values (1,5,10),(3,7,12),(4,5,2),(9,10,15),(2,2,2);
529
insert into t2 values (9,"abc","def"),(5,"opq","lmn"),(2,"test t","t test");
530
select * from t1 order by i1;
531
select * from t2 order by id;
532
update t1,t2 set t1.i2=15, t2.c2="ppc" where t1.i1=t2.id;
533
select * from t1 order by i1;
534
select * from t2 order by id;
535
delete t1.*,t2.* from t1,t2 where t1.i2=t2.id;
536
select * from t1 order by i1;
537
select * from t2 order by id;
541
# Bug#27716 multi-update did partially and has not binlogged
544
`a` int NOT NULL auto_increment,
545
`b` int default NULL,
550
`a` int NOT NULL auto_increment,
551
`b` int default NULL,
556
# @TODO Only row format now...commenting out.
557
# as the test is about to see erroed queries in binlog
558
#set @sav_binlog_format= @@session.binlog_format;
559
#set @@session.binlog_format= mixed;
562
# A. testing multi_update::send_error() effective update
563
insert into t1 values (1,1),(2,2);
564
insert into t2 values (1,1),(4,4);
567
UPDATE t2,t1 SET t2.a=t1.a+2;
569
select * from t2 /* must be (3,1), (4,4) */;
570
show master status /* there must be the UPDATE query event */;
572
# B. testing multi_update::send_error() ineffective update
573
# (as there is a policy described at mysql_update() still go to binlog)
576
insert into t1 values (1,2),(3,4),(4,4);
577
insert into t2 values (1,2),(3,4),(4,4);
580
UPDATE t2,t1 SET t2.a=t2.b where t2.a=t1.a;
581
show master status /* there must be the UPDATE query event */;
585
#set @@session.binlog_format= @sav_binlog_format;
588
# Bug #29136 erred multi-delete on trans table does not rollback
590
# @TODO JRP: This doesn't test any rollback! Commenting out...
593
#drop table if exists t1, t2, t3;
595
#CREATE TABLE t1 (a int, PRIMARY KEY (a));
596
#CREATE TABLE t2 (a int, PRIMARY KEY (a));
597
#CREATE TABLE t3 (a int, PRIMARY KEY (a)) ENGINE=MyISAM;
598
#create trigger trg_del_t3 before delete on t3 for each row insert into t1 values (1);
600
#insert into t2 values (1),(2);
601
#insert into t3 values (1),(2);
604
# exec cases B, A - see innodb.test
606
# B. send_eof() and send_error() afterward
608
#--error ER_DUP_ENTRY
609
#delete t3.* from t2,t3 where t2.a=t3.a;
612
#select count(*) from t1 /* must be 1 */;
613
#select count(*) from t3 /* must be 1 */;
616
#drop table t1, t2, t3;
619
# Add further tests from here