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));
21
eval insert into t1(t) values ('$1');
24
eval insert into t2(id2,t) values ($1,'$2');
28
eval insert into t3(id3,t) values ($1,'$2');
38
select count(*) from t1 where id1 > 95;
39
select count(*) from t2 where id2 > 95;
40
select count(*) from t3 where id3 > 95;
42
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;
43
select count(*) from t1 where t = "aaa";
44
select count(*) from t1 where id1 > 90;
45
select count(*) from t2 where t = "bbb";
46
select count(*) from t2 where id2 > 90;
47
select count(*) from t3 where t = "cc";
48
select count(*) from t3 where id3 > 90;
49
delete t1.*, t2.*, t3.* from t1,t2,t3 where t1.id1 = t2.id2 and t2.id2 = t3.id3 and t1.id1 > 95;
51
check table t1, t2, t3;
53
select count(*) from t1 where id1 > 95;
54
select count(*) from t2 where id2 > 95;
55
select count(*) from t3 where id3 > 95;
57
delete t1, t2, t3 from t1,t2,t3 where t1.id1 = t2.id2 and t2.id2 = t3.id3 and t1.id1 > 5;
58
select count(*) from t1 where id1 > 5;
59
select count(*) from t2 where id2 > 5;
60
select count(*) from t3 where id3 > 5;
62
delete from t1, t2, t3 using t1,t2,t3 where t1.id1 = t2.id2 and t2.id2 = t3.id3 and t1.id1 > 0;
64
# These queries will force a scan of the table
65
select count(*) from t1 where id1;
66
select count(*) from t2 where id2;
67
select count(*) from t3 where id3;
70
create table t1(id1 int not null primary key, t varchar(100)) pack_keys = 1;
71
create table t2(id2 int not null, t varchar(100), index(id2)) pack_keys = 1;
78
eval insert into t1 values ($1,'aaaaaaaaaaaaaaaaaaaa');
81
eval insert into t2(id2,t) values ($1,'bbbbbbbbbbbbbbbbb');
88
delete t1 from t1,t2 where t1.id1 = t2.id2 and t1.id1 > 500;
92
id int NOT NULL default '0',
93
name varchar(10) default NULL,
96
INSERT INTO t1 VALUES (1,'aaa'),(2,'aaa'),(3,'aaa');
98
id int NOT NULL default '0',
99
name varchar(10) default NULL,
102
INSERT INTO t2 VALUES (2,'bbb'),(3,'bbb'),(4,'bbb');
104
id int NOT NULL default '0',
105
mydate datetime default NULL,
108
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
109
00:00:00'),(7,'2002-07-22 00:00:00');
110
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;
114
CREATE TABLE IF NOT EXISTS `t1` (
115
`id` int NOT NULL auto_increment,
121
CREATE TABLE IF NOT EXISTS `t2` (
122
`ID` int NOT NULL auto_increment,
123
`ParId` int default NULL,
127
KEY `IX_ParId_t2` (`ParId`),
128
FOREIGN KEY (`ParId`) REFERENCES `t1` (`id`)
131
INSERT INTO t1(tst,tst1) VALUES("MySQL","MySQL AB"), ("MSSQL","Microsoft"), ("ORACLE","ORACLE");
133
INSERT INTO t2(ParId) VALUES(1), (2), (3);
137
UPDATE t2, t1 SET t2.tst = t1.tst, t2.tst1 = t1.tst1 WHERE t2.ParId = t1.Id;
142
create table t1 (n numeric(10));
143
create table t2 (n numeric(10));
144
insert into t2 values (1),(2),(4),(8),(16),(32);
145
select * from t2 left outer join t1 using (n);
146
delete t1,t2 from t2 left outer join t1 using (n);
147
select * from t2 left outer join t1 using (n);
154
create table t1 (n int not null primary key, d int);
155
create table t2 (n int not null primary key, d int);
156
insert into t1 values(1,1);
157
insert into t2 values(1,10),(2,20);
158
LOCK TABLES t1 write, t2 read;
160
DELETE t1.*, t2.* FROM t1,t2 where t1.n=t2.n;
162
UPDATE t1,t2 SET t1.d=t2.d,t2.d=30 WHERE t1.n=t2.n;
163
UPDATE t1,t2 SET t1.d=t2.d WHERE t1.n=t2.n;
165
LOCK TABLES t1 write, t2 write;
166
UPDATE t1,t2 SET t1.d=t2.d WHERE t1.n=t2.n;
168
DELETE t1.*, t2.* FROM t1,t2 where t1.n=t2.n;
175
# Test safe updates and timestamps
177
set sql_safe_updates=1;
178
create table t1 (n int, d int);
179
create table t2 (n int, d int);
180
insert into t1 values(1,1);
181
insert into t2 values(1,10),(2,20);
183
UPDATE t1,t2 SET t1.d=t2.d WHERE t1.n=t2.n;
184
set sql_safe_updates=0;
186
set timestamp=1038401397;
187
create table t1 (n int not null primary key, d int, t timestamp);
188
create table t2 (n int not null primary key, d int, t timestamp);
189
insert into t1 values(1,1,NULL);
190
insert into t2 values(1,10,NULL),(2,20,NULL);
191
set timestamp=1038000000;
192
UPDATE t1,t2 SET t1.d=t2.d WHERE t1.n=t2.n;
193
select n,d,unix_timestamp(t) from t1;
194
select n,d,unix_timestamp(t) from t2;
196
UPDATE t1,t2 SET 1=2 WHERE t1.n=t2.n;
199
set sql_safe_updates=0;
200
create table t1 (n int not null primary key, d int);
201
create table t2 (n int not null primary key, d int);
202
insert into t1 values(1,1), (3,3);
203
insert into t2 values(1,10),(2,20);
204
UPDATE t2 left outer join t1 on t1.n=t2.n SET t1.d=t2.d;
208
create table t1 (n int, d int);
209
create table t2 (n int, d int);
210
insert into t1 values(1,1),(1,2);
211
insert into t2 values(1,10),(2,20);
212
UPDATE t1,t2 SET t1.d=t2.d,t2.d=30 WHERE t1.n=t2.n;
216
create table t1 (n int, d int);
217
create table t2 (n int, d int);
218
insert into t1 values(1,1),(3,2);
219
insert into t2 values(1,10),(1,20);
220
UPDATE t1,t2 SET t1.d=t2.d,t2.d=30 WHERE t1.n=t2.n;
223
UPDATE t1 a ,t2 b SET a.d=b.d,b.d=30 WHERE a.n=b.n;
226
DELETE a, b FROM t1 a,t2 b where a.n=b.n;
231
CREATE TABLE t1 ( broj int NOT NULL default '0', naziv char(25) NOT NULL default 'NEPOZNAT', PRIMARY KEY (broj)) ENGINE=MyISAM;
232
INSERT INTO t1 VALUES (1,'jedan'),(2,'dva'),(3,'tri'),(4,'xxxxxxxxxx'),(5,'a'),(10,''),(11,''),(12,''),(13,'');
233
CREATE TABLE t2 ( broj int NOT NULL default '0', naziv char(25) NOT NULL default 'NEPOZNAT', PRIMARY KEY (broj)) ENGINE=MyISAM;
234
INSERT INTO t2 VALUES (1,'jedan'),(2,'dva'),(3,'tri'),(4,'xxxxxxxxxx'),(5,'a');
235
CREATE TABLE t3 ( broj int NOT NULL default '0', naziv char(25) NOT NULL default 'NEPOZNAT', PRIMARY KEY (broj)) ENGINE=MyISAM;
236
INSERT INTO t3 VALUES (1,'jedan'),(2,'dva');
237
update t1,t2 set t1.naziv="aaaa" where t1.broj=t2.broj;
238
update t1,t2,t3 set t1.naziv="bbbb", t2.naziv="aaaa" where t1.broj=t2.broj and t2.broj=t3.broj;
242
# Test multi update with different join methods
245
CREATE TABLE t1 (a int not null primary key, b int not null, key (b));
246
CREATE TABLE t2 (a int not null primary key, b int not null, key (b));
247
INSERT INTO t1 values (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9);
248
INSERT INTO t2 values (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9);
250
# Full join, without key
251
update t1,t2 set t1.a=t1.a+100;
255
update t1,t2 set t1.a=t1.a+100 where t1.a=101;
259
update t1,t2 set t1.b=t1.b+10 where t1.b=2;
263
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;
267
# test for non-updating table which is also used in sub-select
269
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);
272
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;
273
INSERT INTO t3 VALUES ('A',1,1,22,3.2,'R','R','BASE2',0.24,NULL);
274
create table t1 (A varchar(1));
275
insert into t1 values ("A") ,("B"),("C"),("D");
276
create table t2(Z varchar(15));
277
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;
278
update t2,t3 set Z =param_scenario_costs;
280
create table t1 (a int, b int);
281
create table t2 (a int, b int);
282
insert into t1 values (1,1),(2,1),(3,1);
283
insert into t2 values (1,1), (3,1);
284
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;
285
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;
289
# Test reuse of same table
292
create table t1 (a int not null auto_increment primary key, b int not null);
293
insert into t1 (b) values (1),(2),(3),(4);
294
update t1, t1 as t2 set t1.b=t2.b+1 where t1.a=t2.a;
298
# Test multi-update and multi-delete with impossible where
300
create table t1(id1 int, field char(5));
301
create table t2(id2 int, field char(5));
303
insert into t1 values (1, 'a'), (2, 'aa');
304
insert into t2 values (1, 'b'), (2, 'bb');
309
update t2 inner join t1 on t1.id1=t2.id2
310
set t2.field=t1.field
312
update t2, t1 set t2.field=t1.field
313
where t1.id1=t2.id2 and 0=1;
315
delete t1, t2 from t2 inner join t1 on t1.id1=t2.id2
317
delete t1, t2 from t2,t1
318
where t1.id1=t2.id2 and 0=1;
323
# Test for bug #1820.
326
create table t1 ( a int not null, b int not null) ;
328
insert into t1 values (1,1),(2,2),(3,3),(4,4);
334
eval insert into t1 select a+@d,b+@d from t1;
340
alter table t1 add index i1(a);
341
delete from t1 where a > 2000000;
342
create table t2 like t1;
343
insert into t2 select * from t1;
345
select 't2 rows before small delete', count(*) from t1;
346
delete t1,t2 from t1,t2 where t1.b=t2.a and t1.a < 2;
347
select 't2 rows after small delete', count(*) from t2;
348
select 't1 rows after small delete', count(*) from t1;
350
## Try deleting many rows
352
delete t1,t2 from t1,t2 where t1.b=t2.a and t1.a < 100*1000;
353
select 't2 rows after big delete', count(*) from t2;
354
select 't1 rows after big delete', count(*) from t1;
359
# Test alias (this is not correct in 4.0)
362
CREATE TABLE t1 ( a int );
363
CREATE TABLE t2 ( a int );
364
DELETE t1 FROM t1, t2 AS t3;
365
DELETE t4 FROM t1, t1 AS t4;
366
DELETE t3 FROM t1 AS t3, t1 AS t4;
368
DELETE t1 FROM t1 AS t3, t2 AS t4;
369
INSERT INTO t1 values (1),(2);
370
INSERT INTO t2 values (1),(2);
371
DELETE t1 FROM t1 AS t2, t2 AS t1 where t1.a=t2.a and t1.a=1;
374
DELETE t2 FROM t1 AS t2, t2 AS t1 where t1.a=t2.a and t1.a=2;
380
# Test update with const tables
382
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`) );
383
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`) );
384
insert into t1 values (0,'A01-Comp',1);
385
insert into t1 values (0,'B01-Comp',1);
386
insert into t2 values (0,1,'A Note',1);
387
update t1 left join t2 on p_id = c2_p_id set c2_note = 'asdf-1' where p_id = 2;
393
# multi delete wrong table check
395
create table t1 (a int, primary key (a));
396
create table t2 (a int, primary key (a));
397
create table t3 (a int, primary key (a));
399
delete t1,t3 from t1,t2 where t1.a=t2.a and t2.a=(select t3.a from t3 where t1.a=t3.a);
400
drop table t1, t2, t3;
403
# multi* unique updating table check
405
create table t1 (col1 int);
406
create table t2 (col1 int);
408
update t1,t2 set t1.col1 = (select max(col1) from t1) where t1.col1 = t2.col1;
410
delete t1 from t1,t2 where t1.col1 < (select max(col1) from t1) and t1.col1 = t2.col1;
413
# Test for BUG#5837 - delete with outer join and const tables
416
aclid bigint not null primary key,
421
refid bigint not null primary key,
422
aclid bigint, index idx_acl(aclid)
425
insert into t2 values(1,null);
426
delete t2, t1 from t2 left join t1 on (t2.aclid=t1.aclid) where t2.refid='1';
430
# Bug#19225: unchecked error leads to server crash
432
create table t1(a int);
433
create table t2(a int);
435
delete from t1,t2 using t1,t2 where t1.a=(select a from t1);
440
# Test for bug #1980.
443
create table t1 ( c char(8) not null ) engine=innodb;
446
insert into t1 values ('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9');
447
insert into t1 values ('A'),('B'),('C'),('D'),('E'),('F');
449
alter table t1 add b char(8) not null;
450
alter table t1 add a char(8) not null;
451
alter table t1 add primary key (a,b,c);
452
update t1 set a=c, b=c;
454
create table t2 like t1;
455
insert into t2 select * from t1;
457
delete t1,t2 from t2,t1 where t1.a<'B' and t2.b=t1.b;
462
create table t1 ( c char(8) not null ) engine=innodb;
465
insert into t1 values ('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9');
466
insert into t1 values ('A'),('B'),('C'),('D'),('E'),('F');
468
alter table t1 add b char(8) not null;
469
alter table t1 add a char(8) not null;
470
alter table t1 add primary key (a,b,c);
471
update t1 set a=c, b=c;
473
create table t2 like t1;
474
insert into t2 select * from t1;
476
delete t1,t2 from t2,t1 where t1.a<'B' and t2.b=t1.b;
481
# Test alter table and a concurrent multi update
482
# (This will force update to reopen tables)
485
create table t1 (a int, b int);
486
insert into t1 values (1, 2), (2, 3), (3, 4);
487
create table t2 (a int);
488
insert into t2 values (10), (20), (30);
489
create table v1 as select a as b, a/10 as a from t2;
491
connect (locker,localhost,root,,test);
495
connect (changer,localhost,root,,test);
497
send alter table t1 add column c int default 100 after a;
499
connect (updater,localhost,root,,test);
502
send update t1, v1 set t1.b=t1.a+t1.b+v1.b where t1.a=v1.a;
519
# Test multi updates and deletes using primary key and without.
521
create table t1 (i1 int, i2 int, i3 int);
522
create table t2 (id int, c1 varchar(20), c2 varchar(20));
523
insert into t1 values (1,5,10),(3,7,12),(4,5,2),(9,10,15),(2,2,2);
524
insert into t2 values (9,"abc","def"),(5,"opq","lmn"),(2,"test t","t test");
525
select * from t1 order by i1;
527
update t1,t2 set t1.i2=15, t2.c2="ppc" where t1.i1=t2.id;
528
select * from t1 order by i1;
529
select * from t2 order by id;
530
delete t1.*,t2.* from t1,t2 where t1.i2=t2.id;
531
select * from t1 order by i1;
532
select * from t2 order by id;
534
create table t1 (i1 int auto_increment not null, i2 int, i3 int, primary key (i1));
535
create table t2 (id int auto_increment not null, c1 varchar(20), c2 varchar(20), primary key(id));
536
insert into t1 values (1,5,10),(3,7,12),(4,5,2),(9,10,15),(2,2,2);
537
insert into t2 values (9,"abc","def"),(5,"opq","lmn"),(2,"test t","t test");
538
select * from t1 order by i1;
539
select * from t2 order by id;
540
update t1,t2 set t1.i2=15, t2.c2="ppc" where t1.i1=t2.id;
541
select * from t1 order by i1;
542
select * from t2 order by id;
543
delete t1.*,t2.* from t1,t2 where t1.i2=t2.id;
544
select * from t1 order by i1;
545
select * from t2 order by id;
548
#set @@session.binlog_format= @sav_binlog_format;
551
# Bug #29136 erred multi-delete on trans table does not rollback
553
# @TODO JRP: This doesn't test any rollback! Commenting out...
556
#drop table if exists t1, t2, t3;
558
#CREATE TABLE t1 (a int, PRIMARY KEY (a));
559
#CREATE TABLE t2 (a int, PRIMARY KEY (a));
560
#CREATE TABLE t3 (a int, PRIMARY KEY (a)) ENGINE=MyISAM;
561
#create trigger trg_del_t3 before delete on t3 for each row insert into t1 values (1);
563
#insert into t2 values (1),(2);
564
#insert into t3 values (1),(2);
567
# exec cases B, A - see innodb.test
569
# B. send_eof() and send_error() afterward
571
#--error ER_DUP_ENTRY
572
#delete t3.* from t2,t3 where t2.a=t3.a;
575
#select count(*) from t1 /* must be 1 */;
576
#select count(*) from t3 /* must be 1 */;
579
#drop table t1, t2, t3;
582
# Add further tests from here