2
# Check for problems with delete
6
drop table if exists t1,t2,t3,t11,t12;
8
CREATE TABLE t1 (a int, b int);
9
INSERT INTO t1 VALUES (1,1);
10
INSERT LOW_PRIORITY INTO t1 VALUES (1,2);
11
INSERT INTO t1 VALUES (1,3);
12
DELETE from t1 where a=1 limit 1;
13
DELETE LOW_PRIORITY from t1 where a=1;
15
INSERT INTO t1 VALUES (1,1);
18
INSERT INTO t1 VALUES (1,2);
21
INSERT INTO t1 VALUES (1,2);
28
# Test of delete when the delete will cause a node to disappear and reappear
29
# (This assumes a block size of 1024)
34
b bigint not null default 0,
35
c bigint not null default 0,
36
d bigint not null default 0,
37
e bigint not null default 0,
38
f bigint not null default 0,
39
g bigint not null default 0,
40
h bigint not null default 0,
41
i bigint not null default 0,
42
j bigint not null default 0,
43
primary key (a,b,c,d,e,f,g,h,i,j));
44
insert into t1 (a) values (2),(4),(6),(8),(10),(12),(14),(16),(18),(20),(22),(24),(26),(23);
45
delete from t1 where a=26;
49
b bigint not null default 0,
50
c bigint not null default 0,
51
d bigint not null default 0,
52
e bigint not null default 0,
53
f bigint not null default 0,
54
g bigint not null default 0,
55
h bigint not null default 0,
56
i bigint not null default 0,
57
j bigint not null default 0,
58
primary key (a,b,c,d,e,f,g,h,i,j));
59
insert into t1 (a) values (2),(4),(6),(8),(10),(12),(14),(16),(18),(20),(22),(24),(26),(23),(27);
60
delete from t1 where a=27;
64
`i` int NOT NULL default '0',
65
`i2` int NOT NULL default '0',
68
DELETE FROM t1 USING t1 WHERE post='1';
72
# CHAR(0) bug - not actually DELETE bug, but anyway...
76
bool char(0) default NULL,
77
not_null varchar(20) binary NOT NULL default '',
78
misc integer not null,
79
PRIMARY KEY (not_null)
82
INSERT INTO t1 VALUES (NULL,'a',4), (NULL,'b',5), (NULL,'c',6), (NULL,'d',7);
84
select * from t1 where misc > 5 and bool is null;
85
delete from t1 where misc > 5 and bool is null;
86
select * from t1 where misc > 5 and bool is null;
88
select count(*) from t1;
89
delete from t1 where 1 > 2;
90
select count(*) from t1;
91
delete from t1 where 3 > 2;
92
select count(*) from t1;
96
# Bug #5733: Table handler error with self-join multi-table DELETE
99
create table t1 (a int not null auto_increment primary key, b char(32));
100
insert into t1 (b) values ('apple'), ('apple');
102
delete t1 from t1, t1 as t2 where t1.b = t2.b and t1.a > t2.a;
109
create table t11 (a int NOT NULL, b int, primary key (a));
110
create table t12 (a int NOT NULL, b int, primary key (a));
111
create table t2 (a int NOT NULL, b int, primary key (a));
112
insert into t11 values (0, 10),(1, 11),(2, 12);
113
insert into t12 values (33, 10),(0, 11),(2, 12);
114
insert into t2 values (1, 21),(2, 12),(3, 23);
118
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b <> (select b from t2 where t11.a < t2.a);
121
delete ignore t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b <> (select b from t2 where t11.a < t2.a);
124
insert into t11 values (2, 12);
125
delete from t11 where t11.b <> (select b from t2 where t11.a < t2.a);
127
delete ignore from t11 where t11.b <> (select b from t2 where t11.a < t2.a);
129
drop table t11, t12, t2;
132
# Bug #4198: deletion and KEYREAD
135
create table t1 (a int, b int, unique key (a), key (b));
136
insert into t1 values (3, 3), (7, 7);
137
delete t1 from t1 where a = 3;
143
# Bug #8392: delete with ORDER BY containing a direct reference to the table
146
CREATE TABLE t1 ( a int PRIMARY KEY );
147
DELETE FROM t1 WHERE t1.a > 0 ORDER BY t1.a;
148
INSERT INTO t1 VALUES (0),(1),(2);
149
DELETE FROM t1 WHERE t1.a > 0 ORDER BY t1.a LIMIT 1;
154
# Bug #21392: multi-table delete with alias table name fails with
155
# 1003: Incorrect table name
158
create table t1 (a int);
159
delete `4.t1` from t1 as `4.t1` where `4.t1`.a = 5;
160
delete FROM `4.t1` USING t1 as `4.t1` where `4.t1`.a = 5;
164
# Bug#17711: DELETE doesn't use index when ORDER BY, LIMIT and
165
# non-restricting WHERE is present.
167
create table t1(f1 int primary key);
168
insert into t1 values (4),(3),(1),(2);
169
delete from t1 where (@a:= f1) order by f1 limit 1;
173
# BUG#30385 "Server crash when deleting with order by and limit"
177
`seq` int NOT NULL auto_increment,
183
DELETE FROM t1 ORDER BY date ASC, time ASC LIMIT 1;
186
--echo End of 4.1 tests
189
# Test of multi-delete where we are not scanning the first table
192
CREATE TABLE t1 (a int not null,b int not null);
193
CREATE TABLE t2 (a int not null, b int not null, primary key (a,b));
194
CREATE TABLE t3 (a int not null, b int not null, primary key (a,b));
195
insert into t1 values (1,1),(2,1),(1,3);
196
insert into t2 values (1,1),(2,2),(3,3);
197
insert into t3 values (1,1),(2,1),(1,3);
198
select * from t1,t2,t3 where t1.a=t2.a AND t2.b=t3.a and t1.b=t3.b;
199
explain select * from t1,t2,t3 where t1.a=t2.a AND t2.b=t3.a and t1.b=t3.b;
200
delete t2.*,t3.* from t1,t2,t3 where t1.a=t2.a AND t2.b=t3.a and t1.b=t3.b;
201
# This should be empty
206
# Bug #8143: deleting '0000-00-00' values using IS NULL
209
create table t1(a date not null);
210
insert into t1 values (0);
211
select * from t1 where a is null;
212
delete from t1 where a is null;
213
select count(*) from t1;
217
# Bug #26186: delete order by, sometimes accept unknown column
219
CREATE TABLE t1 (a INT); INSERT INTO t1 VALUES (1);
221
--error ER_BAD_FIELD_ERROR
222
DELETE FROM t1 ORDER BY x;
224
# even columns from a table not used in query (and not even existing)
225
--error ER_BAD_FIELD_ERROR
226
DELETE FROM t1 ORDER BY t2.x;
228
# subquery (as long as the subquery from is valid or DUAL)
229
--error ER_BAD_FIELD_ERROR
230
DELETE FROM t1 ORDER BY (SELECT x);
235
# Bug #30234: Unexpected behavior using DELETE with AS and USING
246
CREATE TABLE db1.t1 (
249
INSERT INTO db1.t1 (a) SELECT * FROM t1;
252
CREATE TABLE db2.t1 (
255
INSERT INTO db2.t1 (a) SELECT * FROM t2;
257
--error ER_PARSE_ERROR
258
DELETE FROM t1 alias USING t1, t2 alias WHERE t1.a = alias.a;
259
DELETE FROM alias USING t1, t2 alias WHERE t1.a = alias.a;
260
DELETE FROM t1, alias USING t1, t2 alias WHERE t1.a = alias.a;
261
--error ER_UNKNOWN_TABLE
262
DELETE FROM t1, t2 USING t1, t2 alias WHERE t1.a = alias.a;
263
--error ER_PARSE_ERROR
264
DELETE FROM db1.t1 alias USING db1.t1, db2.t1 alias WHERE db1.t1.a = alias.a;
265
DELETE FROM alias USING db1.t1, db2.t1 alias WHERE db1.t1.a = alias.a;
266
--error ER_UNKNOWN_TABLE
267
DELETE FROM db2.alias USING db1.t1, db2.t1 alias WHERE db1.t1.a = alias.a;
268
DELETE FROM t1 USING t1 WHERE a = 1;
270
--error ER_PARSE_ERROR
271
DELETE FROM t1 alias USING t1 alias WHERE a = 2;
278
--echo End of 5.0 tests
281
# Bug#27525: table not found when using multi-table-deletes with aliases over
283
# Bug#21148: MULTI-DELETE fails to resolve a table by alias if it's from a
288
DROP DATABASE IF EXISTS db1;
289
DROP DATABASE IF EXISTS db2;
290
DROP DATABASE IF EXISTS db3;
291
DROP DATABASE IF EXISTS db4;
292
DROP TABLE IF EXISTS t1, t2;
298
CREATE TABLE db1.t1 (a INT, b INT);
299
INSERT INTO db1.t1 VALUES (1,1),(2,2),(3,3);
300
CREATE TABLE db1.t2 AS SELECT * FROM db1.t1;
301
CREATE TABLE db2.t1 AS SELECT * FROM db1.t2;
302
CREATE TABLE db2.t2 AS SELECT * FROM db2.t1;
303
CREATE TABLE t1 AS SELECT * FROM db2.t2;
304
CREATE TABLE t2 AS SELECT * FROM t1;
307
# Testing without a selected database
313
--error ER_NO_DB_ERROR
316
# Detect missing table references
318
--error ER_NO_DB_ERROR
319
DELETE a1,a2 FROM db1.t1, db2.t2;
320
--error ER_NO_DB_ERROR
321
DELETE a1,a2 FROM db1.t1, db2.t2;
322
--error ER_NO_DB_ERROR
323
DELETE a1,a2 FROM db1.t1 AS a1, db2.t2;
324
--error ER_NO_DB_ERROR
325
DELETE a1,a2 FROM db1.t1, db2.t2 AS a2;
326
--error ER_NO_DB_ERROR
327
DELETE a1,a2 FROM db3.t1 AS a1, db4.t2 AS a2;
328
--error ER_NO_DB_ERROR
329
DELETE a1,a2 FROM db3.t1 AS a1, db4.t2 AS a2;
331
--error ER_NO_DB_ERROR
332
DELETE FROM a1,a2 USING db1.t1, db2.t2;
333
--error ER_NO_DB_ERROR
334
DELETE FROM a1,a2 USING db1.t1, db2.t2;
335
--error ER_NO_DB_ERROR
336
DELETE FROM a1,a2 USING db1.t1 AS a1, db2.t2;
337
--error ER_NO_DB_ERROR
338
DELETE FROM a1,a2 USING db1.t1, db2.t2 AS a2;
339
--error ER_NO_DB_ERROR
340
DELETE FROM a1,a2 USING db3.t1 AS a1, db4.t2 AS a2;
341
--error ER_NO_DB_ERROR
342
DELETE FROM a1,a2 USING db3.t1 AS a1, db4.t2 AS a2;
344
# Ambiguous table references
346
--error ER_NO_DB_ERROR
347
DELETE a1 FROM db1.t1 AS a1, db2.t2 AS a1;
348
--error ER_NO_DB_ERROR
349
DELETE a1 FROM db1.a1, db2.t2 AS a1;
350
--error ER_NO_DB_ERROR
351
DELETE a1 FROM a1, db1.t1 AS a1;
352
--error ER_NO_DB_ERROR
353
DELETE t1 FROM db1.t1, db2.t1 AS a1;
354
--error ER_NO_DB_ERROR
355
DELETE t1 FROM db1.t1 AS a1, db2.t1 AS a2;
356
--error ER_NO_DB_ERROR
357
DELETE t1 FROM db1.t1, db2.t1;
359
# Test all again, now with a selected database
363
# Detect missing table references
365
--error ER_UNKNOWN_TABLE
366
DELETE a1,a2 FROM db1.t1, db2.t2;
367
--error ER_UNKNOWN_TABLE
368
DELETE a1,a2 FROM db1.t1, db2.t2;
369
--error ER_UNKNOWN_TABLE
370
DELETE a1,a2 FROM db1.t1 AS a1, db2.t2;
371
--error ER_UNKNOWN_TABLE
372
DELETE a1,a2 FROM db1.t1, db2.t2 AS a2;
373
--error ER_NO_SUCH_TABLE
374
DELETE a1,a2 FROM db3.t1 AS a1, db4.t2 AS a2;
375
--error ER_NO_SUCH_TABLE
376
DELETE a1,a2 FROM db3.t1 AS a1, db4.t2 AS a2;
378
--error ER_UNKNOWN_TABLE
379
DELETE FROM a1,a2 USING db1.t1, db2.t2;
380
--error ER_UNKNOWN_TABLE
381
DELETE FROM a1,a2 USING db1.t1, db2.t2;
382
--error ER_UNKNOWN_TABLE
383
DELETE FROM a1,a2 USING db1.t1 AS a1, db2.t2;
384
--error ER_UNKNOWN_TABLE
385
DELETE FROM a1,a2 USING db1.t1, db2.t2 AS a2;
386
--error ER_NO_SUCH_TABLE
387
DELETE FROM a1,a2 USING db3.t1 AS a1, db4.t2 AS a2;
388
--error ER_NO_SUCH_TABLE
389
DELETE FROM a1,a2 USING db3.t1 AS a1, db4.t2 AS a2;
391
# Ambiguous table references
393
--error ER_NONUNIQ_TABLE
394
DELETE a1 FROM db1.t1 AS a1, db2.t2 AS a1;
395
--error ER_NO_SUCH_TABLE
396
DELETE a1 FROM db1.a1, db2.t2 AS a1;
397
--error ER_NONUNIQ_TABLE
398
DELETE a1 FROM a1, db1.t1 AS a1;
399
--error ER_UNKNOWN_TABLE
400
DELETE t1 FROM db1.t1, db2.t1 AS a1;
401
--error ER_UNKNOWN_TABLE
402
DELETE t1 FROM db1.t1 AS a1, db2.t1 AS a2;
403
--error ER_UNKNOWN_TABLE
404
DELETE t1 FROM db1.t1, db2.t1;
406
# Test multiple-table cross database deletes
408
DELETE t1 FROM db1.t2 AS t1, db2.t2 AS t2 WHERE t2.a = 1 AND t1.a = t2.a;
410
DELETE a1, a2 FROM db2.t1 AS a1, t2 AS a2 WHERE a1.a = 2 AND a2.a = 2;