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;
10
drop view if exists v1;
12
revoke all privileges on mysqltest.t1 from mysqltest_1@localhost;
14
revoke all privileges on mysqltest.* from mysqltest_1@localhost;
15
delete from mysql.user where user=_binary'mysqltest_1';
18
create table t1(id1 int not null auto_increment primary key, t char(12));
19
create table t2(id2 int not null, t char(12));
20
create table t3(id3 int not null, t char(12), index(id3));
26
eval insert into t1(t) values ('$1');
29
eval insert into t2(id2,t) values ($1,'$2');
33
eval insert into t3(id3,t) values ($1,'$2');
42
select count(*) from t1 where id1 > 95;
43
select count(*) from t2 where id2 > 95;
44
select count(*) from t3 where id3 > 95;
46
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;
47
select count(*) from t1 where t = "aaa";
48
select count(*) from t1 where id1 > 90;
49
select count(*) from t2 where t = "bbb";
50
select count(*) from t2 where id2 > 90;
51
select count(*) from t3 where t = "cc";
52
select count(*) from t3 where id3 > 90;
53
delete t1.*, t2.*, t3.* from t1,t2,t3 where t1.id1 = t2.id2 and t2.id2 = t3.id3 and t1.id1 > 95;
55
check table t1, t2, t3;
57
select count(*) from t1 where id1 > 95;
58
select count(*) from t2 where id2 > 95;
59
select count(*) from t3 where id3 > 95;
61
delete t1, t2, t3 from t1,t2,t3 where t1.id1 = t2.id2 and t2.id2 = t3.id3 and t1.id1 > 5;
62
select count(*) from t1 where id1 > 5;
63
select count(*) from t2 where id2 > 5;
64
select count(*) from t3 where id3 > 5;
66
delete from t1, t2, t3 using t1,t2,t3 where t1.id1 = t2.id2 and t2.id2 = t3.id3 and t1.id1 > 0;
68
# These queries will force a scan of the table
69
select count(*) from t1 where id1;
70
select count(*) from t2 where id2;
71
select count(*) from t3 where id3;
74
create table t1(id1 int not null primary key, t varchar(100)) pack_keys = 1;
75
create table t2(id2 int not null, t varchar(100), index(id2)) pack_keys = 1;
81
eval insert into t1 values ($1,'aaaaaaaaaaaaaaaaaaaa');
84
eval insert into t2(id2,t) values ($1,'bbbbbbbbbbbbbbbbb');
90
delete t1 from t1,t2 where t1.id1 = t2.id2 and t1.id1 > 500;
94
id int(11) NOT NULL default '0',
95
name varchar(10) default NULL,
98
INSERT INTO t1 VALUES (1,'aaa'),(2,'aaa'),(3,'aaa');
100
id int(11) NOT NULL default '0',
101
name varchar(10) default NULL,
104
INSERT INTO t2 VALUES (2,'bbb'),(3,'bbb'),(4,'bbb');
106
id int(11) NOT NULL default '0',
107
mydate datetime default NULL,
110
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
111
00:00:00'),(7,'2002-07-22 00:00:00');
112
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;
116
CREATE TABLE IF NOT EXISTS `t1` (
117
`id` int(11) NOT NULL auto_increment,
123
CREATE TABLE IF NOT EXISTS `t2` (
124
`ID` int(11) NOT NULL auto_increment,
125
`ParId` int(11) default NULL,
129
KEY `IX_ParId_t2` (`ParId`),
130
FOREIGN KEY (`ParId`) REFERENCES `t1` (`id`)
133
INSERT INTO t1(tst,tst1) VALUES("MySQL","MySQL AB"), ("MSSQL","Microsoft"), ("ORACLE","ORACLE");
135
INSERT INTO t2(ParId) VALUES(1), (2), (3);
139
UPDATE t2, t1 SET t2.tst = t1.tst, t2.tst1 = t1.tst1 WHERE t2.ParId = t1.Id;
144
create table t1 (n numeric(10));
145
create table t2 (n numeric(10));
146
insert into t2 values (1),(2),(4),(8),(16),(32);
147
select * from t2 left outer join t1 using (n);
148
delete t1,t2 from t2 left outer join t1 using (n);
149
select * from t2 left outer join t1 using (n);
156
create table t1 (n int(10) not null primary key, d int(10));
157
create table t2 (n int(10) not null primary key, d int(10));
158
insert into t1 values(1,1);
159
insert into t2 values(1,10),(2,20);
160
LOCK TABLES t1 write, t2 read;
162
DELETE t1.*, t2.* FROM t1,t2 where t1.n=t2.n;
164
UPDATE t1,t2 SET t1.d=t2.d,t2.d=30 WHERE t1.n=t2.n;
165
UPDATE t1,t2 SET t1.d=t2.d WHERE t1.n=t2.n;
167
LOCK TABLES t1 write, t2 write;
168
UPDATE t1,t2 SET t1.d=t2.d WHERE t1.n=t2.n;
170
DELETE t1.*, t2.* FROM t1,t2 where t1.n=t2.n;
177
# Test safe updates and timestamps
179
set sql_safe_updates=1;
180
create table t1 (n int(10), d int(10));
181
create table t2 (n int(10), d int(10));
182
insert into t1 values(1,1);
183
insert into t2 values(1,10),(2,20);
185
UPDATE t1,t2 SET t1.d=t2.d WHERE t1.n=t2.n;
186
set sql_safe_updates=0;
188
set timestamp=1038401397;
189
create table t1 (n int(10) not null primary key, d int(10), t timestamp);
190
create table t2 (n int(10) not null primary key, d int(10), t timestamp);
191
insert into t1 values(1,1,NULL);
192
insert into t2 values(1,10,NULL),(2,20,NULL);
193
set timestamp=1038000000;
194
UPDATE t1,t2 SET t1.d=t2.d WHERE t1.n=t2.n;
195
select n,d,unix_timestamp(t) from t1;
196
select n,d,unix_timestamp(t) from t2;
198
UPDATE t1,t2 SET 1=2 WHERE t1.n=t2.n;
201
set sql_safe_updates=0;
202
create table t1 (n int(10) not null primary key, d int(10));
203
create table t2 (n int(10) not null primary key, d int(10));
204
insert into t1 values(1,1), (3,3);
205
insert into t2 values(1,10),(2,20);
206
UPDATE t2 left outer join t1 on t1.n=t2.n SET t1.d=t2.d;
210
create table t1 (n int(10), d int(10));
211
create table t2 (n int(10), d int(10));
212
insert into t1 values(1,1),(1,2);
213
insert into t2 values(1,10),(2,20);
214
UPDATE t1,t2 SET t1.d=t2.d,t2.d=30 WHERE t1.n=t2.n;
218
create table t1 (n int(10), d int(10));
219
create table t2 (n int(10), d int(10));
220
insert into t1 values(1,1),(3,2);
221
insert into t2 values(1,10),(1,20);
222
UPDATE t1,t2 SET t1.d=t2.d,t2.d=30 WHERE t1.n=t2.n;
225
UPDATE t1 a ,t2 b SET a.d=b.d,b.d=30 WHERE a.n=b.n;
228
DELETE a, b FROM t1 a,t2 b where a.n=b.n;
233
CREATE TABLE t1 ( broj int(4) NOT NULL default '0', naziv char(25) NOT NULL default 'NEPOZNAT', PRIMARY KEY (broj)) ENGINE=MyISAM;
234
INSERT INTO t1 VALUES (1,'jedan'),(2,'dva'),(3,'tri'),(4,'xxxxxxxxxx'),(5,'a'),(10,''),(11,''),(12,''),(13,'');
235
CREATE TABLE t2 ( broj int(4) NOT NULL default '0', naziv char(25) NOT NULL default 'NEPOZNAT', PRIMARY KEY (broj)) ENGINE=MyISAM;
236
INSERT INTO t2 VALUES (1,'jedan'),(2,'dva'),(3,'tri'),(4,'xxxxxxxxxx'),(5,'a');
237
CREATE TABLE t3 ( broj int(4) NOT NULL default '0', naziv char(25) NOT NULL default 'NEPOZNAT', PRIMARY KEY (broj)) ENGINE=MyISAM;
238
INSERT INTO t3 VALUES (1,'jedan'),(2,'dva');
239
update t1,t2 set t1.naziv="aaaa" where t1.broj=t2.broj;
240
update t1,t2,t3 set t1.naziv="bbbb", t2.naziv="aaaa" where t1.broj=t2.broj and t2.broj=t3.broj;
244
# Test multi update with different join methods
247
CREATE TABLE t1 (a int not null primary key, b int not null, key (b));
248
CREATE TABLE t2 (a int not null primary key, b int not null, key (b));
249
INSERT INTO t1 values (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9);
250
INSERT INTO t2 values (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9);
252
# Full join, without key
253
update t1,t2 set t1.a=t1.a+100;
257
update t1,t2 set t1.a=t1.a+100 where t1.a=101;
261
update t1,t2 set t1.b=t1.b+10 where t1.b=2;
265
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;
269
# test for non-updating table which is also used in sub-select
271
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);
274
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(11) default NULL, PRIMARY KEY (KEY1)) ENGINE=MyISAM;
275
INSERT INTO t3 VALUES ('A',1,1,22,3.2,'R','R','BASE2',0.24,NULL);
276
create table t1 (A varchar(1));
277
insert into t1 values ("A") ,("B"),("C"),("D");
278
create table t2(Z varchar(15));
279
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;
280
update t2,t3 set Z =param_scenario_costs;
282
create table t1 (a int, b int);
283
create table t2 (a int, b int);
284
insert into t1 values (1,1),(2,1),(3,1);
285
insert into t2 values (1,1), (3,1);
286
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;
287
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;
291
# Test reuse of same table
294
create table t1 (a int not null auto_increment primary key, b int not null);
295
insert into t1 (b) values (1),(2),(3),(4);
296
update t1, t1 as t2 set t1.b=t2.b+1 where t1.a=t2.a;
300
# Test multi-update and multi-delete with impossible where
302
create table t1(id1 int(5), field char(5));
303
create table t2(id2 int(5), field char(5));
305
insert into t1 values (1, 'a'), (2, 'aa');
306
insert into t2 values (1, 'b'), (2, 'bb');
311
update t2 inner join t1 on t1.id1=t2.id2
312
set t2.field=t1.field
314
update t2, t1 set t2.field=t1.field
315
where t1.id1=t2.id2 and 0=1;
317
delete t1, t2 from t2 inner join t1 on t1.id1=t2.id2
319
delete t1, t2 from t2,t1
320
where t1.id1=t2.id2 and 0=1;
325
# Test for bug #1820.
328
create table t1 ( a int not null, b int not null) ;
330
insert into t1 values (1,1),(2,2),(3,3),(4,4);
335
eval insert into t1 select a+@d,b+@d from t1;
341
alter table t1 add index i1(a);
342
delete from t1 where a > 2000000;
343
create table t2 like t1;
344
insert into t2 select * from t1;
346
select 't2 rows before small delete', count(*) from t1;
347
delete t1,t2 from t1,t2 where t1.b=t2.a and t1.a < 2;
348
select 't2 rows after small delete', count(*) from t2;
349
select 't1 rows after small delete', count(*) from t1;
351
## Try deleting many rows
353
delete t1,t2 from t1,t2 where t1.b=t2.a and t1.a < 100*1000;
354
select 't2 rows after big delete', count(*) from t2;
355
select 't1 rows after big delete', count(*) from t1;
360
# Test alias (this is not correct in 4.0)
363
CREATE TABLE t1 ( a int );
364
CREATE TABLE t2 ( a int );
365
DELETE t1 FROM t1, t2 AS t3;
366
DELETE t4 FROM t1, t1 AS t4;
367
DELETE t3 FROM t1 AS t3, t1 AS t4;
369
DELETE t1 FROM t1 AS t3, t2 AS t4;
370
INSERT INTO t1 values (1),(2);
371
INSERT INTO t2 values (1),(2);
372
DELETE t1 FROM t1 AS t2, t2 AS t1 where t1.a=t2.a and t1.a=1;
375
DELETE t2 FROM t1 AS t2, t2 AS t1 where t1.a=t2.a and t1.a=2;
381
# Test update with const tables
383
create table `t1` (`p_id` int(10) NOT NULL auto_increment, `p_code` varchar(20) NOT NULL default '', `p_active` int(1) NOT NULL default '1', PRIMARY KEY (`p_id`) );
384
create table `t2` (`c2_id` int(10) NULL auto_increment, `c2_p_id` int(10) NOT NULL default '0', `c2_note` text NOT NULL, `c2_active` int(1) NOT NULL default '1', PRIMARY KEY (`c2_id`), KEY `c2_p_id` (`c2_p_id`) );
385
insert into t1 values (0,'A01-Comp',1);
386
insert into t1 values (0,'B01-Comp',1);
387
insert into t2 values (0,1,'A Note',1);
388
update t1 left join t2 on p_id = c2_p_id set c2_note = 'asdf-1' where p_id = 2;
394
# privilege check for multiupdate with other tables
397
connect (root,localhost,root,,test,$MASTER_MYPORT,$MASTER_MYSOCK);
400
create database mysqltest;
402
create table mysqltest.t1 (a int, b int, primary key (a));
403
create table mysqltest.t2 (a int, b int, primary key (a));
404
create table mysqltest.t3 (a int, b int, primary key (a));
405
grant select on mysqltest.* to mysqltest_1@localhost;
406
grant update on mysqltest.t1 to mysqltest_1@localhost;
407
connect (user1,localhost,mysqltest_1,,mysqltest,$MASTER_MYPORT,$MASTER_MYSOCK);
409
update t1, t2 set t1.b=1 where t1.a=t2.a;
410
update t1, t2 set t1.b=(select t3.b from t3 where t1.a=t3.a) where t1.a=t2.a;
412
revoke all privileges on mysqltest.t1 from mysqltest_1@localhost;
413
revoke all privileges on mysqltest.* from mysqltest_1@localhost;
414
delete from mysql.user where user=_binary'mysqltest_1';
415
drop database mysqltest;
418
# multi delete wrong table check
420
create table t1 (a int, primary key (a));
421
create table t2 (a int, primary key (a));
422
create table t3 (a int, primary key (a));
424
delete t1,t3 from t1,t2 where t1.a=t2.a and t2.a=(select t3.a from t3 where t1.a=t3.a);
425
drop table t1, t2, t3;
428
# multi* unique updating table check
430
create table t1 (col1 int);
431
create table t2 (col1 int);
433
update t1,t2 set t1.col1 = (select max(col1) from t1) where t1.col1 = t2.col1;
435
delete t1 from t1,t2 where t1.col1 < (select max(col1) from t1) and t1.col1 = t2.col1;
438
# Test for BUG#5837 - delete with outer join and const tables
441
aclid bigint not null primary key,
442
status int(1) not null
446
refid bigint not null primary key,
447
aclid bigint, index idx_acl(aclid)
450
insert into t2 values(1,null);
451
delete t2, t1 from t2 left join t1 on (t2.aclid=t1.aclid) where t2.refid='1';
455
# Bug#19225: unchecked error leads to server crash
457
create table t1(a int);
458
create table t2(a int);
460
delete from t1,t2 using t1,t2 where t1.a=(select a from t1);
465
# Test for bug #1980.
468
create table t1 ( c char(8) not null ) engine=innodb;
471
insert into t1 values ('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9');
472
insert into t1 values ('A'),('B'),('C'),('D'),('E'),('F');
474
alter table t1 add b char(8) not null;
475
alter table t1 add a char(8) not null;
476
alter table t1 add primary key (a,b,c);
477
update t1 set a=c, b=c;
479
create table t2 like t1;
480
insert into t2 select * from t1;
482
delete t1,t2 from t2,t1 where t1.a<'B' and t2.b=t1.b;
487
create table t1 ( c char(8) not null ) engine=innodb;
490
insert into t1 values ('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9');
491
insert into t1 values ('A'),('B'),('C'),('D'),('E'),('F');
493
alter table t1 add b char(8) not null;
494
alter table t1 add a char(8) not null;
495
alter table t1 add primary key (a,b,c);
496
update t1 set a=c, b=c;
498
create table t2 like t1;
499
insert into t2 select * from t1;
501
delete t1,t2 from t2,t1 where t1.a<'B' and t2.b=t1.b;
506
# Test alter table and a concurrent multi update
507
# (This will force update to reopen tables)
510
create table t1 (a int, b int);
511
insert into t1 values (1, 2), (2, 3), (3, 4);
512
create table t2 (a int);
513
insert into t2 values (10), (20), (30);
514
create view v1 as select a as b, a/10 as a from t2;
516
connect (locker,localhost,root,,test);
520
connect (changer,localhost,root,,test);
522
send alter table t1 add column c int default 100 after a;
524
connect (updater,localhost,root,,test);
527
send update t1, v1 set t1.b=t1.a+t1.b+v1.b where t1.a=v1.a;
544
# Test multi updates and deletes using primary key and without.
546
create table t1 (i1 int, i2 int, i3 int);
547
create table t2 (id int, c1 varchar(20), c2 varchar(20));
548
insert into t1 values (1,5,10),(3,7,12),(4,5,2),(9,10,15),(2,2,2);
549
insert into t2 values (9,"abc","def"),(5,"opq","lmn"),(2,"test t","t test");
550
select * from t1 order by i1;
552
update t1,t2 set t1.i2=15, t2.c2="ppc" where t1.i1=t2.id;
553
select * from t1 order by i1;
554
select * from t2 order by id;
555
delete t1.*,t2.* from t1,t2 where t1.i2=t2.id;
556
select * from t1 order by i1;
557
select * from t2 order by id;
559
create table t1 (i1 int auto_increment not null, i2 int, i3 int, primary key (i1));
560
create table t2 (id int auto_increment not null, c1 varchar(20), c2 varchar(20), primary key(id));
561
insert into t1 values (1,5,10),(3,7,12),(4,5,2),(9,10,15),(2,2,2);
562
insert into t2 values (9,"abc","def"),(5,"opq","lmn"),(2,"test t","t test");
563
select * from t1 order by i1;
564
select * from t2 order by id;
565
update t1,t2 set t1.i2=15, t2.c2="ppc" where t1.i1=t2.id;
566
select * from t1 order by i1;
567
select * from t2 order by id;
568
delete t1.*,t2.* from t1,t2 where t1.i2=t2.id;
569
select * from t1 order by i1;
570
select * from t2 order by id;
574
# Bug#27716 multi-update did partially and has not binlogged
577
`a` int(11) NOT NULL auto_increment,
578
`b` int(11) default NULL,
580
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;
583
`a` int(11) NOT NULL auto_increment,
584
`b` int(11) default NULL,
586
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;
588
# as the test is about to see erroed queries in binlog
589
set @sav_binlog_format= @@session.binlog_format;
590
set @@session.binlog_format= mixed;
593
# A. testing multi_update::send_error() effective update
594
insert into t1 values (1,1),(2,2);
595
insert into t2 values (1,1),(4,4);
598
UPDATE t2,t1 SET t2.a=t1.a+2;
600
select * from t2 /* must be (3,1), (4,4) */;
601
show master status /* there must be the UPDATE query event */;
603
# B. testing multi_update::send_error() ineffective update
604
# (as there is a policy described at mysql_update() still go to binlog)
607
insert into t1 values (1,2),(3,4),(4,4);
608
insert into t2 values (1,2),(3,4),(4,4);
611
UPDATE t2,t1 SET t2.a=t2.b where t2.a=t1.a;
612
show master status /* there must be the UPDATE query event */;
616
set @@session.binlog_format= @sav_binlog_format;
619
# Bug #29136 erred multi-delete on trans table does not rollback
624
drop table if exists t1, t2, t3;
626
CREATE TABLE t1 (a int, PRIMARY KEY (a));
627
CREATE TABLE t2 (a int, PRIMARY KEY (a));
628
CREATE TABLE t3 (a int, PRIMARY KEY (a)) ENGINE=MyISAM;
629
create trigger trg_del_t3 before delete on t3 for each row insert into t1 values (1);
631
insert into t2 values (1),(2);
632
insert into t3 values (1),(2);
635
# exec cases B, A - see innodb.test
637
# B. send_eof() and send_error() afterward
640
delete t3.* from t2,t3 where t2.a=t3.a;
643
select count(*) from t1 /* must be 1 */;
644
select count(*) from t3 /* must be 1 */;
647
drop table t1, t2, t3;
650
# Add further tests from here