github.com/matrixorigin/matrixone@v1.2.0/test/distributed/cases/foreign_key/fk_self_refer3.sql (about) 1 drop database if exists fk_self_refer3; 2 create database fk_self_refer3; 3 use fk_self_refer3; 4 5 drop table if exists t1; 6 ----create two FKs on a unique key and a primary key---- 7 -- test only two FKs self refer in a table 8 create table t1(a int, b int, 9 c int, 10 d int, e int, 11 f int, g int, 12 primary key (a,b), 13 unique key (a,c), 14 constraint `c1` foreign key fk1(d,e) references t1(a,b), 15 constraint `c2` foreign key fk2(f,g) references t1(a,c) 16 ); 17 show create table t1; 18 19 --no error 20 insert into t1 values (1,2,1,1,2,1,1); 21 22 --no error 23 insert into t1 values (1,3,2,1,3,1,2); 24 25 --error. 5 does not exist in the column b 26 insert into t1 values (1,4,3,1,5,1,2); 27 28 --error. 4 does not exist in the column c 29 insert into t1 values (1,4,3,1,4,1,4); 30 31 --no error 32 insert into t1 values (1,4,3,1,4,1,3); 33 34 --error 35 update t1 set b = 5 where b = 4; 36 37 --error. 5 does not exist in the column b 38 update t1 set e = 5 where b = 4; 39 40 update t1 set e = NULL where b = 4; 41 42 --error. duplicate 3 in the column b 43 --update t1 set b = 3 where b = 4; 44 45 --no error. 46 update t1 set b = 5 where b = 4; 47 48 --error. 49 update t1 set c = 4 where b = 5; 50 51 --error 52 update t1 set g = 4 where b = 5; 53 54 --no error 55 update t1 set g = 2 where b = 5; 56 57 --error. duplicate 2 in the column c 58 --update t1 set c = 2 where b = 5; 59 60 --no error. 61 update t1 set c = 4 where b = 5; 62 63 --no error. 64 delete from t1 where b = 5; 65 66 --error 67 delete from t1 where b = 3; 68 69 update t1 set e = NULL where b = 3; 70 71 --error 72 delete from t1 where b = 3; 73 74 update t1 set g = NULL where b = 3; 75 76 --no error 77 delete from t1 where b = 3; 78 79 --error 80 delete from t1 where b = 2; 81 82 update t1 set e = NULL where b = 2; 83 84 --error 85 delete from t1 where b = 2; 86 87 update t1 set g = NULL where b = 2; 88 89 delete from t1 where b = 2; 90 91 select count(*) from t1; 92 93 drop table if exists p1; 94 create table p1( 95 pa int, 96 pb int, 97 primary key (pa,pb) 98 ); 99 100 drop table if exists q2; 101 create table q2( 102 qa int, 103 qb int, 104 unique key (qa,qb) 105 ); 106 107 drop table if exists t1; 108 ----create two FKs on a unique key and a primary key---- 109 ----create tow FKs on two parent tables. 110 -- test only two FKs self refer in a table 111 create table t1(a int, b int, 112 c int, 113 d int, e int, 114 f int, g int, 115 h int, i int, 116 j int, k int, 117 primary key (a,b), 118 unique key (a,c), 119 constraint `c1` foreign key fk1(d,e) references t1(a,b), 120 constraint `c2` foreign key fk2(f,h) references t1(a,c), 121 constraint `c3` foreign key fk3(h,i) references p1(pa,pb), 122 constraint `c4` foreign key fk4(h,k) references q2(qa,qb) 123 ); 124 show create table t1; 125 126 --error. nothing parent tables 127 insert into t1 values (1,2,3,1,2,1,3,4,4,4,4); 128 129 insert into p1 values (4,4); 130 131 insert into q2 values (4,4); 132 133 --no error 134 insert into t1 values ( 1,2, 4, 1,2, 1,10, 4,4, 10,4); 135 136 --no error 137 insert into t1 values (1,3,3,1,2,1,10,NULL,NULL,NULL,4); 138 139 --error 140 insert into t1 values ( 141 1,4,5, 142 1,3, 143 1,5, 144 5,5, 145 10,5); 146 147 insert into p1 values (5,5); 148 149 insert into q2 values (5,5); 150 151 --no error 152 insert into t1 values ( 153 1,4,5, 154 1,3, 155 1,5, 156 5,5, 157 10,5); 158 159 --error. duplicate 160 --update t1 set c = 4 where b = 3; 161 162 --no error 163 update t1 set c = 6 where b = 3; 164 165 --no error 166 update t1 set c = NULL where b = 3; 167 168 --error 169 delete from t1 where c = 4; 170 171 --error 172 delete from t1 where c = 4; 173 174 --no error 175 update t1 set h = NULL where c = 4; 176 177 --error 178 delete from t1 where c = 4; 179 180 --no error 181 update t1 set d = NULL where c = 4; 182 183 --error 184 delete from t1 where c = 4; 185 186 --no error 187 update t1 set f = NULL,g = NULL where c = 4; 188 189 --error 190 delete from t1 where c = 4; 191 192 --no error 193 update t1 set i = NULL, j = NULL, k = NULL where c = 4; 194 195 --error 196 delete from t1 where c = 4; 197 198 199 update t1 set c = NULL where b = 2; 200 201 --error 202 delete from t1 where b = 2; 203 204 205 ---delete or update rows on fk self table 206 drop table if exists p1; 207 create table t1(a int primary key,b int,constraint `c1` foreign key fk1(b) references t1(a)); 208 show tables; 209 show create table t1; 210 insert into t1 values (1,1); 211 insert into t1 values (2,1); 212 insert into t1 values (3,2); 213 214 --error 215 delete A from t1 as A, t1 as B where A.a = B.b; 216 217 --error 218 delete A,B from t1 as A, t1 as B where A.a = B.b; 219 220 --error 221 update t1 as A,t1 as B set A.a = 4 where A.a = B.b; 222 223 --error 224 update t1 as A,t1 as B set A.a = 4, B.b = 3 where A.a = B.b; 225 226 --error 227 update t1 as A,t1 as B set A.a = 4, A.b = 3 where A.a = B.b; 228 229 --error 230 update t1 as A,t1 as B set B.a = 4 where A.a = B.b; 231 232 --no error 233 update t1 as A,t1 as B set A.a = 4 where A.a = 3; 234 235 --error 236 update t1 as A set A.a = 3, A.b = 3 where A.a = A.b; 237 238 --no error 239 insert into t1 values (3,3); 240 241 --error 242 update t1 as A set A.a = 4, A.b = 4 where A.a = A.b and A.a = 3; 243 244 --error 245 update t1 as A set A.b = 4, A.a = 4 where A.a = A.b and A.a = 3; 246 247 drop table if exists t1; 248 drop database if exists fk_self_refer3;