You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
MDEV-40776 Atomic CREATE OR REPLACE silently breaks the foreign key
Give an error if one tries to drop a table referenced by a foreign keys
This is needed as innodb will keep the reference to the origina table
even when it is renamed to a temporary name as part of create or replace.
Other things:
- Changed the error message for ER_TRUNCATE_ILLEGAL_FK to say
"Cannot drop or truncate a table ..."
Copy file name to clipboardExpand all lines: mysql-test/main/create_or_replace.result
+7-2Lines changed: 7 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -933,14 +933,19 @@ drop prepare stmt;
933
933
create table t1 (x int primary key, y int) engine innodb;
934
934
create table t2 (x int references t1(x)) engine innodb;
935
935
create or replace table t1 (x int primary key);
936
+
ERROR 42000: Cannot drop or truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`x`) REFERENCES `test`.`t1` (`x`))
936
937
create table t3 (x int);
937
938
create or replace table t1 like t3;
939
+
ERROR 42000: Cannot drop or truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`x`) REFERENCES `test`.`t1` (`x`))
938
940
create or replace table t1 select * from t3;
941
+
ERROR 42000: Cannot drop or truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`x`) REFERENCES `test`.`t1` (`x`))
Copy file name to clipboardExpand all lines: mysql-test/main/trigger-trans.result
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -160,7 +160,7 @@ CREATE TRIGGER t1_ad AFTER DELETE ON t1 FOR EACH ROW SET @b = 1;
160
160
SET @a = 0;
161
161
SET @b = 0;
162
162
TRUNCATE t1;
163
-
ERROR 42000: Cannot truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`b`) REFERENCES `test`.`t1` (`a`))
163
+
ERROR 42000: Cannot drop or truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`b`) REFERENCES `test`.`t1` (`a`))
Copy file name to clipboardExpand all lines: mysql-test/suite/atomic/create_fk.result
+48Lines changed: 48 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -49,3 +49,51 @@ select * from t1;
49
49
a b c
50
50
1 1 1
51
51
drop table t1;
52
+
#
53
+
# MDEV-40776 Atomic CREATE OR REPLACE silently breaks the foreign key
54
+
#
55
+
create table t1 (pk int primary key, a int) engine=InnoDB;
56
+
create table t2 (pk int primary key, b int, foreign key(b) references t1(pk)) engine=InnoDB;
57
+
insert into t1 values (1,10),(2,20);
58
+
insert into t2 values (1,1),(2,2);
59
+
create or replace table t1 (pk int primary key, a int) engine=InnoDB;
60
+
ERROR 42000: Cannot drop or truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`b`) REFERENCES `test`.`t1` (`pk`))
create or replace table t1 (pk int primary key, a int) engine=InnoDB;
80
+
ERROR 42000: Cannot drop or truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`b`) REFERENCES `test`.`t1` (`pk`))
Copy file name to clipboardExpand all lines: mysql-test/suite/atomic/create_fk.test
+25Lines changed: 25 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -52,3 +52,28 @@ insert into t1 values (1,1,1);
52
52
create or replace table t1 (a int primary key, b int, c int, g int, constraint d foreign key e (b) references t1 (a)) engine=innodb select 1 as a ,2 as b,3 as c;
53
53
select * from t1;
54
54
drop table t1;
55
+
56
+
--echo #
57
+
--echo # MDEV-40776 Atomic CREATE OR REPLACE silently breaks the foreign key
58
+
--echo #
59
+
60
+
create table t1 (pk int primary key, a int) engine=InnoDB;
61
+
create table t2 (pk int primary key, b int, foreign key(b) references t1(pk)) engine=InnoDB;
62
+
insert into t1 values (1,10),(2,20);
63
+
insert into t2 values (1,1),(2,2);
64
+
65
+
--error ER_TRUNCATE_ILLEGAL_FK
66
+
create or replace table t1 (pk int primary key, a int) engine=InnoDB;
67
+
select * from t1;
68
+
select * from t2;
69
+
show create table t2;
70
+
71
+
set @@foreign_key_checks=0;
72
+
--error ER_TRUNCATE_ILLEGAL_FK
73
+
create or replace table t1 (pk int primary key, a int) engine=InnoDB;
Copy file name to clipboardExpand all lines: mysql-test/suite/innodb/r/foreign_key.result
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1073,7 +1073,7 @@ INSERT INTO child SET a=1;
1073
1073
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`child`, CONSTRAINT `1` FOREIGN KEY (`a`) REFERENCES `parent` (`a`))
1074
1074
connection default;
1075
1075
TRUNCATE TABLE parent;
1076
-
ERROR 42000: Cannot truncate a table referenced in a foreign key constraint (`test`.`child`, CONSTRAINT `1` FOREIGN KEY (`a`) REFERENCES `test`.`parent` (`a`))
1076
+
ERROR 42000: Cannot drop or truncate a table referenced in a foreign key constraint (`test`.`child`, CONSTRAINT `1` FOREIGN KEY (`a`) REFERENCES `test`.`parent` (`a`))
1077
1077
DROP TABLE parent;
1078
1078
ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails
1079
1079
SET innodb_lock_wait_timeout=0;
@@ -1104,7 +1104,7 @@ ALTER TABLE parent FORCE, ALGORITHM=INPLACE;
1104
1104
ALTER TABLE parent ADD COLUMN b INT, ALGORITHM=INSTANT;
1105
1105
SET foreign_key_checks=ON;
1106
1106
TRUNCATE TABLE parent;
1107
-
ERROR 42000: Cannot truncate a table referenced in a foreign key constraint (`test`.`child`, CONSTRAINT `1` FOREIGN KEY (`a`) REFERENCES `test`.`parent` (`a`))
1107
+
ERROR 42000: Cannot drop or truncate a table referenced in a foreign key constraint (`test`.`child`, CONSTRAINT `1` FOREIGN KEY (`a`) REFERENCES `test`.`parent` (`a`))
Copy file name to clipboardExpand all lines: mysql-test/suite/innodb/r/innodb-truncate.result
+4-4Lines changed: 4 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@
7
7
CREATE TABLE t1 (pk INT PRIMARY KEY) ENGINE=INNODB;
8
8
CREATE TABLE t2 (fk INT NOT NULL, FOREIGN KEY (fk) REFERENCES t1 (pk)) ENGINE=INNODB;
9
9
TRUNCATE TABLE t1;
10
-
ERROR 42000: Cannot truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`fk`) REFERENCES `test`.`t1` (`pk`))
10
+
ERROR 42000: Cannot drop or truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`fk`) REFERENCES `test`.`t1` (`pk`))
11
11
# Truncation of child should succeed.
12
12
TRUNCATE TABLE t2;
13
13
DROP TABLE t2;
@@ -32,15 +32,15 @@ TRUNCATE TABLE t2;
32
32
TRUNCATE TABLE t3;
33
33
SET @@SESSION.foreign_key_checks = 1;
34
34
TRUNCATE TABLE t1;
35
-
ERROR 42000: Cannot truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`fk`) REFERENCES `test`.`t1` (`pk`))
35
+
ERROR 42000: Cannot drop or truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`fk`) REFERENCES `test`.`t1` (`pk`))
36
36
TRUNCATE TABLE t2;
37
37
TRUNCATE TABLE t3;
38
38
LOCK TABLES t1 WRITE;
39
39
SET @@SESSION.foreign_key_checks = 0;
40
40
TRUNCATE TABLE t1;
41
41
SET @@SESSION.foreign_key_checks = 1;
42
42
TRUNCATE TABLE t1;
43
-
ERROR 42000: Cannot truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`fk`) REFERENCES `test`.`t1` (`pk`))
43
+
ERROR 42000: Cannot drop or truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`fk`) REFERENCES `test`.`t1` (`pk`))
44
44
UNLOCK TABLES;
45
45
DROP TABLE t3,t2,t1;
46
46
SET @@SESSION.foreign_key_checks = @old_foreign_key_checks;
ERROR 42000: Cannot truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`f2`) REFERENCES `test`.`t3` (`f2`))
91
+
ERROR 42000: Cannot drop or truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`f2`) REFERENCES `test`.`t3` (`f2`))
Copy file name to clipboardExpand all lines: mysql-test/suite/innodb/r/innodb.result
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -2410,15 +2410,15 @@ SELECT * FROM t1;
2410
2410
id
2411
2411
1
2412
2412
TRUNCATE t1;
2413
-
ERROR 42000: Cannot truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`id`) REFERENCES `test`.`t1` (`id`))
2413
+
ERROR 42000: Cannot drop or truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`id`) REFERENCES `test`.`t1` (`id`))
2414
2414
INSERT INTO t1 (id) VALUES (NULL);
2415
2415
SELECT * FROM t1;
2416
2416
id
2417
2417
1
2418
2418
2
2419
2419
DELETE FROM t1;
2420
2420
TRUNCATE t1;
2421
-
ERROR 42000: Cannot truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`id`) REFERENCES `test`.`t1` (`id`))
2421
+
ERROR 42000: Cannot drop or truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`id`) REFERENCES `test`.`t1` (`id`))
2422
2422
INSERT INTO t1 (id) VALUES (NULL);
2423
2423
SELECT * FROM t1;
2424
2424
id
@@ -2596,15 +2596,15 @@ ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fail
2596
2596
update t4 set a=2;
2597
2597
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`t4`, CONSTRAINT `1` FOREIGN KEY (`a`) REFERENCES `t3` (`a`))
2598
2598
truncate t1;
2599
-
ERROR 42000: Cannot truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`a`) REFERENCES `test`.`t1` (`a`))
2599
+
ERROR 42000: Cannot drop or truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`a`) REFERENCES `test`.`t1` (`a`))
2600
2600
truncate t3;
2601
-
ERROR 42000: Cannot truncate a table referenced in a foreign key constraint (`test`.`t4`, CONSTRAINT `1` FOREIGN KEY (`a`) REFERENCES `test`.`t3` (`a`))
2601
+
ERROR 42000: Cannot drop or truncate a table referenced in a foreign key constraint (`test`.`t4`, CONSTRAINT `1` FOREIGN KEY (`a`) REFERENCES `test`.`t3` (`a`))
2602
2602
truncate t2;
2603
2603
truncate t4;
2604
2604
truncate t1;
2605
-
ERROR 42000: Cannot truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`a`) REFERENCES `test`.`t1` (`a`))
2605
+
ERROR 42000: Cannot drop or truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`a`) REFERENCES `test`.`t1` (`a`))
2606
2606
truncate t3;
2607
-
ERROR 42000: Cannot truncate a table referenced in a foreign key constraint (`test`.`t4`, CONSTRAINT `1` FOREIGN KEY (`a`) REFERENCES `test`.`t3` (`a`))
2607
+
ERROR 42000: Cannot drop or truncate a table referenced in a foreign key constraint (`test`.`t4`, CONSTRAINT `1` FOREIGN KEY (`a`) REFERENCES `test`.`t3` (`a`))
2608
2608
drop table t4,t3,t2,t1;
2609
2609
create table t1 (a varchar(255) character set utf8mb3,
Copy file name to clipboardExpand all lines: mysql-test/suite/innodb/r/innodb_mysql.result
+4-4Lines changed: 4 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -2449,7 +2449,7 @@ INSERT INTO t2 VALUES (3,2);
2449
2449
SET AUTOCOMMIT = 0;
2450
2450
START TRANSACTION;
2451
2451
TRUNCATE TABLE t1;
2452
-
ERROR 42000: Cannot truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`t1_id`) REFERENCES `test`.`t1` (`id`))
2452
+
ERROR 42000: Cannot drop or truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`t1_id`) REFERENCES `test`.`t1` (`id`))
2453
2453
SELECT * FROM t1;
2454
2454
id
2455
2455
1
@@ -2461,7 +2461,7 @@ id
2461
2461
2
2462
2462
START TRANSACTION;
2463
2463
TRUNCATE TABLE t1;
2464
-
ERROR 42000: Cannot truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`t1_id`) REFERENCES `test`.`t1` (`id`))
2464
+
ERROR 42000: Cannot drop or truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`t1_id`) REFERENCES `test`.`t1` (`id`))
2465
2465
SELECT * FROM t1;
2466
2466
id
2467
2467
1
@@ -2479,7 +2479,7 @@ id
2479
2479
2
2480
2480
COMMIT;
2481
2481
TRUNCATE TABLE t1;
2482
-
ERROR 42000: Cannot truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`t1_id`) REFERENCES `test`.`t1` (`id`))
2482
+
ERROR 42000: Cannot drop or truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`t1_id`) REFERENCES `test`.`t1` (`id`))
2483
2483
SELECT * FROM t1;
2484
2484
id
2485
2485
1
@@ -2491,7 +2491,7 @@ id
2491
2491
1
2492
2492
2
2493
2493
TRUNCATE TABLE t1;
2494
-
ERROR 42000: Cannot truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`t1_id`) REFERENCES `test`.`t1` (`id`))
2494
+
ERROR 42000: Cannot drop or truncate a table referenced in a foreign key constraint (`test`.`t2`, CONSTRAINT `1` FOREIGN KEY (`t1_id`) REFERENCES `test`.`t1` (`id`))
Copy file name to clipboardExpand all lines: mysql-test/suite/innodb/r/truncate_foreign.result
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ ON UPDATE CASCADE)
5
5
ENGINE=InnoDB;
6
6
INSERT INTO child SET a=1;
7
7
TRUNCATE TABLE parent;
8
-
ERROR 42000: Cannot truncate a table referenced in a foreign key constraint (`test`.`child`, CONSTRAINT `1` FOREIGN KEY (`a`) REFERENCES `test`.`parent` (`a`))
8
+
ERROR 42000: Cannot drop or truncate a table referenced in a foreign key constraint (`test`.`child`, CONSTRAINT `1` FOREIGN KEY (`a`) REFERENCES `test`.`parent` (`a`))
0 commit comments