mark some file to been opensource for ce-farm

This commit is contained in:
niyuhang
2023-11-15 11:44:43 +00:00
committed by ob-robot
parent 4900683cff
commit c8ace58297
685 changed files with 1080566 additions and 111051 deletions

View File

@ -0,0 +1,763 @@
set foreign_key_checks=on;
drop table if exists t6, t5, t4, t3, t2, t1;
create table t1(pk int, a int, b int, primary key (pk), unique key uk_a_b (b, a));
create table t2(pk int, a int, b int, primary key (pk), unique key uk_a_b (a, b), constraint fake_name_2 foreign key (a, b) references t1 (b, a) on update CASCADE on delete CASCADE);
create table t3(pk int, a int, b int, primary key (pk), unique key uk_a_b (b, a), constraint fake_name_3 foreign key (a, b) references t2 (a, b) on update CASCADE on delete CASCADE);
create table t4(pk int, a int, b int, primary key (pk), unique key uk_a_b (a, b), constraint fake_name_4 foreign key (a, b) references t3 (b, a) on update CASCADE on delete CASCADE);
insert into t1 values (10, 11, 12), (20, 21, 22), (30, 31, 32), (40, 41, 42), (50, 51, 52), (60, 61, 62), (70, 71, 72), (80, 81, 82);
insert into t2 values (10, 12, 11), (20, 22, 21), (30, 32, 31), (40, 42, 41), (50, 52, 51), (60, 62, 61), (70, 72, 71);
insert into t3 values (10, 12, 11), (20, 22, 21), (30, 32, 31), (40, 42, 41), (50, 52, 51), (60, 62, 61);
insert into t4 values (10, 11, 12), (20, 21, 22), (30, 31, 32), (40, 41, 42), (50, 51, 52);
insert into t2 values (80, 82, 81), (90, 92, 91);
insert into t3 values (70, 72, 71), (80, 82, 81);
insert into t4 values (60, 61, 62), (70, 71, 72);
select * from t1 order by pk;
pk a b
10 11 12
20 21 22
30 31 32
40 41 42
50 51 52
60 61 62
70 71 72
80 81 82
select * from t2 order by pk;
pk a b
10 12 11
20 22 21
30 32 31
40 42 41
50 52 51
60 62 61
70 72 71
select * from t3 order by pk;
pk a b
10 12 11
20 22 21
30 32 31
40 42 41
50 52 51
60 62 61
select * from t4 order by pk;
pk a b
10 11 12
20 21 22
30 31 32
40 41 42
50 51 52
update t1 set a = 91, b = 92 where pk = 10;
update t2 set a = 82, b = 81 where pk = 20;
update t3 set a = 72, b = 71 where pk = 30;
update t4 set a = 61, b = 62 where pk = 40;
update t2 set a = a * 10 where pk = 70;
update t3 set a = a * 10 where pk = 60;
update t4 set a = a * 10 where pk = 50;
create table t5(pk int, a int, b int, primary key (pk), unique key uk_a_b (b, a), constraint fake_name_5 foreign key (a, b) references t4 (a, b) on update RESTRICT);
insert into t5 values (10, 91, 92), (20, 81, 82), (30, 71, 72), (40, 61, 62);
update t1 set a = 11, b = 12 where pk = 10;
update t2 set a = 22, b = 21 where pk = 20;
update t3 set a = 32, b = 31 where pk = 30;
update t4 set a = 41, b = 42 where pk = 40;
update t1 set pk = pk * 10 where pk = 10;
update t2 set pk = pk * 10 where pk = 20;
update t3 set pk = pk * 10 where pk = 30;
update t4 set pk = pk * 10 where pk = 40;
select * from t1 order by pk;
pk a b
20 21 22
30 31 32
40 41 42
50 51 52
60 61 62
70 71 72
80 81 82
100 91 92
select * from t2 order by pk;
pk a b
10 92 91
30 32 31
40 42 41
50 52 51
60 62 61
70 72 71
200 82 81
select * from t3 order by pk;
pk a b
10 92 91
20 82 81
40 42 41
50 52 51
60 62 61
300 72 71
select * from t4 order by pk;
pk a b
10 91 92
20 81 82
30 71 72
50 51 52
400 61 62
select * from t5 order by pk;
pk a b
10 91 92
20 81 82
30 71 72
40 61 62
drop table if exists t5;
delete from t1 where pk = 10;
delete from t2 where pk = 20;
delete from t3 where pk = 30;
delete from t4 where pk = 40;
select * from t1 order by pk;
pk a b
20 21 22
30 31 32
40 41 42
50 51 52
60 61 62
70 71 72
80 81 82
100 91 92
select * from t2 order by pk;
pk a b
10 92 91
30 32 31
40 42 41
50 52 51
60 62 61
70 72 71
200 82 81
select * from t3 order by pk;
pk a b
10 92 91
20 82 81
40 42 41
50 52 51
60 62 61
300 72 71
select * from t4 order by pk;
pk a b
10 91 92
20 81 82
30 71 72
50 51 52
400 61 62
create table t5(pk int, a int, b int, primary key (pk), unique key uk_a_b (b, a), constraint fake_name_5 foreign key (a, b) references t4 (a, b) on delete NO ACTION);
insert into t5 values (50, 51, 52);
delete from t1 where pk = 50;
delete from t2 where pk = 50;
delete from t3 where pk = 50;
delete from t4 where pk = 50;
select * from t1 order by pk;
pk a b
20 21 22
30 31 32
40 41 42
50 51 52
60 61 62
70 71 72
80 81 82
100 91 92
select * from t2 order by pk;
pk a b
10 92 91
30 32 31
40 42 41
50 52 51
60 62 61
70 72 71
200 82 81
select * from t3 order by pk;
pk a b
10 92 91
20 82 81
40 42 41
50 52 51
60 62 61
300 72 71
select * from t4 order by pk;
pk a b
10 91 92
20 81 82
30 71 72
50 51 52
400 61 62
select * from t5 order by pk;
pk a b
50 51 52
drop table if exists t5;
delete from t1;
select * from t1 order by pk;
pk a b
select * from t2 order by pk;
pk a b
select * from t3 order by pk;
pk a b
select * from t4 order by pk;
pk a b
replace into t1 values (10, 11, 12), (20, 21, 22), (30, 31, 32), (40, 41, 42), (50, 51, 52), (60, 61, 62), (70, 71, 72), (80, 81, 82);
replace into t2 values (10, 12, 11), (20, 22, 21), (30, 32, 31), (40, 42, 41), (50, 52, 51), (60, 62, 61), (70, 72, 71);
replace into t3 values (10, 12, 11), (20, 22, 21), (30, 32, 31), (40, 42, 41), (50, 52, 51), (60, 62, 61);
replace into t4 values (10, 11, 12), (20, 21, 22), (30, 31, 32), (40, 41, 42), (50, 51, 52);
replace into t2 values (80, 82, 81), (90, 92, 91);
replace into t3 values (70, 72, 71), (80, 82, 81);
replace into t4 values (60, 61, 62), (70, 71, 72);
select * from t1 order by pk;
pk a b
10 11 12
20 21 22
30 31 32
40 41 42
50 51 52
60 61 62
70 71 72
80 81 82
select * from t2 order by pk;
pk a b
10 12 11
20 22 21
30 32 31
40 42 41
50 52 51
60 62 61
70 72 71
select * from t3 order by pk;
pk a b
10 12 11
20 22 21
30 32 31
40 42 41
50 52 51
60 62 61
select * from t4 order by pk;
pk a b
10 11 12
20 21 22
30 31 32
40 41 42
50 51 52
replace into t1 values (10, 91, 92);
replace into t2 values (20, 82, 81);
replace into t3 values (30, 72, 71);
replace into t4 values (40, 61, 62);
select * from t1 order by pk;
pk a b
10 91 92
20 21 22
30 31 32
40 41 42
50 51 52
60 61 62
70 71 72
80 81 82
select * from t2 order by pk;
pk a b
20 82 81
30 32 31
40 42 41
50 52 51
60 62 61
70 72 71
select * from t3 order by pk;
pk a b
30 72 71
40 42 41
50 52 51
60 62 61
select * from t4 order by pk;
pk a b
40 61 62
50 51 52
create table t5(pk int, a int, b int, primary key (pk), unique key uk_a_b (b, a), constraint fake_name_5 foreign key (a, b) references t4 (a, b));
replace into t5 values (50, 51, 52);
replace into t1 values (50, 11, 12);
replace into t2 values (50, 22, 21);
replace into t3 values (50, 32, 31);
replace into t4 values (50, 41, 42);
select * from t1 order by pk;
pk a b
10 91 92
20 21 22
30 31 32
40 41 42
50 51 52
60 61 62
70 71 72
80 81 82
select * from t2 order by pk;
pk a b
20 82 81
30 32 31
40 42 41
50 52 51
60 62 61
70 72 71
select * from t3 order by pk;
pk a b
30 72 71
40 42 41
50 52 51
60 62 61
select * from t4 order by pk;
pk a b
40 61 62
50 51 52
select * from t5 order by pk;
pk a b
50 51 52
drop table if exists t5;
delete from t1;
select * from t1 order by pk;
pk a b
select * from t2 order by pk;
pk a b
select * from t3 order by pk;
pk a b
select * from t4 order by pk;
pk a b
insert into t1 values (10, 11, 12), (20, 21, 22), (30, 31, 32), (40, 41, 42), (50, 51, 52), (60, 61, 62), (70, 71, 72), (80, 81, 82);
insert into t2 values (10, 12, 11), (20, 22, 21), (30, 32, 31), (40, 42, 41), (50, 52, 51), (60, 62, 61), (70, 72, 71);
insert into t3 values (10, 12, 11), (20, 22, 21), (30, 32, 31), (40, 42, 41), (50, 52, 51), (60, 62, 61);
insert into t4 values (10, 11, 12), (20, 21, 22), (30, 31, 32), (40, 41, 42), (50, 51, 52);
select * from t1 order by pk;
pk a b
10 11 12
20 21 22
30 31 32
40 41 42
50 51 52
60 61 62
70 71 72
80 81 82
select * from t2 order by pk;
pk a b
10 12 11
20 22 21
30 32 31
40 42 41
50 52 51
60 62 61
70 72 71
select * from t3 order by pk;
pk a b
10 12 11
20 22 21
30 32 31
40 42 41
50 52 51
60 62 61
select * from t4 order by pk;
pk a b
10 11 12
20 21 22
30 31 32
40 41 42
50 51 52
insert into t1 values (10, 101, 102) on duplicate key update a = 91, b = 92;
insert into t2 values (20, 92, 91) on duplicate key update a = 82, b = 81;
insert into t3 values (30, 82, 81) on duplicate key update a = 72, b = 71;
insert into t4 values (40, 71, 72) on duplicate key update a = 61, b = 62;
select * from t1 order by pk;
pk a b
10 91 92
20 21 22
30 31 32
40 41 42
50 51 52
60 61 62
70 71 72
80 81 82
select * from t2 order by pk;
pk a b
10 92 91
20 82 81
30 32 31
40 42 41
50 52 51
60 62 61
70 72 71
select * from t3 order by pk;
pk a b
10 92 91
20 82 81
30 72 71
40 42 41
50 52 51
60 62 61
select * from t4 order by pk;
pk a b
10 91 92
20 81 82
30 71 72
40 61 62
50 51 52
insert into t2 values (20, 0, 0) on duplicate key update a = a * 10;
insert into t3 values (30, 0, 0) on duplicate key update a = a * 10;
insert into t4 values (40, 0, 0) on duplicate key update a = a * 10;
create table t5(pk int, a int, b int, primary key (pk), unique key uk_a_b (b, a), constraint fake_name_5 foreign key (a, b) references t4 (a, b));
insert into t5 values (10, 91, 92), (20, 81, 82), (30, 71, 72), (40, 61, 62);
insert into t1 values (10, 101, 102) on duplicate key update a = 101, b = 102;
insert into t1 values (100, 101, 102);
insert into t2 values (20, 102, 101) on duplicate key update a = 102, b = 101;
insert into t2 values (100, 102, 101);
insert into t3 values (30, 102, 101) on duplicate key update a = 102, b = 101;
insert into t3 values (100, 102, 101);
insert into t4 values (40, 101, 102) on duplicate key update a = 101, b = 102;
insert into t4 values (100, 101, 102);
select * from t1 order by pk;
pk a b
10 91 92
20 21 22
30 31 32
40 41 42
50 51 52
60 61 62
70 71 72
80 81 82
100 101 102
select * from t2 order by pk;
pk a b
10 92 91
20 82 81
30 32 31
40 42 41
50 52 51
60 62 61
70 72 71
100 102 101
select * from t3 order by pk;
pk a b
10 92 91
20 82 81
30 72 71
40 42 41
50 52 51
60 62 61
100 102 101
select * from t4 order by pk;
pk a b
10 91 92
20 81 82
30 71 72
40 61 62
50 51 52
100 101 102
select * from t5 order by pk;
pk a b
10 91 92
20 81 82
30 71 72
40 61 62
insert into t1 values (0, 101, 102) on duplicate key update a = 101, b = 102;
insert into t2 values (0, 102, 101) on duplicate key update a = 102, b = 101;
insert into t3 values (0, 102, 101) on duplicate key update a = 102, b = 101;
insert into t4 values (0, 101, 102) on duplicate key update a = 101, b = 102;
select * from t1 order by pk;
pk a b
10 91 92
20 21 22
30 31 32
40 41 42
50 51 52
60 61 62
70 71 72
80 81 82
100 101 102
select * from t2 order by pk;
pk a b
10 92 91
20 82 81
30 32 31
40 42 41
50 52 51
60 62 61
70 72 71
100 102 101
select * from t3 order by pk;
pk a b
10 92 91
20 82 81
30 72 71
40 42 41
50 52 51
60 62 61
100 102 101
select * from t4 order by pk;
pk a b
10 91 92
20 81 82
30 71 72
40 61 62
50 51 52
100 101 102
select * from t5 order by pk;
pk a b
10 91 92
20 81 82
30 71 72
40 61 62
insert into t1 values (0, 21, 22) on duplicate key update pk = 1000;
insert into t2 values (0, 32, 31) on duplicate key update pk = 1010;
insert into t3 values (0, 42, 41) on duplicate key update pk = 1020;
insert into t4 values (0, 51, 52) on duplicate key update pk = 1030;
select * from t1 order by pk;
pk a b
10 91 92
30 31 32
40 41 42
50 51 52
60 61 62
70 71 72
80 81 82
100 101 102
1000 21 22
select * from t2 order by pk;
pk a b
10 92 91
20 82 81
40 42 41
50 52 51
60 62 61
70 72 71
100 102 101
1010 32 31
select * from t3 order by pk;
pk a b
10 92 91
20 82 81
30 72 71
50 52 51
60 62 61
100 102 101
1020 42 41
select * from t4 order by pk;
pk a b
10 91 92
20 81 82
30 71 72
40 61 62
100 101 102
1030 51 52
select * from t5 order by pk;
pk a b
10 91 92
20 81 82
30 71 72
40 61 62
drop table if exists t5, t4, t3, t2, t1;
create table t1(pk int, a varchar(16), b datetime(6), primary key (pk), unique key uk_a_b (b, a));
create table t2(pk int, a datetime(6), b varchar(16), primary key (pk), unique key uk_a_b (a, b), foreign key (a, b) references t1 (b, a) on update CASCADE on delete CASCADE);
insert into t1 values (10, 'abc', '2018-03-31 12:00:00'),
(20, 'DEF', '2018-03-31 13:00:00'),
(30, 'ghi', '2018-03-31 14:00:00'),
(40, 'JKL', '2018-03-31 15:00:00'),
(50, 'mno', '2018-03-31 16:00:00'),
(60, 'PQR', '2018-03-31 17:00:00');
insert into t2 values (10, '2018-03-31 12:00:00', 'ABC'),
(20, '2018-03-31 13:00:00', 'def'),
(30, '2018-03-31 14:00:00', 'GHI'),
(40, '2018-03-31 15:00:00', 'jkl'),
(50, '2018-03-31 00:00:00', NULL),
(60, NULL, NULL);
delete from t1 where pk = 10;
update t1 set a = 'stu', b = '2018-03-31 19:00:00' where pk = 20;
update t1 set a = NULL where pk = 30;
select * from t1 order by pk;
pk a b
20 stu 2018-03-31 19:00:00.000000
30 NULL 2018-03-31 14:00:00.000000
40 JKL 2018-03-31 15:00:00.000000
50 mno 2018-03-31 16:00:00.000000
60 PQR 2018-03-31 17:00:00.000000
select * from t2 order by pk;
pk a b
20 2018-03-31 19:00:00.000000 stu
30 2018-03-31 14:00:00.000000 NULL
40 2018-03-31 15:00:00.000000 jkl
50 2018-03-31 00:00:00.000000 NULL
60 NULL NULL
delete from t1 where pk = 30;
update t2 set a = NULL, b = 'vwx' where pk = 20;
select * from t1 order by pk;
pk a b
20 stu 2018-03-31 19:00:00.000000
40 JKL 2018-03-31 15:00:00.000000
50 mno 2018-03-31 16:00:00.000000
60 PQR 2018-03-31 17:00:00.000000
select * from t2 order by pk;
pk a b
20 NULL vwx
30 2018-03-31 14:00:00.000000 NULL
40 2018-03-31 15:00:00.000000 jkl
50 2018-03-31 00:00:00.000000 NULL
60 NULL NULL
create table t3(pk int, a datetime(6), b varchar(16), primary key (pk), unique key uk_a_b (a, b), foreign key (a, b) references t1 (b, a));
insert into t3 values (40, '2018-03-31 15:00:00', 'jkl');
update t2 set b = 'hello' where pk = 20;
select * from t2 order by pk;
pk a b
20 NULL hello
30 2018-03-31 14:00:00.000000 NULL
40 2018-03-31 15:00:00.000000 jkl
50 2018-03-31 00:00:00.000000 NULL
60 NULL NULL
select * from t3 order by pk;
pk a b
40 2018-03-31 15:00:00.000000 jkl
delete from t2 where pk = 20;
select * from t2 order by pk;
pk a b
30 2018-03-31 14:00:00.000000 NULL
40 2018-03-31 15:00:00.000000 jkl
50 2018-03-31 00:00:00.000000 NULL
60 NULL NULL
select * from t3 order by pk;
pk a b
40 2018-03-31 15:00:00.000000 jkl
insert into t3 values (10, '2018-03-31 12:00:00', 'ABC');
update t1 set a = 'hello' where pk = 40;
delete from t1 where pk = 40;
drop table if exists t3, t2, t1;
create table t1(pk int, a int, b int, primary key (pk), unique key uk_a_b (b, a));
create table t2(pk int, a int, b int, primary key (pk), unique key uk_a_b (a, b), constraint fake_name_2 foreign key (a, b) references t1 (b, a));
set foreign_key_checks=on;
insert into t1 values (10, 11, 12), (20, 21, 22), (30, 31, 32), (40, 41, 42), (50, 51, 52), (60, 61, 62), (70, 71, 72), (80, 81, 82);
insert into t2 values (10, 12, 11), (20, 22, 21), (30, 32, 31), (40, 42, 41), (50, 52, 51), (60, 62, 61), (70, 72, 71);
insert into t2 values (80, 82, 81), (90, 92, 91);
update t1 set a = 101, b = 102 where pk = 10;
delete from t1 where pk = 20;
select * from t1 order by pk;
pk a b
10 11 12
20 21 22
30 31 32
40 41 42
50 51 52
60 61 62
70 71 72
80 81 82
select * from t2 order by pk;
pk a b
10 12 11
20 22 21
30 32 31
40 42 41
50 52 51
60 62 61
70 72 71
set foreign_key_checks=off;
insert into t2 values (80, 82, 81), (90, 92, 91);
update t1 set a = 101, b = 102 where pk = 10;
delete from t1 where pk = 20;
select * from t1 order by pk;
pk a b
10 101 102
30 31 32
40 41 42
50 51 52
60 61 62
70 71 72
80 81 82
select * from t2 order by pk;
pk a b
10 12 11
20 22 21
30 32 31
40 42 41
50 52 51
60 62 61
70 72 71
80 82 81
90 92 91
set foreign_key_checks=on;
drop table if exists t3, t2, t1;
create table t1(pk int, a int, b int, c int, d int, primary key (a, b), constraint fake_name_1 foreign key (c, d) references t1 (a, b) on update CASCADE on delete CASCADE);
insert into t1 values (10, 11, 12, NULL, NULL), (20, 13, 14, NULL, NULL);
insert into t1 values (30, 21, 22, 11, 12), (40, 23, 24, 11, 12);
insert into t1 values (50, 31, 32, 21, 22), (60, 33, 34, 21, 22), (70, 35, 36, 23, 24), (80, 37, 38, 23, 24);
insert into t1 values (90, 41, 42, 31, 32), (100, 43, 44, 33, 34), (110, 45, 46, 35, 36), (120, 47, 48, 37, 38);
insert into t1 values (130, 51, 52, 41, 42), (140, 53, 54, 49, 50);
update t1 set c = 15, d = 16 where pk = 30;
select * from t1 order by pk;
pk a b c d
10 11 12 NULL NULL
20 13 14 NULL NULL
30 21 22 11 12
40 23 24 11 12
50 31 32 21 22
60 33 34 21 22
70 35 36 23 24
80 37 38 23 24
90 41 42 31 32
100 43 44 33 34
110 45 46 35 36
120 47 48 37 38
delete from t1 where pk = 50;
select * from t1 order by pk;
pk a b c d
10 11 12 NULL NULL
20 13 14 NULL NULL
30 21 22 11 12
40 23 24 11 12
60 33 34 21 22
70 35 36 23 24
80 37 38 23 24
100 43 44 33 34
110 45 46 35 36
120 47 48 37 38
drop table if exists t1;
drop table if exists t2, t1;
create table t1(pk int, a int, b int, primary key (pk), unique key uk_a_b (b, a));
create table t2(pk int, a int, b int, primary key (pk), unique key uk_a_b (a, b), constraint cst_name foreign key (a, b) references t1 (b, a) on update CASCADE on delete CASCADE);
insert into t1 values (10, 11, 12), (20, 21, 22);
insert into t2 values (10, 12, 11), (20, 22, 21);
insert into t2 values (30, 32, 31);
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails
drop table t1;
ERROR HY000: Cannot drop table 't1' referenced by a foreign key constraint 'cst_name' on table 't2'
alter table t2 drop foreign key cst_name;
insert into t2 values (30, 32, 31);
drop table t1;
select * from t2 order by pk;
pk a b
10 12 11
20 22 21
30 32 31
drop table if exists jx_t1;
create table jx_t1(pk int, a int, b int, primary key (pk), unique key uk_b (b), foreign key (a) references jx_t1 (b) on update RESTRICT on delete RESTRICT);
insert into jx_t1 values (1, 2, 3);
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails
replace into jx_t1 values (1, NULL, 1);
insert into jx_t1 values (2, NULL, 2);
update jx_t1 set b=22 where pk=2;
select * from jx_t1;
pk a b
1 NULL 1
2 NULL 22
insert into jx_t1 values (3, 4, 4);
# BUG#29775335 - 修复外键指向自身时删除数据失败
delete from jx_t1 where pk = 3;
drop table jx_t1;
create table jx_t1(pk int, a int, b int, c int, d int, primary key (pk), unique key uk_b (a, b), foreign key (c, d) references jx_t1 (a, b) on update RESTRICT on delete RESTRICT);
insert into jx_t1 values(1, 2, 2, 2, 2);
# BUG#29775335 - 修复外键指向自身时删除数据失败
update jx_t1 set a =3, c = 3 where pk = 1;
# BUG#29775335 - 修复外键指向自身时删除数据失败
update jx_t1 set c =3, a = 3 where pk = 1;
drop table jx_t1;
drop table if exists child, parent;
create table parent (id int not null, name varchar(228), primary key (id));
create table child (id int, name varchar(228), parent_id int,
foreign key (parent_id) references parent(id) on delete cascade);
insert into parent value ('1', 'test');
insert into child value ('1', 'test', '1');
select * from parent;
id name
1 test
select * from child;
id name parent_id
1 test 1
delete from parent;
select * from parent;
id name
select * from child;
id name parent_id

View File

@ -0,0 +1,366 @@
#### owner: yuchen.wyc
#### owner group: sql2
#### description: foreign key
--disable_warnings
set foreign_key_checks=on;
#drop tablegroup if exists fk_group;
#create tablegroup fk_group;
drop table if exists t6, t5, t4, t3, t2, t1;
create table t1(pk int, a int, b int, primary key (pk), unique key uk_a_b (b, a));
create table t2(pk int, a int, b int, primary key (pk), unique key uk_a_b (a, b), constraint fake_name_2 foreign key (a, b) references t1 (b, a) on update CASCADE on delete CASCADE);
create table t3(pk int, a int, b int, primary key (pk), unique key uk_a_b (b, a), constraint fake_name_3 foreign key (a, b) references t2 (a, b) on update CASCADE on delete CASCADE);
create table t4(pk int, a int, b int, primary key (pk), unique key uk_a_b (a, b), constraint fake_name_4 foreign key (a, b) references t3 (b, a) on update CASCADE on delete CASCADE);
## insert.
insert into t1 values (10, 11, 12), (20, 21, 22), (30, 31, 32), (40, 41, 42), (50, 51, 52), (60, 61, 62), (70, 71, 72), (80, 81, 82);
insert into t2 values (10, 12, 11), (20, 22, 21), (30, 32, 31), (40, 42, 41), (50, 52, 51), (60, 62, 61), (70, 72, 71);
insert into t3 values (10, 12, 11), (20, 22, 21), (30, 32, 31), (40, 42, 41), (50, 52, 51), (60, 62, 61);
insert into t4 values (10, 11, 12), (20, 21, 22), (30, 31, 32), (40, 41, 42), (50, 51, 52);
--disable_result_log
--error ER_NO_REFERENCED_ROW_2
insert into t2 values (80, 82, 81), (90, 92, 91);
--error ER_NO_REFERENCED_ROW_2
insert into t3 values (70, 72, 71), (80, 82, 81);
--error ER_NO_REFERENCED_ROW_2
insert into t4 values (60, 61, 62), (70, 71, 72);
--enable_result_log
select * from t1 order by pk;
select * from t2 order by pk;
select * from t3 order by pk;
select * from t4 order by pk;
## update.
update t1 set a = 91, b = 92 where pk = 10;
update t2 set a = 82, b = 81 where pk = 20;
update t3 set a = 72, b = 71 where pk = 30;
update t4 set a = 61, b = 62 where pk = 40;
--disable_result_log
--error ER_NO_REFERENCED_ROW_2
update t2 set a = a * 10 where pk = 70;
--error ER_NO_REFERENCED_ROW_2
update t3 set a = a * 10 where pk = 60;
--error ER_NO_REFERENCED_ROW_2
update t4 set a = a * 10 where pk = 50;
--enable_result_log
create table t5(pk int, a int, b int, primary key (pk), unique key uk_a_b (b, a), constraint fake_name_5 foreign key (a, b) references t4 (a, b) on update RESTRICT);
insert into t5 values (10, 91, 92), (20, 81, 82), (30, 71, 72), (40, 61, 62);
--disable_result_log
--error ER_ROW_IS_REFERENCED_2
update t1 set a = 11, b = 12 where pk = 10;
--error ER_ROW_IS_REFERENCED_2
update t2 set a = 22, b = 21 where pk = 20;
--error ER_ROW_IS_REFERENCED_2
update t3 set a = 32, b = 31 where pk = 30;
--error ER_ROW_IS_REFERENCED_2
update t4 set a = 41, b = 42 where pk = 40;
--enable_result_log
# update column unconcerned with foreign key.
update t1 set pk = pk * 10 where pk = 10;
update t2 set pk = pk * 10 where pk = 20;
update t3 set pk = pk * 10 where pk = 30;
update t4 set pk = pk * 10 where pk = 40;
select * from t1 order by pk;
select * from t2 order by pk;
select * from t3 order by pk;
select * from t4 order by pk;
select * from t5 order by pk;
drop table if exists t5;
# delete.
delete from t1 where pk = 10;
delete from t2 where pk = 20;
delete from t3 where pk = 30;
delete from t4 where pk = 40;
select * from t1 order by pk;
select * from t2 order by pk;
select * from t3 order by pk;
select * from t4 order by pk;
create table t5(pk int, a int, b int, primary key (pk), unique key uk_a_b (b, a), constraint fake_name_5 foreign key (a, b) references t4 (a, b) on delete NO ACTION);
insert into t5 values (50, 51, 52);
--disable_result_log
--error ER_ROW_IS_REFERENCED_2
delete from t1 where pk = 50;
--error ER_ROW_IS_REFERENCED_2
delete from t2 where pk = 50;
--error ER_ROW_IS_REFERENCED_2
delete from t3 where pk = 50;
--error ER_ROW_IS_REFERENCED_2
delete from t4 where pk = 50;
--enable_result_log
select * from t1 order by pk;
select * from t2 order by pk;
select * from t3 order by pk;
select * from t4 order by pk;
select * from t5 order by pk;
drop table if exists t5;
delete from t1;
select * from t1 order by pk;
select * from t2 order by pk;
select * from t3 order by pk;
select * from t4 order by pk;
# replace.
replace into t1 values (10, 11, 12), (20, 21, 22), (30, 31, 32), (40, 41, 42), (50, 51, 52), (60, 61, 62), (70, 71, 72), (80, 81, 82);
replace into t2 values (10, 12, 11), (20, 22, 21), (30, 32, 31), (40, 42, 41), (50, 52, 51), (60, 62, 61), (70, 72, 71);
replace into t3 values (10, 12, 11), (20, 22, 21), (30, 32, 31), (40, 42, 41), (50, 52, 51), (60, 62, 61);
replace into t4 values (10, 11, 12), (20, 21, 22), (30, 31, 32), (40, 41, 42), (50, 51, 52);
--disable_result_log
--error ER_NO_REFERENCED_ROW_2
replace into t2 values (80, 82, 81), (90, 92, 91);
--error ER_NO_REFERENCED_ROW_2
replace into t3 values (70, 72, 71), (80, 82, 81);
--error ER_NO_REFERENCED_ROW_2
replace into t4 values (60, 61, 62), (70, 71, 72);
--enable_result_log
select * from t1 order by pk;
select * from t2 order by pk;
select * from t3 order by pk;
select * from t4 order by pk;
replace into t1 values (10, 91, 92);
replace into t2 values (20, 82, 81);
replace into t3 values (30, 72, 71);
replace into t4 values (40, 61, 62);
select * from t1 order by pk;
select * from t2 order by pk;
select * from t3 order by pk;
select * from t4 order by pk;
create table t5(pk int, a int, b int, primary key (pk), unique key uk_a_b (b, a), constraint fake_name_5 foreign key (a, b) references t4 (a, b));
replace into t5 values (50, 51, 52);
--disable_result_log
--error ER_ROW_IS_REFERENCED_2
replace into t1 values (50, 11, 12);
--error ER_ROW_IS_REFERENCED_2
replace into t2 values (50, 22, 21);
--error ER_ROW_IS_REFERENCED_2
replace into t3 values (50, 32, 31);
--error ER_ROW_IS_REFERENCED_2
replace into t4 values (50, 41, 42);
--enable_result_log
select * from t1 order by pk;
select * from t2 order by pk;
select * from t3 order by pk;
select * from t4 order by pk;
select * from t5 order by pk;
drop table if exists t5;
delete from t1;
select * from t1 order by pk;
select * from t2 order by pk;
select * from t3 order by pk;
select * from t4 order by pk;
# insert on dup.
insert into t1 values (10, 11, 12), (20, 21, 22), (30, 31, 32), (40, 41, 42), (50, 51, 52), (60, 61, 62), (70, 71, 72), (80, 81, 82);
insert into t2 values (10, 12, 11), (20, 22, 21), (30, 32, 31), (40, 42, 41), (50, 52, 51), (60, 62, 61), (70, 72, 71);
insert into t3 values (10, 12, 11), (20, 22, 21), (30, 32, 31), (40, 42, 41), (50, 52, 51), (60, 62, 61);
insert into t4 values (10, 11, 12), (20, 21, 22), (30, 31, 32), (40, 41, 42), (50, 51, 52);
select * from t1 order by pk;
select * from t2 order by pk;
select * from t3 order by pk;
select * from t4 order by pk;
insert into t1 values (10, 101, 102) on duplicate key update a = 91, b = 92;
insert into t2 values (20, 92, 91) on duplicate key update a = 82, b = 81;
insert into t3 values (30, 82, 81) on duplicate key update a = 72, b = 71;
insert into t4 values (40, 71, 72) on duplicate key update a = 61, b = 62;
select * from t1 order by pk;
select * from t2 order by pk;
select * from t3 order by pk;
select * from t4 order by pk;
--disable_result_log
--error ER_NO_REFERENCED_ROW_2
insert into t2 values (20, 0, 0) on duplicate key update a = a * 10;
--error ER_NO_REFERENCED_ROW_2
insert into t3 values (30, 0, 0) on duplicate key update a = a * 10;
--error ER_NO_REFERENCED_ROW_2
insert into t4 values (40, 0, 0) on duplicate key update a = a * 10;
--enable_result_log
create table t5(pk int, a int, b int, primary key (pk), unique key uk_a_b (b, a), constraint fake_name_5 foreign key (a, b) references t4 (a, b));
insert into t5 values (10, 91, 92), (20, 81, 82), (30, 71, 72), (40, 61, 62);
--disable_result_log
--error ER_ROW_IS_REFERENCED_2
insert into t1 values (10, 101, 102) on duplicate key update a = 101, b = 102;
insert into t1 values (100, 101, 102);
--error ER_ROW_IS_REFERENCED_2
insert into t2 values (20, 102, 101) on duplicate key update a = 102, b = 101;
insert into t2 values (100, 102, 101);
--error ER_ROW_IS_REFERENCED_2
insert into t3 values (30, 102, 101) on duplicate key update a = 102, b = 101;
insert into t3 values (100, 102, 101);
--error ER_ROW_IS_REFERENCED_2
insert into t4 values (40, 101, 102) on duplicate key update a = 101, b = 102;
insert into t4 values (100, 101, 102);
--enable_result_log
select * from t1 order by pk;
select * from t2 order by pk;
select * from t3 order by pk;
select * from t4 order by pk;
select * from t5 order by pk;
# update column concerned with foreign key but old value is equal to new value.
insert into t1 values (0, 101, 102) on duplicate key update a = 101, b = 102;
insert into t2 values (0, 102, 101) on duplicate key update a = 102, b = 101;
insert into t3 values (0, 102, 101) on duplicate key update a = 102, b = 101;
insert into t4 values (0, 101, 102) on duplicate key update a = 101, b = 102;
select * from t1 order by pk;
select * from t2 order by pk;
select * from t3 order by pk;
select * from t4 order by pk;
select * from t5 order by pk;
# update column unconcerned with foreign key.
insert into t1 values (0, 21, 22) on duplicate key update pk = 1000;
insert into t2 values (0, 32, 31) on duplicate key update pk = 1010;
insert into t3 values (0, 42, 41) on duplicate key update pk = 1020;
insert into t4 values (0, 51, 52) on duplicate key update pk = 1030;
select * from t1 order by pk;
select * from t2 order by pk;
select * from t3 order by pk;
select * from t4 order by pk;
select * from t5 order by pk;
drop table if exists t5, t4, t3, t2, t1;
## more datatypes, especially datetime, varchar, and NULL.
create table t1(pk int, a varchar(16), b datetime(6), primary key (pk), unique key uk_a_b (b, a));
create table t2(pk int, a datetime(6), b varchar(16), primary key (pk), unique key uk_a_b (a, b), foreign key (a, b) references t1 (b, a) on update CASCADE on delete CASCADE);
insert into t1 values (10, 'abc', '2018-03-31 12:00:00'),
(20, 'DEF', '2018-03-31 13:00:00'),
(30, 'ghi', '2018-03-31 14:00:00'),
(40, 'JKL', '2018-03-31 15:00:00'),
(50, 'mno', '2018-03-31 16:00:00'),
(60, 'PQR', '2018-03-31 17:00:00');
insert into t2 values (10, '2018-03-31 12:00:00', 'ABC'),
(20, '2018-03-31 13:00:00', 'def'),
(30, '2018-03-31 14:00:00', 'GHI'),
(40, '2018-03-31 15:00:00', 'jkl'),
(50, '2018-03-31 00:00:00', NULL),
(60, NULL, NULL);
delete from t1 where pk = 10;
update t1 set a = 'stu', b = '2018-03-31 19:00:00' where pk = 20;
update t1 set a = NULL where pk = 30;
select * from t1 order by pk;
select * from t2 order by pk;
delete from t1 where pk = 30;
update t2 set a = NULL, b = 'vwx' where pk = 20;
select * from t1 order by pk;
select * from t2 order by pk;
create table t3(pk int, a datetime(6), b varchar(16), primary key (pk), unique key uk_a_b (a, b), foreign key (a, b) references t1 (b, a));
insert into t3 values (40, '2018-03-31 15:00:00', 'jkl');
update t2 set b = 'hello' where pk = 20;
select * from t2 order by pk;
select * from t3 order by pk;
delete from t2 where pk = 20;
select * from t2 order by pk;
select * from t3 order by pk;
--disable_result_log
--error ER_NO_REFERENCED_ROW_2
insert into t3 values (10, '2018-03-31 12:00:00', 'ABC');
--error ER_ROW_IS_REFERENCED_2
update t1 set a = 'hello' where pk = 40;
--error ER_ROW_IS_REFERENCED_2
delete from t1 where pk = 40;
--enable_result_log
drop table if exists t3, t2, t1;
## foreign_key_checks on/off.
create table t1(pk int, a int, b int, primary key (pk), unique key uk_a_b (b, a));
create table t2(pk int, a int, b int, primary key (pk), unique key uk_a_b (a, b), constraint fake_name_2 foreign key (a, b) references t1 (b, a));
set foreign_key_checks=on;
insert into t1 values (10, 11, 12), (20, 21, 22), (30, 31, 32), (40, 41, 42), (50, 51, 52), (60, 61, 62), (70, 71, 72), (80, 81, 82);
insert into t2 values (10, 12, 11), (20, 22, 21), (30, 32, 31), (40, 42, 41), (50, 52, 51), (60, 62, 61), (70, 72, 71);
--disable_result_log
--error ER_NO_REFERENCED_ROW_2
insert into t2 values (80, 82, 81), (90, 92, 91);
--error ER_ROW_IS_REFERENCED_2
update t1 set a = 101, b = 102 where pk = 10;
--error ER_ROW_IS_REFERENCED_2
delete from t1 where pk = 20;
--enable_result_log
select * from t1 order by pk;
select * from t2 order by pk;
set foreign_key_checks=off;
insert into t2 values (80, 82, 81), (90, 92, 91);
update t1 set a = 101, b = 102 where pk = 10;
delete from t1 where pk = 20;
select * from t1 order by pk;
select * from t2 order by pk;
set foreign_key_checks=on;
drop table if exists t3, t2, t1;
## self reference.
create table t1(pk int, a int, b int, c int, d int, primary key (a, b), constraint fake_name_1 foreign key (c, d) references t1 (a, b) on update CASCADE on delete CASCADE);
insert into t1 values (10, 11, 12, NULL, NULL), (20, 13, 14, NULL, NULL);
insert into t1 values (30, 21, 22, 11, 12), (40, 23, 24, 11, 12);
insert into t1 values (50, 31, 32, 21, 22), (60, 33, 34, 21, 22), (70, 35, 36, 23, 24), (80, 37, 38, 23, 24);
insert into t1 values (90, 41, 42, 31, 32), (100, 43, 44, 33, 34), (110, 45, 46, 35, 36), (120, 47, 48, 37, 38);
--disable_result_log
--error ER_NO_REFERENCED_ROW_2
insert into t1 values (130, 51, 52, 41, 42), (140, 53, 54, 49, 50);
--error ER_NO_REFERENCED_ROW_2
update t1 set c = 15, d = 16 where pk = 30;
--enable_result_log
select * from t1 order by pk;
# mysql will return ER_ROW_IS_REFERENCED_2, maybe bug.
# update t1 set a = 25, b = 26 where pk = 40;
delete from t1 where pk = 50;
select * from t1 order by pk;
drop table if exists t1;
drop table if exists t2, t1;
create table t1(pk int, a int, b int, primary key (pk), unique key uk_a_b (b, a));
create table t2(pk int, a int, b int, primary key (pk), unique key uk_a_b (a, b), constraint cst_name foreign key (a, b) references t1 (b, a) on update CASCADE on delete CASCADE);
insert into t1 values (10, 11, 12), (20, 21, 22);
insert into t2 values (10, 12, 11), (20, 22, 21);
--error ER_NO_REFERENCED_ROW_2
insert into t2 values (30, 32, 31);
--error 3730
drop table t1;
alter table t2 drop foreign key cst_name;
insert into t2 values (30, 32, 31);
drop table t1;
select * from t2 order by pk;
#bug:
drop table if exists jx_t1;
create table jx_t1(pk int, a int, b int, primary key (pk), unique key uk_b (b), foreign key (a) references jx_t1 (b) on update RESTRICT on delete RESTRICT);
--error ER_NO_REFERENCED_ROW_2
insert into jx_t1 values (1, 2, 3);
replace into jx_t1 values (1, NULL, 1);
insert into jx_t1 values (2, NULL, 2);
update jx_t1 set b=22 where pk=2;
select * from jx_t1;
insert into jx_t1 values (3, 4, 4);
--echo # BUG#29775335 - 修复外键指向自身时删除数据失败
delete from jx_t1 where pk = 3;
drop table jx_t1;
create table jx_t1(pk int, a int, b int, c int, d int, primary key (pk), unique key uk_b (a, b), foreign key (c, d) references jx_t1 (a, b) on update RESTRICT on delete RESTRICT);
insert into jx_t1 values(1, 2, 2, 2, 2);
--echo # BUG#29775335 - 修复外键指向自身时删除数据失败
update jx_t1 set a =3, c = 3 where pk = 1;
--echo # BUG#29775335 - 修复外键指向自身时删除数据失败
update jx_t1 set c =3, a = 3 where pk = 1;
drop table jx_t1;
# issue/18132630
drop table if exists child, parent;
create table parent (id int not null, name varchar(228), primary key (id));
create table child (id int, name varchar(228), parent_id int,
foreign key (parent_id) references parent(id) on delete cascade);
insert into parent value ('1', 'test');
insert into child value ('1', 'test', '1');
select * from parent;
select * from child;
delete from parent;
select * from parent;
select * from child;
--enable_warnings