File: foreign_key_debug.result

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (806 lines) | stat: -rw-r--r-- 33,155 bytes parent folder | download
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
SET @saved_binlog_format= @@SESSION.binlog_format;
Warnings:
Warning	1287	'@@binlog_format' is deprecated and will be removed in a future release.
SET SESSION debug= '+d,skip_dd_table_access_check';
SET @@foreign_key_checks= DEFAULT;
#
# WL#6929: Move FOREIGN KEY constraints to the global data dictionary
#
# Test coverage for foreign key name generation. Should be removed
# or rewritten when WL#7141 is pushed.
CREATE TABLE t1(a INT PRIMARY KEY);
CREATE TABLE t2(a INT PRIMARY KEY);
CREATE TABLE t3(a INT PRIMARY KEY, b INT, c INT);
ALTER TABLE t3 ADD FOREIGN KEY (b) REFERENCES t1(a);
SHOW CREATE TABLE t3;
Table	Create Table
t3	CREATE TABLE `t3` (
  `a` int NOT NULL,
  `b` int DEFAULT NULL,
  `c` int DEFAULT NULL,
  PRIMARY KEY (`a`),
  KEY `b` (`b`),
  CONSTRAINT `t3_ibfk_1` FOREIGN KEY (`b`) REFERENCES `t1` (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SELECT fk.name FROM mysql.foreign_keys AS fk, mysql.tables AS t
WHERE fk.table_id = t.id AND t.name = 't3';
name
t3_ibfk_1
ALTER TABLE t3 ADD FOREIGN KEY (c) REFERENCES t1(a);
SHOW CREATE TABLE t3;
Table	Create Table
t3	CREATE TABLE `t3` (
  `a` int NOT NULL,
  `b` int DEFAULT NULL,
  `c` int DEFAULT NULL,
  PRIMARY KEY (`a`),
  KEY `b` (`b`),
  KEY `c` (`c`),
  CONSTRAINT `t3_ibfk_1` FOREIGN KEY (`b`) REFERENCES `t1` (`a`),
  CONSTRAINT `t3_ibfk_2` FOREIGN KEY (`c`) REFERENCES `t1` (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SELECT fk.name FROM mysql.foreign_keys AS fk, mysql.tables AS t
WHERE fk.table_id = t.id AND t.name = 't3';
name
t3_ibfk_1
t3_ibfk_2
ALTER TABLE t3 ADD FOREIGN KEY (b) REFERENCES t1(a);
SHOW CREATE TABLE t3;
Table	Create Table
t3	CREATE TABLE `t3` (
  `a` int NOT NULL,
  `b` int DEFAULT NULL,
  `c` int DEFAULT NULL,
  PRIMARY KEY (`a`),
  KEY `c` (`c`),
  KEY `b` (`b`),
  CONSTRAINT `t3_ibfk_1` FOREIGN KEY (`b`) REFERENCES `t1` (`a`),
  CONSTRAINT `t3_ibfk_2` FOREIGN KEY (`c`) REFERENCES `t1` (`a`),
  CONSTRAINT `t3_ibfk_3` FOREIGN KEY (`b`) REFERENCES `t1` (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SELECT fk.name FROM mysql.foreign_keys AS fk, mysql.tables AS t
WHERE fk.table_id = t.id AND t.name = 't3';
name
t3_ibfk_1
t3_ibfk_2
t3_ibfk_3
ALTER TABLE t3 DROP FOREIGN KEY t3_ibfk_1;
SHOW CREATE TABLE t3;
Table	Create Table
t3	CREATE TABLE `t3` (
  `a` int NOT NULL,
  `b` int DEFAULT NULL,
  `c` int DEFAULT NULL,
  PRIMARY KEY (`a`),
  KEY `c` (`c`),
  KEY `b` (`b`),
  CONSTRAINT `t3_ibfk_2` FOREIGN KEY (`c`) REFERENCES `t1` (`a`),
  CONSTRAINT `t3_ibfk_3` FOREIGN KEY (`b`) REFERENCES `t1` (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SELECT fk.name FROM mysql.foreign_keys AS fk, mysql.tables AS t
WHERE fk.table_id = t.id AND t.name = 't3';
name
t3_ibfk_2
t3_ibfk_3
ALTER TABLE t3 ADD FOREIGN KEY (b) REFERENCES t1(a);
SHOW CREATE TABLE t3;
Table	Create Table
t3	CREATE TABLE `t3` (
  `a` int NOT NULL,
  `b` int DEFAULT NULL,
  `c` int DEFAULT NULL,
  PRIMARY KEY (`a`),
  KEY `c` (`c`),
  KEY `b` (`b`),
  CONSTRAINT `t3_ibfk_2` FOREIGN KEY (`c`) REFERENCES `t1` (`a`),
  CONSTRAINT `t3_ibfk_3` FOREIGN KEY (`b`) REFERENCES `t1` (`a`),
  CONSTRAINT `t3_ibfk_4` FOREIGN KEY (`b`) REFERENCES `t1` (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
SELECT fk.name FROM mysql.foreign_keys AS fk, mysql.tables AS t
WHERE fk.table_id = t.id AND t.name = 't3';
name
t3_ibfk_2
t3_ibfk_3
t3_ibfk_4
DROP TABLE t3, t2, t1;
CREATE TABLE t1(a INT PRIMARY KEY);
CREATE TABLE name567890123456789012345678901234567890123456789012345678901234(a INT PRIMARY KEY, b INT);
ALTER TABLE name567890123456789012345678901234567890123456789012345678901234
ADD FOREIGN KEY(b) REFERENCES t1(a);
ERROR 42000: Identifier name 'name567890123456789012345678901234567890123456789012345678901234_ibfk_1' is too long
DROP TABLE name567890123456789012345678901234567890123456789012345678901234, t1;
#
# WL#6049: Meta data locking for foreign keys.
#
#
# Normal CT will set the FK unique constraint name.
#
CREATE TABLE parent(pk INTEGER PRIMARY KEY, j INTEGER,
UNIQUE KEY my_key (j));
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk INTEGER,
FOREIGN KEY (fk) REFERENCES parent(j));
SELECT unique_constraint_name FROM mysql.foreign_keys
WHERE referenced_table_name LIKE 'parent';
unique_constraint_name
my_key
DROP TABLES child, parent;
#
# CT introducing a missing parent will update the FK unique constraint name in the child.
#
SET @@foreign_key_checks= 0;
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk INTEGER,
FOREIGN KEY (fk) REFERENCES parent(j));
# An index is created for the FK, but the unique constraint name is NULL.
SELECT name FROM mysql.indexes
WHERE table_id = (SELECT id from mysql.tables WHERE name LIKE 'child');
name
PRIMARY
fk
SELECT unique_constraint_name FROM mysql.foreign_keys
WHERE referenced_table_name LIKE 'parent';
unique_constraint_name
NULL
CREATE TABLE parent(pk INTEGER PRIMARY KEY, j INTEGER,
UNIQUE KEY my_key (j));
SET @@foreign_key_checks= 1;
# After creating the parent, the unique constraint name is updated.
SELECT unique_constraint_name FROM mysql.foreign_keys
WHERE referenced_table_name LIKE 'parent';
unique_constraint_name
my_key
DROP TABLES child, parent;
#
# CTL does not copy FKs from the source table.
#
CREATE TABLE parent(pk INTEGER PRIMARY KEY, j INTEGER,
UNIQUE KEY my_key (j));
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk INTEGER,
FOREIGN KEY (fk) REFERENCES parent(j));
SELECT unique_constraint_name FROM mysql.foreign_keys
WHERE referenced_table_name LIKE 'parent';
unique_constraint_name
my_key
CREATE TABLE child_copy LIKE child;
## The index is re-created for the new table.
SELECT name FROM mysql.indexes
WHERE table_id = (SELECT id from mysql.tables WHERE name LIKE 'child');
name
PRIMARY
fk
SELECT name FROM mysql.indexes
WHERE table_id = (SELECT id from mysql.tables WHERE name LIKE 'child_copy');
name
PRIMARY
fk
## ... but not the constraint.
SELECT unique_constraint_name FROM mysql.foreign_keys
WHERE referenced_table_name LIKE 'parent';
unique_constraint_name
my_key
DROP TABLES child, child_copy, parent;
#
# CTL introducing a missing parent will update the FK in the child.
#
SET @@foreign_key_checks= 0;
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk INTEGER,
FOREIGN KEY (fk) REFERENCES parent(j));
## An index is created for the FK, but the unique constraint name is NULL.
SELECT name FROM mysql.indexes
WHERE table_id = (SELECT id from mysql.tables WHERE name LIKE 'parent');
name
SELECT unique_constraint_name FROM mysql.foreign_keys
WHERE referenced_table_name LIKE 'parent';
unique_constraint_name
NULL
CREATE TABLE parent_base(pk INTEGER PRIMARY KEY, j INTEGER,
UNIQUE KEY my_key (j));
CREATE TABLE parent LIKE parent_base;
SET @@foreign_key_checks= 1;
## After creating the parent, the unique constraint name is updated.
SELECT unique_constraint_name FROM mysql.foreign_keys
WHERE referenced_table_name LIKE 'parent';
unique_constraint_name
my_key
DROP TABLE child, parent_base, parent;
#
# CTS will update the unique constraint name in its FK info.
#
CREATE TABLE source(pk INTEGER PRIMARY KEY, j INTEGER);
INSERT INTO source VALUES (1, 1);
CREATE TABLE parent(pk INTEGER PRIMARY KEY, j INTEGER,
UNIQUE KEY my_key(j));
INSERT INTO parent VALUES (2, 1);
SET @@SESSION.binlog_format=STATEMENT;
Warnings:
Warning	1287	'@@binlog_format' is deprecated and will be removed in a future release.
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk INTEGER,
FOREIGN KEY (fk) REFERENCES parent(j)) AS SELECT pk, j AS fk FROM source;
SET SESSION binlog_format= @saved_binlog_format;
Warnings:
Warning	1287	'@@binlog_format' is deprecated and will be removed in a future release.
SELECT * FROM child;
pk	fk
1	1
## An index is created for the FK, and the unique constraint name is updated.
SELECT name FROM mysql.indexes
WHERE table_id = (SELECT id from mysql.tables WHERE name LIKE 'child');
name
PRIMARY
fk
SELECT unique_constraint_name FROM mysql.foreign_keys
WHERE referenced_table_name LIKE 'parent';
unique_constraint_name
my_key
DROP TABLES source, child, parent;
#
# CTS introducing a missing parent will update the FK in the child.
#
SET @@foreign_key_checks= 0;
CREATE TABLE source(pk INTEGER PRIMARY KEY, j INTEGER);
INSERT INTO source VALUES (1, 1);
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk INTEGER,
FOREIGN KEY (fk) REFERENCES parent(j));
## An index is created for the FK, but the unique constraint name is NULL.
SELECT name FROM mysql.indexes
WHERE table_id = (SELECT id from mysql.tables WHERE name LIKE 'child');
name
PRIMARY
fk
SELECT unique_constraint_name FROM mysql.foreign_keys
WHERE referenced_table_name LIKE 'parent';
unique_constraint_name
NULL
SET @@SESSION.binlog_format=STATEMENT;
Warnings:
Warning	1287	'@@binlog_format' is deprecated and will be removed in a future release.
CREATE TABLE parent(pk INTEGER PRIMARY KEY, j INTEGER,
UNIQUE KEY my_key(j)) AS SELECT * FROM source;
SET SESSION binlog_format= @saved_binlog_format;
Warnings:
Warning	1287	'@@binlog_format' is deprecated and will be removed in a future release.
SELECT * FROM child;
pk	fk
SET @@foreign_key_checks= 1;
## After creating the parent, the unique constraint name is updated.
SELECT unique_constraint_name FROM mysql.foreign_keys
WHERE referenced_table_name LIKE 'parent';
unique_constraint_name
my_key
DROP TABLES source, child, parent;
#
# RENAME will update FK information in both children and parents.
#
CREATE TABLE parent(pk INTEGER PRIMARY KEY, j INTEGER,
UNIQUE KEY parent_key(j));
CREATE TABLE child(pk INTEGER PRIMARY KEY, k INTEGER, fk INTEGER,
FOREIGN KEY (fk) REFERENCES parent(j), UNIQUE KEY child_key(k));
# Constraint is enforced.
INSERT INTO child VALUES (1, 2, 3);
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`child`, CONSTRAINT `child_ibfk_1` FOREIGN KEY (`fk`) REFERENCES `parent` (`j`))
CREATE TABLE grandchild(pk INTEGER PRIMARY KEY, fk INTEGER,
FOREIGN KEY (fk) REFERENCES child(k));
# Constraint is enforced.
INSERT INTO grandchild VALUES (1, 2);
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`grandchild`, CONSTRAINT `grandchild_ibfk_1` FOREIGN KEY (`fk`) REFERENCES `child` (`k`))
SET @@foreign_key_checks= 0;
CREATE TABLE orphan_grandchild(pk INTEGER PRIMARY KEY, fk INTEGER,
FOREIGN KEY (fk) REFERENCES siebling(k));
SET @@foreign_key_checks= 1;
# FK definitions before rename:
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
child_ibfk_1	parent_key	test	parent
grandchild_ibfk_1	child_key	test	child
orphan_grandchild_ibfk_1	NULL	test	siebling
RENAME TABLE child TO siebling;
# After the rename, we see that:
# 1. The name of the constraint is changed to 'siebling_ibfk...'.
# 2. The referenced table name of the grandchild is changed to 'siebling'.
# 3. The unique constraint name of the orphan_grandchild is corrected.
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
grandchild_ibfk_1	child_key	test	siebling
orphan_grandchild_ibfk_1	child_key	test	siebling
siebling_ibfk_1	parent_key	test	parent
# Constraint is still enforced.
INSERT INTO siebling VALUES (1, 2, 3);
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`siebling`, CONSTRAINT `siebling_ibfk_1` FOREIGN KEY (`fk`) REFERENCES `parent` (`j`))
# Constraint is still enforced.
INSERT INTO grandchild VALUES (1, 2);
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`grandchild`, CONSTRAINT `grandchild_ibfk_1` FOREIGN KEY (`fk`) REFERENCES `siebling` (`k`))
# Constraint is enforced here too.
INSERT INTO orphan_grandchild VALUES (1, 2);
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (`test`.`orphan_grandchild`, CONSTRAINT `orphan_grandchild_ibfk_1` FOREIGN KEY (`fk`) REFERENCES `siebling` (`k`))
DROP TABLE grandchild;
DROP TABLE orphan_grandchild;
DROP TABLE siebling;
DROP TABLE parent;
# When processing LOCK TABLES, we will prelock even
# when F_K_C = 0.
#
CREATE TABLE parent(pk INTEGER PRIMARY KEY, j INTEGER,
UNIQUE KEY parent_key(j));
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk INTEGER,
FOREIGN KEY (fk) REFERENCES parent(j) ON DELETE CASCADE);
SET @@foreign_key_checks= 0;
LOCK TABLES parent WRITE;
# There are two metadata locks because the child has
# two different FK roles wrt. the parent. Note that
# the locks are stronger for LOCK TABLES.
SELECT OBJECT_TYPE, OBJECT_SCHEMA, OBJECT_NAME, COLUMN_NAME,
LOCK_TYPE FROM performance_schema.metadata_locks
WHERE OBJECT_NAME LIKE 'child'
  ORDER BY OBJECT_TYPE, OBJECT_SCHEMA, OBJECT_NAME, COLUMN_NAME, LOCK_TYPE;
OBJECT_TYPE	OBJECT_SCHEMA	OBJECT_NAME	COLUMN_NAME	LOCK_TYPE
COLUMN STATISTICS	test	child	fk	SHARED_READ
COLUMN STATISTICS	test	child	pk	SHARED_READ
TABLE	test	child	NULL	SHARED_NO_READ_WRITE
TABLE	test	child	NULL	SHARED_READ_ONLY
# From another connection, verify that child is locked.
SET @@session.lock_wait_timeout= 1;
INSERT INTO child VALUES (1, 1);
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
UNLOCK TABLES;
SET @@foreign_key_checks= 1;
# Now, the locks are gone.
SELECT LOCK_TYPE FROM performance_schema.metadata_locks
WHERE OBJECT_NAME LIKE 'child';
LOCK_TYPE
DROP TABLE child;
DROP TABLE parent;
#
# A prepared statement will become invalid if a child table
# is modified between executions of the prepared statement.
#
CREATE TABLE parent(pk INTEGER PRIMARY KEY, j INTEGER,
UNIQUE KEY parent_key(j));
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk INTEGER,
FOREIGN KEY (fk) REFERENCES parent(j) ON DELETE CASCADE);
PREPARE stmt FROM 'DELETE FROM parent WHERE pk = ?';
SELECT COUNT_REPREPARE, COUNT_EXECUTE
FROM performance_schema.prepared_statements_instances
WHERE STATEMENT_NAME LIKE 'stmt';
COUNT_REPREPARE	COUNT_EXECUTE
0	0
SET @a= 1;
EXECUTE stmt USING @a;
# No reprepare for first execution.
SELECT COUNT_REPREPARE, COUNT_EXECUTE
FROM performance_schema.prepared_statements_instances
WHERE STATEMENT_NAME LIKE 'stmt';
COUNT_REPREPARE	COUNT_EXECUTE
0	1
# Altering child will trigger reprepare on next execution.
ALTER TABLE child ADD COLUMN (j INTEGER);
EXECUTE stmt USING @a;
# Statement has been reprepared for second execution.
SELECT COUNT_REPREPARE, COUNT_EXECUTE
FROM performance_schema.prepared_statements_instances
WHERE STATEMENT_NAME LIKE 'stmt';
COUNT_REPREPARE	COUNT_EXECUTE
1	2
EXECUTE stmt USING @a;
# Cache version for the prelock entry is updated, so no
# reprepare for third execution.
SELECT COUNT_REPREPARE, COUNT_EXECUTE
FROM performance_schema.prepared_statements_instances
WHERE STATEMENT_NAME LIKE 'stmt';
COUNT_REPREPARE	COUNT_EXECUTE
1	3
DROP TABLE child;
DROP TABLE parent;
#
# Check situations where there are multiple foreign keys
# referring the same table.
#
CREATE TABLE parent(pk INTEGER PRIMARY KEY, i INTEGER, j INTEGER,
UNIQUE KEY parent_i_key(i), UNIQUE KEY parent_j_key(j));
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk_i INTEGER, fk_j INTEGER,
FOREIGN KEY (fk_i) REFERENCES parent(i),
FOREIGN KEY (fk_j) REFERENCES parent(j));
ALTER TABLE child RENAME TO siebling;
DROP TABLES siebling, parent;
#
# Rename a table multiple times in the same statement.
#
CREATE TABLE parent(pk INTEGER PRIMARY KEY, i INTEGER,
UNIQUE KEY parent_key(i));
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk_i INTEGER,
FOREIGN KEY (fk_i) REFERENCES parent(i));
RENAME TABLE parent TO mother, mother TO father;
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
child_ibfk_1	parent_key	test	father
RENAME TABLE child TO sister, sister TO brother;
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
brother_ibfk_1	parent_key	test	father
RENAME TABLE father TO mother, brother TO sister, mother TO parent, sister TO child;
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
child_ibfk_1	parent_key	test	parent
DROP TABLES child, parent;
#
# Let a RENAME statement introduce a missing parent and rename
# it further as well.
#
SET @@foreign_key_checks= 0;
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk_i INTEGER,
FOREIGN KEY (fk_i) REFERENCES mother(i));
SET @@foreign_key_checks= 1;
CREATE TABLE parent(pk INTEGER PRIMARY KEY, i INTEGER,
UNIQUE KEY parent_key(i));
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
child_ibfk_1	NULL	test	mother
RENAME TABLE parent TO mother, mother TO father;
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
child_ibfk_1	parent_key	test	father
DROP TABLES child, father;
#
# Rename a parent with a child having several FKs to it.
#
CREATE TABLE parent(pk INTEGER PRIMARY KEY, i INTEGER, j INTEGER,
UNIQUE KEY parent_i_key(i),
UNIQUE KEY parent_j_key(j));
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk_i INTEGER, fk_j INTEGER,
FOREIGN KEY (fk_i) REFERENCES parent(i),
FOREIGN KEY (fk_j) REFERENCES parent(j));
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
child_ibfk_1	parent_i_key	test	parent
child_ibfk_2	parent_j_key	test	parent
RENAME TABLE parent TO mother;
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
child_ibfk_1	parent_i_key	test	mother
child_ibfk_2	parent_j_key	test	mother
DROP TABLES child, mother;
#
# ALTER TABLE RENAME and complex ALTER TABLE RENAME involving
# self-referncing foreign key.
#
CREATE TABLE self (pk INT PRIMARY KEY, fk INT, FOREIGN KEY(fk) REFERENCES self(pk));
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
self_ibfk_1	PRIMARY	test	self
ALTER TABLE self RENAME TO self2;
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
self2_ibfk_1	PRIMARY	test	self2
ALTER TABLE self2 RENAME TO self3, ADD COLUMN i INT;
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
self3_ibfk_1	PRIMARY	test	self3
DROP TABLE self3;
#
# Drop a schema with tables referencing/being referenced by tables
# in a different schema.
#
CREATE TABLE parent(pk INTEGER PRIMARY KEY, i INTEGER,
UNIQUE KEY parent_i_key(i));
SET @@foreign_key_checks= 0;
CREATE TABLE grandchild(pk INTEGER PRIMARY KEY, fk_i INTEGER,
FOREIGN KEY (fk_i) REFERENCES s1.child(i));
SET @@foreign_key_checks= 1;
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 's1';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
grandchild_ibfk_1	NULL	s1	child
CREATE SCHEMA s1;
CREATE TABLE s1.child(pk INTEGER PRIMARY KEY, i INTEGER, fk_i INTEGER,
UNIQUE KEY child_i_key(i),
FOREIGN KEY (fk_i) REFERENCES test.parent(i));
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
child_ibfk_1	parent_i_key	test	parent
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 's1';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
grandchild_ibfk_1	child_i_key	s1	child
DROP SCHEMA s1;
ERROR HY000: Cannot drop table 'child' referenced by a foreign key constraint 'grandchild_ibfk_1' on table 'grandchild'.
SET @@foreign_key_checks= 0;
DROP SCHEMA s1;
SET @@foreign_key_checks= 1;
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 's1';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
grandchild_ibfk_1	NULL	s1	child
# Skip FK to parent.
CREATE SCHEMA s1;
CREATE TABLE s1.child(pk INTEGER PRIMARY KEY, i INTEGER,
UNIQUE KEY child_i_key(i));
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 's1';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
grandchild_ibfk_1	child_i_key	s1	child
DROP SCHEMA s1;
ERROR HY000: Cannot drop table 'child' referenced by a foreign key constraint 'grandchild_ibfk_1' on table 'grandchild'.
SET @@foreign_key_checks= 0;
DROP SCHEMA s1;
SET @@foreign_key_checks= 1;
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 's1';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
grandchild_ibfk_1	NULL	s1	child
DROP TABLE grandchild;
# No FK from grandchild.
CREATE SCHEMA s1;
CREATE TABLE s1.child(pk INTEGER PRIMARY KEY, fk_i INTEGER);
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
# Introduce FK to parent.
ALTER TABLE s1.child ADD FOREIGN KEY (fk_i) REFERENCES test.parent(i);
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
child_ibfk_1	parent_i_key	test	parent
DROP SCHEMA s1;
DROP TABLE parent;
#
# Trigger an error in Foreign_key_parents_invalidator::invalidate().
#
CREATE TABLE parent(pk INTEGER PRIMARY KEY, i INTEGER,
UNIQUE KEY parent_key(i));
CREATE TABLE child(pk INTEGER PRIMARY KEY, fk_i INTEGER,
FOREIGN KEY (fk_i) REFERENCES parent(i));
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
child_ibfk_1	parent_key	test	parent
SET @@session.debug= '+d,fail_while_invalidating_fk_parents';
ALTER TABLE child RENAME TO siebling;
SET @@session.debug= '-d,fail_while_invalidating_fk_parents';
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
siebling_ibfk_1	parent_key	test	parent
DROP TABLE siebling, parent;
#
# Coverage for various corner cases when figuring out unique_constraint_name.
#
CREATE TABLE parent (i INT, j INT, PRIMARY KEY (i), UNIQUE u(i,j));
CREATE TABLE child (i INT, j INT, FOREIGN KEY (i, j) REFERENCES parent (i, j));
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
child_ibfk_1	u	test	parent
ALTER TABLE parent RENAME KEY u TO u1;
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
child_ibfk_1	u1	test	parent
DROP TABLE child, parent;
CREATE TABLE parent (i INT, j INT, k INT, PRIMARY KEY (i), UNIQUE u(j), UNIQUE u1(i,j), UNIQUE u2(i,j,k));
CREATE TABLE child (i INT, j INT, k INT, FOREIGN KEY (i, j, k) REFERENCES parent (i, j, k));
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
child_ibfk_1	u2	test	parent
ALTER TABLE parent RENAME KEY u2 TO u3;
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
child_ibfk_1	u3	test	parent
DROP TABLE child, parent;
CREATE TABLE parent (i INT, j INT,
d INT GENERATED ALWAYS AS (i) VIRTUAL,
e INT GENERATED ALWAYS AS (j) VIRTUAL,
PRIMARY KEY (i), UNIQUE u(i,d), UNIQUE u1(i,j,e));
CREATE TABLE child (i INT, j INT, FOREIGN KEY (i, j) REFERENCES parent (i, j));
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
child_ibfk_1	u1	test	parent
ALTER TABLE parent RENAME KEY u1 TO u2;
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
child_ibfk_1	u2	test	parent
DROP TABLE child, parent;
CREATE TABLE parent (i INT, a VARCHAR(10), b VARCHAR(10),
PRIMARY KEY (i), UNIQUE u(i,a(5)), UNIQUE u1(i,a,b(5)));
CREATE TABLE child (i INT, a VARCHAR(10), FOREIGN KEY (i, a) REFERENCES parent (i, a));
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
child_ibfk_1	u1	test	parent
ALTER TABLE parent RENAME KEY u1 TO u2;
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
child_ibfk_1	u2	test	parent
DROP TABLE child, parent;
CREATE TABLE self (i INT, j INT, i2 INT, j2 INT, PRIMARY KEY (i), UNIQUE u(i,j),
FOREIGN KEY (i2, j2) REFERENCES self (i, j));
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
self_ibfk_1	u	test	self
ALTER TABLE self RENAME KEY u TO u1;
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
self_ibfk_1	u1	test	self
DROP TABLE self;
CREATE TABLE self (i INT, j INT, k INT, i2 INT, j2 INT, k2 INT,
PRIMARY KEY (i), UNIQUE u(j), UNIQUE u1(i,j), UNIQUE u2(i,j,k),
FOREIGN KEY (i2, j2, k2) REFERENCES self (i, j, k));
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
self_ibfk_1	u2	test	self
ALTER TABLE self RENAME KEY u2 TO u3;
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
self_ibfk_1	u3	test	self
DROP TABLE self;
CREATE TABLE self (i INT, j INT, i2 INT, j2 INT,
d INT GENERATED ALWAYS AS (i) VIRTUAL,
e INT GENERATED ALWAYS AS (j) VIRTUAL,
PRIMARY KEY (i), UNIQUE u(i,d), UNIQUE u1(i,j,e),
FOREIGN KEY (i2, j2) REFERENCES self (i, j));
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
self_ibfk_1	u1	test	self
ALTER TABLE self RENAME KEY u1 TO u2;
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
self_ibfk_1	u2	test	self
DROP TABLE self;
CREATE TABLE self (i INT, a VARCHAR(10), b VARCHAR(10), i2 INT, a2 VARCHAR(10),
PRIMARY KEY (i), UNIQUE u(i,a(5)), UNIQUE u1(i,a,b(5)),
FOREIGN KEY (i2, a2) REFERENCES self (i, a));
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
self_ibfk_1	u1	test	self
ALTER TABLE self RENAME KEY u1 TO u2;
SELECT name, unique_constraint_name, referenced_table_schema, referenced_table_name
FROM mysql.foreign_keys
WHERE referenced_table_schema LIKE 'test';
name	unique_constraint_name	referenced_table_schema	referenced_table_name
self_ibfk_1	u2	test	self
DROP TABLE self;
SET @@foreign_key_checks= DEFAULT;
SET SESSION debug= '-d,skip_dd_table_access_check';
#
# Part of systemic test coverage for metadata locks related to foreign
# keys acquired by various DDL statements which requires debug_sync.
#
# The main part of this coverage resides in foreign_key.test.
#
connect  con1, localhost, root,,;
connection default;
#
# 7) ALTER TABLE ... INPLACE
#
# 7.1) ALTER TABLE ... ADD FOREIGN KEY ... INPLACE must start by
#      acquiring SU lock on parent table.
CREATE TABLE parent (pk INT PRIMARY KEY);
CREATE TABLE child (fk INT);
SET DEBUG_SYNC="alter_table_inplace_after_lock_downgrade SIGNAL reached WAIT_FOR go";
SET FOREIGN_KEY_CHECKS=0;
ALTER TABLE child ADD CONSTRAINT fk FOREIGN KEY (fk) REFERENCES parent (pk), ALGORITHM=INPLACE;;
connection con1;
SET DEBUG_SYNC="now WAIT_FOR reached";
# DML on parent is still possible at this point.
INSERT INTO parent VALUES (1);
# But not DDL.
SET @old_lock_wait_timeout= @@lock_wait_timeout;
SET @@lock_wait_timeout= 1;
ALTER TABLE parent ADD COLUMN a INT;
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
SET @@lock_wait_timeout= @old_lock_wait_timeout;
SET DEBUG_SYNC="now SIGNAL go";
connection default;
# Reap ALTER TABLE
SET FOREIGN_KEY_CHECKS=1;
ALTER TABLE child DROP FOREIGN KEY fk;
#
# 8) ALTER TABLE ... COPY
#
# 8.1) ALTER TABLE ... ADD FOREIGN KEY ... COPY must start by
#      acquiring SU lock on parent table.
SET DEBUG_SYNC="alter_table_copy_after_lock_upgrade SIGNAL reached WAIT_FOR go";
ALTER TABLE child ADD CONSTRAINT fk FOREIGN KEY (fk) REFERENCES parent (pk), ALGORITHM=COPY;;
connection con1;
SET DEBUG_SYNC="now WAIT_FOR reached";
# DML on parent is still possible at this point.
INSERT INTO parent VALUES (2);
# But not DDL.
SET @old_lock_wait_timeout= @@lock_wait_timeout;
SET @@lock_wait_timeout= 1;
ALTER TABLE parent ADD COLUMN a INT;
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
SET @@lock_wait_timeout= @old_lock_wait_timeout;
SET DEBUG_SYNC="now SIGNAL go";
connection default;
# Reap ALTER TABLE
SET DEBUG_SYNC="RESET";
ALTER TABLE child DROP FOREIGN KEY fk;
# 8.1') ALTER TABLE ... ADD FOREIGN KEY ... COPY due to workaround
#       must upgrade SU lock on parent table SRO lock.
SET DEBUG_SYNC="alter_after_copy_table SIGNAL reached WAIT_FOR go";
ALTER TABLE child ADD CONSTRAINT fk FOREIGN KEY (fk) REFERENCES parent (pk), ALGORITHM=COPY;;
connection con1;
SET DEBUG_SYNC="now WAIT_FOR reached";
# SELECT on parent is still possible at this point.
SELECT * FROM parent;
pk
1
2
# But not changes.
SET @old_lock_wait_timeout= @@lock_wait_timeout;
SET @@lock_wait_timeout= 1;
DELETE FROM parent;
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
SET @@lock_wait_timeout= @old_lock_wait_timeout;
SET DEBUG_SYNC="now SIGNAL go";
connection default;
# Reap ALTER TABLE
SET DEBUG_SYNC="RESET";
DROP TABLES child, parent;
connection con1;
disconnect con1;
connection default;