summaryrefslogtreecommitdiff
path: root/mysql-test/main/foreign_key.result
blob: 08cbcc9159afa9d617f6b27b3b204de0749268eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
drop table if exists t1,t2;
create table t2 (
a int unique, c int unique);
create table t3 (
a int unique, c int unique, d int unique,
r int references t3(d));
create table t1 (
a int not null references t2,
b int not null constraint t2_c references t2 (c),
primary key (a,b),
foreign key (a) references t3 match full,
foreign key (a) references t3 match partial,
foreign key (a,b) references t3 (c,d) on delete no action
on update no action,
foreign key (a,b) references t3 (c,d) on update cascade,
foreign key (a,b) references t3 (c,d) on delete set default,
foreign key (a,b) references t3 (c,d) on update set null);
create index a on t1 (a);
create unique index b on t1 (a,b);
drop tables t2;
ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (fk_t1)
drop tables t3;
ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (fk_t1_2)
drop tables t1, t2, t3;
create table t1 (id int primary key) engine = innodb;
create table t2 (id int PRIMARY KEY, FOREIGN KEY (id) REFERENCES t1(id)) engine=innodb;
insert into t1 values (1), (2), (3), (4), (5), (6);
insert into t2 values (3), (5);
delete from t1;
ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`t2`, CONSTRAINT `fk_t2` FOREIGN KEY (`id`) REFERENCES `t1` (`id`))
select * from t1;
id
1
2
3
4
5
6
delete ignore from t1;
Warnings:
Warning	1451	Cannot delete or update a parent row: a foreign key constraint fails (`test`.`t2`, CONSTRAINT `fk_t2` FOREIGN KEY (`id`) REFERENCES `t1` (`id`))
Warning	1451	Cannot delete or update a parent row: a foreign key constraint fails (`test`.`t2`, CONSTRAINT `fk_t2` FOREIGN KEY (`id`) REFERENCES `t1` (`id`))
select row_count();
row_count()
-1
select * from t1;
id
3
5
drop table t2;
drop table t1;
drop table if exists t_34455;
create table t_34455 (
a int not null,
foreign key (a) references t3 (a) match full match partial);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'match partial)' at line 3
create table t_34455 (
a int not null,
foreign key (a) references t3 (a) on delete set default match full);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'match full)' at line 3
create table t_34455 (
a int not null,
foreign key (a) references t3 (a) on update set default match full);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'match full)' at line 3
create table t_34455 (
a int not null,
foreign key (a) references t3 (a)
on delete set default on delete set default);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'delete set default)' at line 4
create table t_34455 (
a int not null,
foreign key (a) references t3 (a)
on update set default on update set default);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'update set default)' at line 4
create table t_34455 (a int not null);
alter table t_34455
add foreign key (a) references t3 (a) match full match partial);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'match partial)' at line 2
alter table t_34455
add foreign key (a) references t3 (a) on delete set default match full);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'match full)' at line 2
alter table t_34455
add foreign key (a) references t3 (a) on update set default match full);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'match full)' at line 2
alter table t_34455
add foreign key (a) references t3 (a)
on delete set default on delete set default);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'delete set default)' at line 3
alter table t_34455
add foreign key (a) references t3 (a)
on update set default on update set default);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'update set default)' at line 3
drop table t_34455;
#
# MDEV-18460 Don't allow multiple table CONSTRAINTs with the same name.
#
CREATE TABLE tpk (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,name VARCHAR(100) NOT NULL) ENGINE=Innodb;
CREATE TABLE tfk (c1 INT, c2 INT, CONSTRAINT sid UNIQUE (c1), CONSTRAINT sid CHECK (c2>15));
ERROR HY000: Duplicate CHECK constraint name 'sid'
CREATE TABLE tfk (c1 INT, c2 INT, CONSTRAINT sid UNIQUE (c1));
ALTER TABLE tfk ADD CONSTRAINT sid CHECK (c2>15);
ERROR HY000: Duplicate CHECK constraint name 'sid'
DROP TABLE tfk;
CREATE TABLE tfk (c1 INT, c2 INT,
CONSTRAINT sid FOREIGN KEY (c1) REFERENCES tpk (id)) ENGINE=Innodb;
show create table tfk;
Table	Create Table
tfk	CREATE TABLE `tfk` (
  `c1` int(11) DEFAULT NULL,
  `c2` int(11) DEFAULT NULL,
  KEY `sid` (`c1`),
  CONSTRAINT `sid` FOREIGN KEY (`c1`) REFERENCES `tpk` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
ALTER TABLE tfk ADD CONSTRAINT sid CHECK (c2>15);
ERROR HY000: Duplicate CHECK constraint name 'sid'
ALTER TABLE tfk ADD CONSTRAINT sid UNIQUE(c2);
ERROR 42000: Duplicate key name 'sid'
DROP TABLE tfk;
DROP TABLE tpk;
#
# MDEV-16417 Store Foreign Key metadata outside of InnoDB
#
set default_storage_engine= innodb;
# Check create table
create or replace table t1 (id int primary key);
create or replace table t2 (id int primary key, foreign key (id) references t1(id));
select * from t1, t2;
id	id
check tables t1, t2;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 1 referenced keys
test.t1	check	status	OK
test.t2	check	Note	Found 1 foreign keys
test.t2	check	status	OK
flush tables t1, t2;
check tables t1, t2;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 1 referenced keys
test.t1	check	status	OK
test.t2	check	Note	Found 1 foreign keys
test.t2	check	status	OK
drop table t2, t1;
create table t1 (id int primary key);
create table t2 (id int references t1(id)) select id from t1;
check tables t1, t2;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 1 referenced keys
test.t1	check	status	OK
test.t2	check	Note	Found 1 foreign keys
test.t2	check	status	OK
flush tables;
check tables t1, t2;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 1 referenced keys
test.t1	check	status	OK
test.t2	check	Note	Found 1 foreign keys
test.t2	check	status	OK
drop table t2, t1;
create database D;
create table D.T1(id int primary key);
create table t2(id int primary key, f1 int references D.T1(id));
select * from D.T1;
id
check table D.T1;
Table	Op	Msg_type	Msg_text
D.T1	check	Note	Found 1 referenced keys
D.T1	check	status	OK
drop table t2;
check table D.T1;
Table	Op	Msg_type	Msg_text
D.T1	check	status	OK
flush tables;
check table D.T1;
Table	Op	Msg_type	Msg_text
D.T1	check	status	OK
drop table D.T1;
drop database D;
# Check duplicate id
create or replace table t1 (id int primary key) engine innodb;
create or replace table t2 (id2 int,
constraint c foreign key (id2) references t1 (id),
constraint c foreign key (id2) references t1 (id));
ERROR HY000: Duplicate FOREIGN KEY constraint name 'c'
create or replace table t2 (id2 int, constraint c foreign key (id2) references t1 (id));
alter table t2 add constraint c foreign key (id2) references t1 (id);
ERROR HY000: Duplicate FOREIGN KEY constraint name 'c'
create or replace table t3 (id2 int, constraint c foreign key (id2) references t1 (id));
ERROR HY000: Duplicate FOREIGN KEY constraint name 'c'
create or replace table t3 (id2 int, constraint C foreign key (id2) references t1 (id));
create or replace table t3 (id2 int);
alter table t2 add constraint c foreign key (id2) references t1 (id);
ERROR HY000: Duplicate FOREIGN KEY constraint name 'c'
alter table t2 add constraint fk_t3 foreign key (id2) references t1 (id);
alter table t3 add foreign key (id2) references t1 (id);
ERROR HY000: Duplicate FOREIGN KEY constraint name 'fk_t3'
drop tables t3, t2, t1;
# Check rename column, lock tables
create or replace table t1 (id int primary key);
create or replace table t2 (id int primary key);
create or replace table t3 (id int primary key);
create or replace table ch1 (
id int, id2 int,
foreign key (id) references t1 (id),
foreign key (id2) references t2 (id),
foreign key (id) references t3 (id));
select * from t1, t2, t3;
id	id	id
connect con1, localhost, root;
lock tables t3 read;
connection default;
set @saved_lock_wait_timeout= @@lock_wait_timeout;
set lock_wait_timeout= 1;
alter table ch1 change id2 xid2 int;
alter table ch1 change id xid int;
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
set lock_wait_timeout= @saved_lock_wait_timeout;
connection con1;
unlock tables;
disconnect con1;
connection default;
alter table ch1 change id xid int;
select * from ch1;
xid	xid2
check tables t1, t2, t3;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 1 referenced keys
test.t1	check	status	OK
test.t2	check	Note	Found 1 referenced keys
test.t2	check	status	OK
test.t3	check	Note	Found 1 referenced keys
test.t3	check	status	OK
flush tables t1, t2, t3;
check tables t1, t2, t3;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 1 referenced keys
test.t1	check	status	OK
test.t2	check	Note	Found 1 referenced keys
test.t2	check	status	OK
test.t3	check	Note	Found 1 referenced keys
test.t3	check	status	OK
drop tables ch1, t2, t1, t3;
# Rename column on referenced table
create or replace table t1 (id int primary key);
create or replace table t2 (id int references t1, id2 int references t1(id));
select * from t2;
id	id2
alter table t1 change id xid int;
show create table t2;
Table	Create Table
t2	CREATE TABLE `t2` (
  `id` int(11) DEFAULT NULL,
  `id2` int(11) DEFAULT NULL,
  KEY `fk_t2` (`id`),
  KEY `fk_t2_2` (`id2`),
  CONSTRAINT `fk_t2` FOREIGN KEY (`id`) REFERENCES `t1` (`xid`),
  CONSTRAINT `fk_t2_2` FOREIGN KEY (`id2`) REFERENCES `t1` (`xid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
select * from t1;
xid
check table t2;
Table	Op	Msg_type	Msg_text
test.t2	check	Note	Found 2 foreign keys
test.t2	check	status	OK
flush table t2;
check table t2;
Table	Op	Msg_type	Msg_text
test.t2	check	Note	Found 2 foreign keys
test.t2	check	status	OK
alter table t1 rename column xid to yid;
show create table t2;
Table	Create Table
t2	CREATE TABLE `t2` (
  `id` int(11) DEFAULT NULL,
  `id2` int(11) DEFAULT NULL,
  KEY `fk_t2` (`id`),
  KEY `fk_t2_2` (`id2`),
  CONSTRAINT `fk_t2` FOREIGN KEY (`id`) REFERENCES `t1` (`yid`),
  CONSTRAINT `fk_t2_2` FOREIGN KEY (`id2`) REFERENCES `t1` (`yid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
select * from t1;
yid
check table t2;
Table	Op	Msg_type	Msg_text
test.t2	check	Note	Found 2 foreign keys
test.t2	check	status	OK
flush table t2;
check table t2;
Table	Op	Msg_type	Msg_text
test.t2	check	Note	Found 2 foreign keys
test.t2	check	status	OK
drop tables t2, t1;
# Check rename table
create or replace table t1 (id int primary key);
create or replace table t2 (id int references t1);
select * from t2, t1;
id	id
rename table t2 to t3;
create table t2 (x int);
check table t1;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 1 referenced keys
test.t1	check	status	OK
flush tables t1;
check table t1;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 1 referenced keys
test.t1	check	status	OK
drop tables t3, t1, t2;
create or replace table t1 (id int primary key);
create or replace table t2 (id int primary key);
create or replace table t3 (id int primary key);
select * from t1, t2, t3;
id	id	id
create or replace table t4 (
id int primary key, id2 int references t4(id),
foreign key (id) references t1 (id),
foreign key (id2) references t2 (id),
foreign key (id) references t3 (id));
select * from t4;
id	id2
rename table t4 to xt4;
check tables t1, t2, t3;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 1 referenced keys
test.t1	check	status	OK
test.t2	check	Note	Found 1 referenced keys
test.t2	check	status	OK
test.t3	check	Note	Found 1 referenced keys
test.t3	check	status	OK
flush tables t1, t2, t3;
check tables t1, t2, t3;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 1 referenced keys
test.t1	check	status	OK
test.t2	check	Note	Found 1 referenced keys
test.t2	check	status	OK
test.t3	check	Note	Found 1 referenced keys
test.t3	check	status	OK
alter table xt4 rename to yt4, algorithm=inplace;
select * from t1, t2, t3;
id	id	id
check tables t1, t2, t3;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 1 referenced keys
test.t1	check	status	OK
test.t2	check	Note	Found 1 referenced keys
test.t2	check	status	OK
test.t3	check	Note	Found 1 referenced keys
test.t3	check	status	OK
flush tables t1, t2, t3;
check tables t1, t2, t3;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 1 referenced keys
test.t1	check	status	OK
test.t2	check	Note	Found 1 referenced keys
test.t2	check	status	OK
test.t3	check	Note	Found 1 referenced keys
test.t3	check	status	OK
alter table yt4 rename to zt4, algorithm=copy;
select * from t1, t2, t3;
id	id	id
check tables t1, t2, t3;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 1 referenced keys
test.t1	check	status	OK
test.t2	check	Note	Found 1 referenced keys
test.t2	check	status	OK
test.t3	check	Note	Found 1 referenced keys
test.t3	check	status	OK
flush tables t1, t2, t3;
check tables t1, t2, t3;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 1 referenced keys
test.t1	check	status	OK
test.t2	check	Note	Found 1 referenced keys
test.t2	check	status	OK
test.t3	check	Note	Found 1 referenced keys
test.t3	check	status	OK
drop tables zt4, t2, t1, t3;
# Rename of referenced table
create or replace table t1 (id int primary key);
create or replace table t2 (id int references t1);
select * from t2;
id
rename table t1 to xt1;
show create table t2;
Table	Create Table
t2	CREATE TABLE `t2` (
  `id` int(11) DEFAULT NULL,
  KEY `fk_t2` (`id`),
  CONSTRAINT `fk_t2` FOREIGN KEY (`id`) REFERENCES `xt1` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
select * from xt1;
id
check tables t2, xt1;
Table	Op	Msg_type	Msg_text
test.t2	check	Note	Found 1 foreign keys
test.t2	check	status	OK
test.xt1	check	Note	Found 1 referenced keys
test.xt1	check	status	OK
flush tables t2, xt1;
check tables t2, xt1;
Table	Op	Msg_type	Msg_text
test.t2	check	Note	Found 1 foreign keys
test.t2	check	status	OK
test.xt1	check	Note	Found 1 referenced keys
test.xt1	check	status	OK
alter table xt1 rename to yt1, algorithm=inplace;
show create table t2;
Table	Create Table
t2	CREATE TABLE `t2` (
  `id` int(11) DEFAULT NULL,
  KEY `fk_t2` (`id`),
  CONSTRAINT `fk_t2` FOREIGN KEY (`id`) REFERENCES `yt1` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
select * from yt1;
id
check tables t2, yt1;
Table	Op	Msg_type	Msg_text
test.t2	check	Note	Found 1 foreign keys
test.t2	check	status	OK
test.yt1	check	Note	Found 1 referenced keys
test.yt1	check	status	OK
flush tables t2, yt1;
check tables t2, yt1;
Table	Op	Msg_type	Msg_text
test.t2	check	Note	Found 1 foreign keys
test.t2	check	status	OK
test.yt1	check	Note	Found 1 referenced keys
test.yt1	check	status	OK
alter table yt1 rename to t1, algorithm=copy;
show create table t2;
Table	Create Table
t2	CREATE TABLE `t2` (
  `id` int(11) DEFAULT NULL,
  KEY `fk_t2` (`id`),
  CONSTRAINT `fk_t2` FOREIGN KEY (`id`) REFERENCES `t1` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
select * from t1;
id
check tables t2, t1;
Table	Op	Msg_type	Msg_text
test.t2	check	Note	Found 1 foreign keys
test.t2	check	status	OK
test.t1	check	Note	Found 1 referenced keys
test.t1	check	status	OK
flush tables t2, t1;
check tables t2, t1;
Table	Op	Msg_type	Msg_text
test.t2	check	Note	Found 1 foreign keys
test.t2	check	status	OK
test.t1	check	Note	Found 1 referenced keys
test.t1	check	status	OK
drop tables t2, t1;
# Multi-rename
create or replace table t1 (x int unique, y int unique, z int unique);
create or replace table t2 (x int references t1(x));
create or replace table t3 (x int references t2(x), y int references t1(y));
create or replace table t4 (x int);
create or replace table fail_t4 (x int);
rename table t1 to xt1, t4 to fail_t4;
ERROR 42S01: Table 'fail_t4' already exists
check tables t1, t2, t3;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 2 referenced keys
test.t1	check	status	OK
test.t2	check	Note	Found 1 referenced keys
test.t2	check	Note	Found 1 foreign keys
test.t2	check	status	OK
test.t3	check	Note	Found 2 foreign keys
test.t3	check	status	OK
flush tables t1, t2, t3;
check tables t1, t2, t3;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 2 referenced keys
test.t1	check	status	OK
test.t2	check	Note	Found 1 referenced keys
test.t2	check	Note	Found 1 foreign keys
test.t2	check	status	OK
test.t3	check	Note	Found 2 foreign keys
test.t3	check	status	OK
rename table t1 to xt1, t2 to xt2, t3 to xt3, t4 to fail_t4;
ERROR 42S01: Table 'fail_t4' already exists
check tables t1, t2, t3, xt1, xt2, xt3;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 2 referenced keys
test.t1	check	status	OK
test.t2	check	Note	Found 1 referenced keys
test.t2	check	Note	Found 1 foreign keys
test.t2	check	status	OK
test.t3	check	Note	Found 2 foreign keys
test.t3	check	status	OK
test.xt1	check	Error	Table 'test.xt1' doesn't exist
test.xt1	check	status	Operation failed
test.xt2	check	Error	Table 'test.xt2' doesn't exist
test.xt2	check	status	Operation failed
test.xt3	check	Error	Table 'test.xt3' doesn't exist
test.xt3	check	status	Operation failed
flush tables t1, t2, t3;
check tables t1, t2, t3;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 2 referenced keys
test.t1	check	status	OK
test.t2	check	Note	Found 1 referenced keys
test.t2	check	Note	Found 1 foreign keys
test.t2	check	status	OK
test.t3	check	Note	Found 2 foreign keys
test.t3	check	status	OK
rename table t3 to xt3, t2 to xt2, t1 to xt1, t4 to fail_t4;
ERROR 42S01: Table 'fail_t4' already exists
check tables t1, t2, t3, xt1, xt2, xt3;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 2 referenced keys
test.t1	check	status	OK
test.t2	check	Note	Found 1 referenced keys
test.t2	check	Note	Found 1 foreign keys
test.t2	check	status	OK
test.t3	check	Note	Found 2 foreign keys
test.t3	check	status	OK
test.xt1	check	Error	Table 'test.xt1' doesn't exist
test.xt1	check	status	Operation failed
test.xt2	check	Error	Table 'test.xt2' doesn't exist
test.xt2	check	status	Operation failed
test.xt3	check	Error	Table 'test.xt3' doesn't exist
test.xt3	check	status	Operation failed
flush tables t1, t2, t3;
check tables t1, t2, t3;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 2 referenced keys
test.t1	check	status	OK
test.t2	check	Note	Found 1 referenced keys
test.t2	check	Note	Found 1 foreign keys
test.t2	check	status	OK
test.t3	check	Note	Found 2 foreign keys
test.t3	check	status	OK
rename table t1 to xt1, t2 to xt2, t3 to xt3;
check tables xt1, xt2, xt3;
Table	Op	Msg_type	Msg_text
test.xt1	check	Note	Found 2 referenced keys
test.xt1	check	status	OK
test.xt2	check	Note	Found 1 referenced keys
test.xt2	check	Note	Found 1 foreign keys
test.xt2	check	status	OK
test.xt3	check	Note	Found 2 foreign keys
test.xt3	check	status	OK
flush tables xt1, xt2, xt3;
check tables xt1, xt2, xt3;
Table	Op	Msg_type	Msg_text
test.xt1	check	Note	Found 2 referenced keys
test.xt1	check	status	OK
test.xt2	check	Note	Found 1 referenced keys
test.xt2	check	Note	Found 1 foreign keys
test.xt2	check	status	OK
test.xt3	check	Note	Found 2 foreign keys
test.xt3	check	status	OK
rename table xt3 to t3, xt2 to t2, xt1 to t1;
check tables t1, t2, t3;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 2 referenced keys
test.t1	check	status	OK
test.t2	check	Note	Found 1 referenced keys
test.t2	check	Note	Found 1 foreign keys
test.t2	check	status	OK
test.t3	check	Note	Found 2 foreign keys
test.t3	check	status	OK
flush tables t1, t2, t3;
check tables t1, t2, t3;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 2 referenced keys
test.t1	check	status	OK
test.t2	check	Note	Found 1 referenced keys
test.t2	check	Note	Found 1 foreign keys
test.t2	check	status	OK
test.t3	check	Note	Found 2 foreign keys
test.t3	check	status	OK
drop tables fail_t4, t4;
rename table t3 to t4, t2 to t3, t1 to t2, t4 to t1;
check tables t1, t2, t3;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 2 foreign keys
test.t1	check	status	OK
test.t2	check	Note	Found 2 referenced keys
test.t2	check	status	OK
test.t3	check	Note	Found 1 referenced keys
test.t3	check	Note	Found 1 foreign keys
test.t3	check	status	OK
flush tables t1, t2, t3;
check tables t1, t2, t3;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 2 foreign keys
test.t1	check	status	OK
test.t2	check	Note	Found 2 referenced keys
test.t2	check	status	OK
test.t3	check	Note	Found 1 referenced keys
test.t3	check	Note	Found 1 foreign keys
test.t3	check	status	OK
drop tables t1, t3, t2;
# Check drop table
create or replace table t1 (id int primary key);
create or replace table ch1 (id int, foreign key (id) references t1 (id));
select * from t1;
id
select * from ch1;
id
drop table t1;
ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (fk_ch1)
drop tables ch1, t1;
# Check drop database
create or replace table t1 (id int primary key);
select * from t1;
id
create or replace database test2;
use test2;
create or replace table ch1 (
id int, id2 int,
foreign key (id) references test.t1 (id));
select * from ch1;
id	id2
use test;
drop database test2;
check tables t1;
Table	Op	Msg_type	Msg_text
test.t1	check	status	OK
flush tables t1;
check tables t1;
Table	Op	Msg_type	Msg_text
test.t1	check	status	OK
drop tables t1;
# Check add foreign key
create or replace table t1(fld1 int not null primary key);
create or replace table t2(fld1 int not null, fld2 int as (fld1) virtual);
insert into t1 values(1);
insert into t2 values(1, default);
set foreign_key_checks= 0;
alter table t2 add index(fld2), add foreign key (fld1) references t1(fld1)
on update cascade, algorithm=inplace;
set foreign_key_checks= 1;
update t1 set fld1= 2;
select fld2 from t2;
fld2
2
select * from t2;
fld1	fld2
2	2
drop table t2, t1;
# Check drop column, drop foreign key
create or replace table t1 (id int primary key, a int);
create or replace table t2 (id int, a int, foreign key fk (id) references t1 (id));
alter table t1 drop id;
ERROR HY000: Cannot drop column 'id': needed in a foreign key constraint 'fk' of table `test`.`t2`
alter table t2 drop id;
ERROR 42000: Incorrect foreign key definition for 'id': foreign field not found
alter table t2 drop foreign key fk;
drop tables t1, t2;
# Check self-references
create or replace table t1 (f1 integer primary key references t1(f1));
ERROR 42000: Incorrect foreign key definition for 'f1': foreign key references itself
create or replace table t1 (f1 integer primary key, foreign key (f1) references t1(f1));
ERROR 42000: Incorrect foreign key definition for 'f1': foreign key references itself
create or replace table t1 (f1 integer primary key);
alter table t1 add constraint c1 foreign key (f1) references t1(f1);
ERROR 42000: Incorrect foreign key definition for 'f1': foreign key references itself
create or replace table t1 (id int primary key, id2 int references t1 (id));
check tables t1;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 1 self-references
test.t1	check	status	OK
flush tables t1;
check tables t1;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 1 self-references
test.t1	check	status	OK
show create table t1;
Table	Create Table
t1	CREATE TABLE `t1` (
  `id` int(11) NOT NULL,
  `id2` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_t1` (`id2`),
  CONSTRAINT `fk_t1` FOREIGN KEY (`id2`) REFERENCES `t1` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
insert into t1 values (1, 2);
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`t1`, CONSTRAINT `fk_t1` FOREIGN KEY (`id2`) REFERENCES `t1` (`id`))
insert into t1 values (1, 1);
alter table t1 change id2 id3 int;
check tables t1;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 1 self-references
test.t1	check	status	OK
flush tables t1;
check tables t1;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 1 self-references
test.t1	check	status	OK
show create table t1;
Table	Create Table
t1	CREATE TABLE `t1` (
  `id` int(11) NOT NULL,
  `id3` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_t1` (`id3`),
  CONSTRAINT `fk_t1` FOREIGN KEY (`id3`) REFERENCES `t1` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
alter table t1 add foreign key (id3) references t1 (id);
check tables t1;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 2 self-references
test.t1	check	status	OK
flush tables t1;
check tables t1;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 2 self-references
test.t1	check	status	OK
show create table t1;
Table	Create Table
t1	CREATE TABLE `t1` (
  `id` int(11) NOT NULL,
  `id3` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_t1_2` (`id3`),
  CONSTRAINT `fk_t1` FOREIGN KEY (`id3`) REFERENCES `t1` (`id`),
  CONSTRAINT `fk_t1_2` FOREIGN KEY (`id3`) REFERENCES `t1` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
alter table t1 change id id4 int;
show create table t1;
Table	Create Table
t1	CREATE TABLE `t1` (
  `id4` int(11) NOT NULL,
  `id3` int(11) DEFAULT NULL,
  PRIMARY KEY (`id4`),
  KEY `fk_t1_2` (`id3`),
  CONSTRAINT `fk_t1` FOREIGN KEY (`id3`) REFERENCES `t1` (`id4`),
  CONSTRAINT `fk_t1_2` FOREIGN KEY (`id3`) REFERENCES `t1` (`id4`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
check tables t1;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 2 self-references
test.t1	check	status	OK
flush tables t1;
check tables t1;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 2 self-references
test.t1	check	status	OK
show create table t1;
Table	Create Table
t1	CREATE TABLE `t1` (
  `id4` int(11) NOT NULL,
  `id3` int(11) DEFAULT NULL,
  PRIMARY KEY (`id4`),
  KEY `fk_t1_2` (`id3`),
  CONSTRAINT `fk_t1` FOREIGN KEY (`id3`) REFERENCES `t1` (`id4`),
  CONSTRAINT `fk_t1_2` FOREIGN KEY (`id3`) REFERENCES `t1` (`id4`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
alter table t1 drop foreign key fk_t1;
check tables t1;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 1 self-references
test.t1	check	status	OK
flush tables t1;
check tables t1;
Table	Op	Msg_type	Msg_text
test.t1	check	Note	Found 1 self-references
test.t1	check	status	OK
show create table t1;
Table	Create Table
t1	CREATE TABLE `t1` (
  `id4` int(11) NOT NULL,
  `id3` int(11) DEFAULT NULL,
  PRIMARY KEY (`id4`),
  KEY `fk_t1_2` (`id3`),
  CONSTRAINT `fk_t1_2` FOREIGN KEY (`id3`) REFERENCES `t1` (`id4`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
rename table t1 to t2;
check tables t2;
Table	Op	Msg_type	Msg_text
test.t2	check	Note	Found 1 self-references
test.t2	check	status	OK
flush tables t2;
check tables t2;
Table	Op	Msg_type	Msg_text
test.t2	check	Note	Found 1 self-references
test.t2	check	status	OK
show create table t2;
Table	Create Table
t2	CREATE TABLE `t2` (
  `id4` int(11) NOT NULL,
  `id3` int(11) DEFAULT NULL,
  PRIMARY KEY (`id4`),
  KEY `fk_t1_2` (`id3`),
  CONSTRAINT `fk_t1_2` FOREIGN KEY (`id3`) REFERENCES `t2` (`id4`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
delete from t2;
ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`t2`, CONSTRAINT `fk_t1_2` FOREIGN KEY (`id3`) REFERENCES `t2` (`id4`))
drop table t2;
set default_storage_engine= default;
# Prohibit wrong references and fix field name case
create table t1 (A int unique key, x timestamp references t1(a));
ERROR 42000: Incorrect foreign key definition for 'x': foreign-referenced fields type mismatch
create table t1 (A int unique key, x int references t1(b));
ERROR 42000: Incorrect foreign key definition for 'b': referenced field not found
create table t1 (A int unique key, x int references t1(a));
alter table t1 add foreign key(x) references t1(b);
ERROR 42000: Incorrect foreign key definition for 'b': referenced field not found
alter table t1 add foreign key(x) references t1(a);
show create table t1;
Table	Create Table
t1	CREATE TABLE `t1` (
  `A` int(11) DEFAULT NULL,
  `x` int(11) DEFAULT NULL,
  UNIQUE KEY `A` (`A`),
  KEY `fk_t1_2` (`x`),
  CONSTRAINT `fk_t1` FOREIGN KEY (`x`) REFERENCES `t1` (`A`),
  CONSTRAINT `fk_t1_2` FOREIGN KEY (`x`) REFERENCES `t1` (`A`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
create table t2 (AA int, x int references t1(ax));
ERROR 42000: Incorrect foreign key definition for 'ax': referenced field not found
create table t2 (AA int, x timestamp references t1(a));
ERROR 42000: Incorrect foreign key definition for 'x': foreign-referenced fields type mismatch
create table t2 (AA int, x int references t1(a));
alter table t2 add foreign key(aa) references t1(b);
ERROR 42000: Incorrect foreign key definition for 'b': referenced field not found
alter table t2 add foreign key(aa) references t1(a);
show create table t2;
Table	Create Table
t2	CREATE TABLE `t2` (
  `AA` int(11) DEFAULT NULL,
  `x` int(11) DEFAULT NULL,
  KEY `fk_t2` (`x`),
  KEY `fk_t2_2` (`AA`),
  CONSTRAINT `fk_t2` FOREIGN KEY (`x`) REFERENCES `t1` (`A`),
  CONSTRAINT `fk_t2_2` FOREIGN KEY (`AA`) REFERENCES `t1` (`A`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop tables t2, t1;
# Foreign keys and partitioning
create table t1 (x int primary key) partition by hash(x) partitions 3;
create table t2 (y int references t1(x));
ERROR HY000: Partitioned tables do not support FOREIGN KEY
create table t2 (y int);
alter table t2 add foreign key (y) references t1 (x);
ERROR HY000: Partitioned tables do not support FOREIGN KEY
drop tables t2, t1;
create table t1 (x int primary key);
create table t2 (y int references t1(x)) partition by hash(x) partitions 3;
ERROR HY000: Partitioned tables do not support FOREIGN KEY
create table t2 (y int) partition by hash(y) partitions 3;
alter table t2 add foreign key (y) references t1 (x);
ERROR HY000: Partitioned tables do not support FOREIGN KEY
create or replace table t2 (y int references t1(x));
alter table t1 partition by hash(x) partitions 3;
ERROR HY000: Partitioned tables do not support FOREIGN KEY
alter table t2 partition by hash(y) partitions 3;
ERROR HY000: Partitioned tables do not support FOREIGN KEY
drop tables t2, t1;