mirror of
https://github.com/MariaDB/server.git
synced 2025-01-19 13:32:33 +01:00
f0d4beee79
DELETE FROM ... USING ... statements with the following type of ambiguous aliasing gave unexpected results: DELETE FROM t1 AS alias USING t1, t2 AS alias WHERE t1.a = alias.a; This query would leave table t1 intact but delete rows from t2. Fixed by changing DELETE FROM ... USING syntax so that only alias references (as opposed to alias declarations) may be used in FROM. mysql-test/r/delete.result: Bug#30234: Test Result mysql-test/t/delete.test: Bug#30234: Test Case sql/sql_yacc.yy: Bug#30234: - Added parser rule table_alias_ref_list that contains a list of table aliases only. - Added parser rule table_alias_ref that sets the TL_OPTION_ALIAS in order to turn off semantic checking that applies only for table names.
262 lines
7.5 KiB
Text
262 lines
7.5 KiB
Text
drop table if exists t1,t2,t3,t11,t12;
|
|
CREATE TABLE t1 (a tinyint(3), b tinyint(5));
|
|
INSERT INTO t1 VALUES (1,1);
|
|
INSERT LOW_PRIORITY INTO t1 VALUES (1,2);
|
|
INSERT INTO t1 VALUES (1,3);
|
|
DELETE from t1 where a=1 limit 1;
|
|
DELETE LOW_PRIORITY from t1 where a=1;
|
|
INSERT INTO t1 VALUES (1,1);
|
|
DELETE from t1;
|
|
LOCK TABLE t1 write;
|
|
INSERT INTO t1 VALUES (1,2);
|
|
DELETE from t1;
|
|
UNLOCK TABLES;
|
|
INSERT INTO t1 VALUES (1,2);
|
|
SET AUTOCOMMIT=0;
|
|
DELETE from t1;
|
|
SET AUTOCOMMIT=1;
|
|
drop table t1;
|
|
create table t1 (
|
|
a bigint not null,
|
|
b bigint not null default 0,
|
|
c bigint not null default 0,
|
|
d bigint not null default 0,
|
|
e bigint not null default 0,
|
|
f bigint not null default 0,
|
|
g bigint not null default 0,
|
|
h bigint not null default 0,
|
|
i bigint not null default 0,
|
|
j bigint not null default 0,
|
|
primary key (a,b,c,d,e,f,g,h,i,j));
|
|
insert into t1 (a) values (2),(4),(6),(8),(10),(12),(14),(16),(18),(20),(22),(24),(26),(23);
|
|
delete from t1 where a=26;
|
|
drop table t1;
|
|
create table t1 (
|
|
a bigint not null,
|
|
b bigint not null default 0,
|
|
c bigint not null default 0,
|
|
d bigint not null default 0,
|
|
e bigint not null default 0,
|
|
f bigint not null default 0,
|
|
g bigint not null default 0,
|
|
h bigint not null default 0,
|
|
i bigint not null default 0,
|
|
j bigint not null default 0,
|
|
primary key (a,b,c,d,e,f,g,h,i,j));
|
|
insert into t1 (a) values (2),(4),(6),(8),(10),(12),(14),(16),(18),(20),(22),(24),(26),(23),(27);
|
|
delete from t1 where a=27;
|
|
drop table t1;
|
|
CREATE TABLE `t1` (
|
|
`i` int(10) NOT NULL default '0',
|
|
`i2` int(10) NOT NULL default '0',
|
|
PRIMARY KEY (`i`)
|
|
);
|
|
DELETE FROM t1 USING t1 WHERE post='1';
|
|
ERROR 42S22: Unknown column 'post' in 'where clause'
|
|
drop table t1;
|
|
CREATE TABLE t1 (
|
|
bool char(0) default NULL,
|
|
not_null varchar(20) binary NOT NULL default '',
|
|
misc integer not null,
|
|
PRIMARY KEY (not_null)
|
|
) ENGINE=MyISAM;
|
|
INSERT INTO t1 VALUES (NULL,'a',4), (NULL,'b',5), (NULL,'c',6), (NULL,'d',7);
|
|
select * from t1 where misc > 5 and bool is null;
|
|
bool not_null misc
|
|
NULL c 6
|
|
NULL d 7
|
|
delete from t1 where misc > 5 and bool is null;
|
|
select * from t1 where misc > 5 and bool is null;
|
|
bool not_null misc
|
|
select count(*) from t1;
|
|
count(*)
|
|
2
|
|
delete from t1 where 1 > 2;
|
|
select count(*) from t1;
|
|
count(*)
|
|
2
|
|
delete from t1 where 3 > 2;
|
|
select count(*) from t1;
|
|
count(*)
|
|
0
|
|
drop table t1;
|
|
create table t1 (a int not null auto_increment primary key, b char(32));
|
|
insert into t1 (b) values ('apple'), ('apple');
|
|
select * from t1;
|
|
a b
|
|
1 apple
|
|
2 apple
|
|
delete t1 from t1, t1 as t2 where t1.b = t2.b and t1.a > t2.a;
|
|
select * from t1;
|
|
a b
|
|
1 apple
|
|
drop table t1;
|
|
create table t11 (a int NOT NULL, b int, primary key (a));
|
|
create table t12 (a int NOT NULL, b int, primary key (a));
|
|
create table t2 (a int NOT NULL, b int, primary key (a));
|
|
insert into t11 values (0, 10),(1, 11),(2, 12);
|
|
insert into t12 values (33, 10),(0, 11),(2, 12);
|
|
insert into t2 values (1, 21),(2, 12),(3, 23);
|
|
select * from t11;
|
|
a b
|
|
0 10
|
|
1 11
|
|
2 12
|
|
select * from t12;
|
|
a b
|
|
33 10
|
|
0 11
|
|
2 12
|
|
select * from t2;
|
|
a b
|
|
1 21
|
|
2 12
|
|
3 23
|
|
delete t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b <> (select b from t2 where t11.a < t2.a);
|
|
ERROR 21000: Subquery returns more than 1 row
|
|
select * from t11;
|
|
a b
|
|
0 10
|
|
1 11
|
|
2 12
|
|
select * from t12;
|
|
a b
|
|
33 10
|
|
0 11
|
|
2 12
|
|
delete ignore t11.*, t12.* from t11,t12 where t11.a = t12.a and t11.b <> (select b from t2 where t11.a < t2.a);
|
|
Warnings:
|
|
Error 1242 Subquery returns more than 1 row
|
|
Error 1242 Subquery returns more than 1 row
|
|
select * from t11;
|
|
a b
|
|
0 10
|
|
1 11
|
|
select * from t12;
|
|
a b
|
|
33 10
|
|
0 11
|
|
insert into t11 values (2, 12);
|
|
delete from t11 where t11.b <> (select b from t2 where t11.a < t2.a);
|
|
ERROR 21000: Subquery returns more than 1 row
|
|
select * from t11;
|
|
a b
|
|
0 10
|
|
1 11
|
|
2 12
|
|
delete ignore from t11 where t11.b <> (select b from t2 where t11.a < t2.a);
|
|
Warnings:
|
|
Error 1242 Subquery returns more than 1 row
|
|
Error 1242 Subquery returns more than 1 row
|
|
select * from t11;
|
|
a b
|
|
0 10
|
|
1 11
|
|
drop table t11, t12, t2;
|
|
create table t1 (a int, b int, unique key (a), key (b));
|
|
insert into t1 values (3, 3), (7, 7);
|
|
delete t1 from t1 where a = 3;
|
|
check table t1;
|
|
Table Op Msg_type Msg_text
|
|
test.t1 check status OK
|
|
select * from t1;
|
|
a b
|
|
7 7
|
|
drop table t1;
|
|
CREATE TABLE t1 ( a int PRIMARY KEY );
|
|
DELETE FROM t1 WHERE t1.a > 0 ORDER BY t1.a;
|
|
INSERT INTO t1 VALUES (0),(1),(2);
|
|
DELETE FROM t1 WHERE t1.a > 0 ORDER BY t1.a LIMIT 1;
|
|
SELECT * FROM t1;
|
|
a
|
|
0
|
|
2
|
|
DROP TABLE t1;
|
|
create table t1 (a int);
|
|
delete `4.t1` from t1 as `4.t1` where `4.t1`.a = 5;
|
|
delete FROM `4.t1` USING t1 as `4.t1` where `4.t1`.a = 5;
|
|
drop table t1;
|
|
create table t1(f1 int primary key);
|
|
insert into t1 values (4),(3),(1),(2);
|
|
delete from t1 where (@a:= f1) order by f1 limit 1;
|
|
select @a;
|
|
@a
|
|
1
|
|
drop table t1;
|
|
End of 4.1 tests
|
|
CREATE TABLE t1 (a int not null,b int not null);
|
|
CREATE TABLE t2 (a int not null, b int not null, primary key (a,b));
|
|
CREATE TABLE t3 (a int not null, b int not null, primary key (a,b));
|
|
insert into t1 values (1,1),(2,1),(1,3);
|
|
insert into t2 values (1,1),(2,2),(3,3);
|
|
insert into t3 values (1,1),(2,1),(1,3);
|
|
select * from t1,t2,t3 where t1.a=t2.a AND t2.b=t3.a and t1.b=t3.b;
|
|
a b a b a b
|
|
1 1 1 1 1 1
|
|
2 1 2 2 2 1
|
|
1 3 1 1 1 3
|
|
explain select * from t1,t2,t3 where t1.a=t2.a AND t2.b=t3.a and t1.b=t3.b;
|
|
id select_type table type possible_keys key key_len ref rows Extra
|
|
1 SIMPLE t1 ALL NULL NULL NULL NULL 3
|
|
1 SIMPLE t2 ref PRIMARY PRIMARY 4 test.t1.a 1 Using index
|
|
1 SIMPLE t3 eq_ref PRIMARY PRIMARY 8 test.t2.b,test.t1.b 1 Using index
|
|
delete t2.*,t3.* from t1,t2,t3 where t1.a=t2.a AND t2.b=t3.a and t1.b=t3.b;
|
|
select * from t3;
|
|
a b
|
|
drop table t1,t2,t3;
|
|
create table t1(a date not null);
|
|
insert into t1 values (0);
|
|
select * from t1 where a is null;
|
|
a
|
|
0000-00-00
|
|
delete from t1 where a is null;
|
|
select count(*) from t1;
|
|
count(*)
|
|
0
|
|
drop table t1;
|
|
CREATE TABLE t1 (a INT);
|
|
INSERT INTO t1 VALUES (1);
|
|
DELETE FROM t1 ORDER BY x;
|
|
ERROR 42S22: Unknown column 'x' in 'order clause'
|
|
DELETE FROM t1 ORDER BY t2.x;
|
|
ERROR 42S22: Unknown column 't2.x' in 'order clause'
|
|
DELETE FROM t1 ORDER BY (SELECT x);
|
|
ERROR 42S22: Unknown column 'x' in 'field list'
|
|
DROP TABLE t1;
|
|
CREATE TABLE t1 (
|
|
a INT
|
|
);
|
|
CREATE TABLE t2 (
|
|
a INT
|
|
);
|
|
CREATE DATABASE db1;
|
|
CREATE TABLE db1.t1 (
|
|
a INT
|
|
);
|
|
INSERT INTO db1.t1 (a) SELECT * FROM t1;
|
|
CREATE DATABASE db2;
|
|
CREATE TABLE db2.t1 (
|
|
a INT
|
|
);
|
|
INSERT INTO db2.t1 (a) SELECT * FROM t2;
|
|
DELETE FROM t1 alias USING t1, t2 alias WHERE t1.a = alias.a;
|
|
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'alias USING t1, t2 alias WHERE t1.a = alias.a' at line 1
|
|
DELETE FROM alias USING t1, t2 alias WHERE t1.a = alias.a;
|
|
DELETE FROM t1, alias USING t1, t2 alias WHERE t1.a = alias.a;
|
|
DELETE FROM t1, t2 USING t1, t2 alias WHERE t1.a = alias.a;
|
|
ERROR 42S02: Unknown table 't2' in MULTI DELETE
|
|
DELETE FROM db1.t1 alias USING db1.t1, db2.t1 alias WHERE db1.t1.a = alias.a;
|
|
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'alias USING db1.t1, db2.t1 alias WHERE db1.t1.a = alias.a' at line 1
|
|
DELETE FROM alias USING db1.t1, db2.t1 alias WHERE db1.t1.a = alias.a;
|
|
ERROR 42S02: Unknown table 'alias' in MULTI DELETE
|
|
DELETE FROM db2.alias USING db1.t1, db2.t1 alias WHERE db1.t1.a = alias.a;
|
|
DELETE FROM t1 USING t1 WHERE a = 1;
|
|
SELECT * FROM t1;
|
|
a
|
|
DELETE FROM t1 alias USING t1 alias WHERE a = 2;
|
|
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'alias USING t1 alias WHERE a = 2' at line 1
|
|
SELECT * FROM t1;
|
|
a
|
|
DROP TABLE t1, t2;
|
|
DROP DATABASE db1;
|
|
DROP DATABASE db2;
|