Skip to content

Commit 14df29b

Browse files
committed
Bug#36345882 Fix column ordering in Dbdict::buildFK_prepare
Contributed by Axel Svensson. If one have a table with data adding a multicolumn foreign key constraint could fail even if the data satisifies the constraint. Namely if the order columns are mentioned in the references clause do not match the order the columns are defined for the table. Example#1, alter table add constraint foreign key: mysql> create table prnt (pk1 int, pk2 int, primary key(pk2,pk1) -> ) engine=ndbcluster; mysql> create table chld (pk int primary key, col1 int, col2 int -> ) engine=ndbcluster; mysql> insert into prnt values (1,2); mysql> insert into chld values (0,1,2); mysql> alter table chld add -> constraint fk foreign key(col2,col1) -> references prnt(pk2,pk1); mysql> \W Show warnings enabled. mysql> alter table chld -> add constraint fk foreign key(col2,col1) -> references prnt(pk2,pk1); ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (Unknown error code) Warning (Code 1296): Got error 255 'Foreign key constraint violated: No parent row found' from NDB Error (Code 1452): Cannot add or update a child row: a foreign key constraint fails (Unknown error code) Example#2, ndb_restore --rebuild-indexes: mysql> create table prnt (pk1 int, pk2 int, primary key(pk2,pk1) -> ) engine=ndbcluster; mysql> create table chld (pk int primary key, col1 int, col2 int, -> constraint fk foreign key(col2,col1) -> references prnt(pk2,pk1) -> ) engine=ndbcluster; mysql> insert into prnt values (1,2); mysql> insert into chld values (0,1,2); ndb_mgm> start backup ... Node 1: Backup 1 started from node 3 completed mysql> drop table chld, prnt; $ ndb_restore -b 1 -n 1 -m --disable-indexes BACKUP/BACKUP-1 $ ndb_restore -b 1 -n 1 -r --disable-indexes BACKUP/BACKUP-1 $ ndb_restore -b 1 -n 2 -r --disable-indexes BACKUP/BACKUP-1 $ ndb_restore -b 1 -n 1 --rebuild-indexes BACKUP/BACKUP-1 ... Failed to create foreign key fk parent test.prnt.PK child test.chld.fk : 255: Foreign key constraint violated: No parent row found Problem: When checking the existing data for a pending foreign key constraint with multi column key, columns from referencing table were matches with the columns in the referenced table in wrong order. In the examples above, the columns (col2,col1) from referencing table is matches against columns (pk1,pk2) in referenced table resulting in col2 is matched against pk1 and col1 against pk2 instead of the intention to match col2 against pk2 and col1 against pk1. The checking code expected the index in parent table to use the same column order as specified in the references clause of constraint definition. But unique indexes in NDB always uses the column order as they appear in table definition. In example above: - PRIMARY KEY(pk1,pk2) - PRIMARY KEY(pk2,pk1) - UNIQUE KEY(pk1,pk2) - UNIQUE KEY(pk2,pk1) all would use key with column order as (pk1,pk2) since that is the order in table definition create table prnt (pk1 int, pk2 int) Also note that the ordered index in the child table must have the columns in the order specified in the constraint. Note that the code maintaining the foreign key constraint did handle multicolumn keys correctly. Fix: When Dbdict sends GSN_BUILD_FK_IMPL_REQ to TRIX it now sends the child columns to fetch matching the parent index order. Change-Id: I3bd57d3d2c36628c1dc03e1f590a2d5bcb9b04de
1 parent 8b1ddfa commit 14df29b

File tree

7 files changed

+594
-64
lines changed

7 files changed

+594
-64
lines changed

mysql-test/suite/ndb/include/ndb_desc_print.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ let $ndb_desc_cmd= $NDB_DESC --no-defaults $ndb_desc_opts;
1616
replace_regex /Version: [0-9]*/Version: Any/
1717
/NDB\$BLOB_[0-9]*_/NDB$BLOB_XX_/
1818
/Length of frm data: [0-9]*/Length of frm data: XXX/
19+
/^[0-9]+\/[0-9]+/XX\/XX/
1920
/IndexTable [0-9]*/IndexTable XX/
2021
/^([0-9][0-9]*) [0-9 ]*/\1 .../;
2122
--exec $ndb_desc_cmd

mysql-test/suite/ndb/r/ndb_fk_build.result

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,52 @@ drop foreign key fk4,
4242
drop foreign key fk5,
4343
drop foreign key fk6;
4444
drop table parent, child;
45+
create table parent (
46+
b1 int as (b) stored not null,
47+
c1 int as (c) stored not null,
48+
a1 int as (a) stored not null,
49+
b2 int as (b) stored not null,
50+
c2 int as (c) stored not null,
51+
a2 int as (a) stored not null,
52+
a int default 0,
53+
b int default 0,
54+
c int default 0,
55+
primary key pk1 (a1,b1,c1) using hash,
56+
unique key uk2 (a2,b2,c2) using hash
57+
) engine=ndbcluster;
58+
create table child (
59+
a int default 0,
60+
b int default 0,
61+
c int default 0,
62+
d int default 0,
63+
e int default 0,
64+
f int default 0,
65+
b1 int as (b) stored not null,
66+
c1 int as (c) stored not null,
67+
a1 int as (a) stored not null,
68+
b2 int as (b) stored not null,
69+
c2 int as (c) stored not null,
70+
a2 int as (a) stored not null,
71+
b3 int as (b) stored not null,
72+
c3 int as (c) stored not null,
73+
a3 int as (a) stored not null,
74+
primary key pk1 (a1,b1,c1) using hash,
75+
unique key uk2 (a2,b2,c2) using hash,
76+
key ok3 (a3,b3,c3),
77+
key ok3def (c3,b3,a3,d,e,f)
78+
) engine=ndbcluster;
79+
insert into parent (a, b, c) values (11, 12, 13);
80+
insert into child (a, b, c) values (11, 12, 13);
81+
alter table child add
82+
constraint fk11 foreign key (c1,b1,a1) references parent (c1,b1,a1);
83+
alter table child add
84+
constraint fk21 foreign key (c2,b2,a2) references parent (c1,b1,a1);
85+
alter table child add
86+
constraint fk31 foreign key (c3,b3,a3) references parent (c1,b1,a1);
87+
alter table child add
88+
constraint fk12 foreign key (c1,b1,a1) references parent (c2,b2,a2);
89+
alter table child add
90+
constraint fk22 foreign key (c2,b2,a2) references parent (c2,b2,a2);
91+
alter table child add
92+
constraint fk32 foreign key (c3,b3,a3) references parent (c2,b2,a2);
93+
drop table child, parent;

mysql-test/suite/ndb/r/ndb_fk_restore.result

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,3 +594,276 @@ drop table db3.t5, db3.t4;
594594
# cleanup
595595
drop table db3.t6, db3.t7, db3.t8, db3.t9;
596596
drop database db3;
597+
#
598+
# Bug#36345882 Fix column ordering in Dbdict::buildFK_prepare
599+
#
600+
use test;
601+
create table t1 (
602+
b1 int as (b) stored not null,
603+
c1 int as (c) stored not null,
604+
a1 int as (a) stored not null,
605+
b2 int as (b) stored not null,
606+
c2 int as (c) stored not null,
607+
a2 int as (a) stored not null,
608+
a int default 0,
609+
b int default 0,
610+
c int default 0,
611+
primary key pk1 (a1,b1,c1) using hash,
612+
unique key uk2 (a2,b2,c2) using hash
613+
) engine=ndbcluster;
614+
create table t2 (
615+
a int default 0,
616+
b int default 0,
617+
c int default 0,
618+
d int default 0,
619+
e int default 0,
620+
f int default 0,
621+
b1 int as (b) stored not null,
622+
c1 int as (c) stored not null,
623+
a1 int as (a) stored not null,
624+
b2 int as (b) stored not null,
625+
c2 int as (c) stored not null,
626+
a2 int as (a) stored not null,
627+
b3 int as (b) stored not null,
628+
c3 int as (c) stored not null,
629+
a3 int as (a) stored not null,
630+
primary key pk1 (a1,b1,c1) using hash,
631+
unique key uk2 (a2,b2,c2) using hash,
632+
key ok3 (a3,b3,c3),
633+
key ok3def (c3,b3,a3,d,e,f),
634+
constraint fk11 foreign key (c1,b1,a1) references t1 (c1,b1,a1),
635+
constraint fk21 foreign key (c2,b2,a2) references t1 (c1,b1,a1),
636+
constraint fk31 foreign key (c3,b3,a3) references t1 (c1,b1,a1),
637+
constraint fk12 foreign key (c1,b1,a1) references t1 (c2,b2,a2),
638+
constraint fk22 foreign key (c2,b2,a2) references t1 (c2,b2,a2),
639+
constraint fk32 foreign key (c3,b3,a3) references t1 (c2,b2,a2)
640+
) engine=ndbcluster;
641+
alter table t2 drop index ok3;
642+
alter table t2 drop index fk12;
643+
alter table t2 drop index fk22;
644+
-- t1 --
645+
Version: Any
646+
Fragment type: HashMapPartition
647+
K Value: 6
648+
Min load factor: 78
649+
Max load factor: 80
650+
Temporary table: no
651+
Number of attributes: 9
652+
Number of primary keys: 3
653+
Length of frm data: XXX
654+
Max Rows: 0
655+
Row Checksum: 1
656+
Row GCI: 1
657+
SingleUserMode: 0
658+
ForceVarPart: 1
659+
PartitionCount: 8
660+
FragmentCount: 8
661+
PartitionBalance: FOR_RP_BY_LDM
662+
ExtraRowGciBits: 0
663+
ExtraRowAuthorBits: 0
664+
TableStatus: Retrieved
665+
Table options:
666+
HashMap: DEFAULT-HASHMAP-3840-8
667+
-- Attributes --
668+
b1 Int PRIMARY KEY DISTRIBUTION KEY AT=FIXED ST=MEMORY
669+
c1 Int PRIMARY KEY DISTRIBUTION KEY AT=FIXED ST=MEMORY
670+
a1 Int PRIMARY KEY DISTRIBUTION KEY AT=FIXED ST=MEMORY
671+
b2 Int NOT NULL AT=FIXED ST=MEMORY
672+
c2 Int NOT NULL AT=FIXED ST=MEMORY
673+
a2 Int NOT NULL AT=FIXED ST=MEMORY
674+
a Int NULL AT=FIXED ST=MEMORY DEFAULT 0
675+
b Int NULL AT=FIXED ST=MEMORY DEFAULT 0
676+
c Int NULL AT=FIXED ST=MEMORY DEFAULT 0
677+
-- Indexes --
678+
PRIMARY KEY(b1, c1, a1) - UniqueHashIndex
679+
uk2$unique(b2, c2, a2) - UniqueHashIndex
680+
-- t2 --
681+
Version: Any
682+
Fragment type: HashMapPartition
683+
K Value: 6
684+
Min load factor: 78
685+
Max load factor: 80
686+
Temporary table: no
687+
Number of attributes: 15
688+
Number of primary keys: 3
689+
Length of frm data: XXX
690+
Max Rows: 0
691+
Row Checksum: 1
692+
Row GCI: 1
693+
SingleUserMode: 0
694+
ForceVarPart: 1
695+
PartitionCount: 8
696+
FragmentCount: 8
697+
PartitionBalance: FOR_RP_BY_LDM
698+
ExtraRowGciBits: 0
699+
ExtraRowAuthorBits: 0
700+
TableStatus: Retrieved
701+
Table options:
702+
HashMap: DEFAULT-HASHMAP-3840-8
703+
-- Attributes --
704+
a Int NULL AT=FIXED ST=MEMORY DEFAULT 0
705+
b Int NULL AT=FIXED ST=MEMORY DEFAULT 0
706+
c Int NULL AT=FIXED ST=MEMORY DEFAULT 0
707+
d Int NULL AT=FIXED ST=MEMORY DEFAULT 0
708+
e Int NULL AT=FIXED ST=MEMORY DEFAULT 0
709+
f Int NULL AT=FIXED ST=MEMORY DEFAULT 0
710+
b1 Int PRIMARY KEY DISTRIBUTION KEY AT=FIXED ST=MEMORY
711+
c1 Int PRIMARY KEY DISTRIBUTION KEY AT=FIXED ST=MEMORY
712+
a1 Int PRIMARY KEY DISTRIBUTION KEY AT=FIXED ST=MEMORY
713+
b2 Int NOT NULL AT=FIXED ST=MEMORY
714+
c2 Int NOT NULL AT=FIXED ST=MEMORY
715+
a2 Int NOT NULL AT=FIXED ST=MEMORY
716+
b3 Int NOT NULL AT=FIXED ST=MEMORY
717+
c3 Int NOT NULL AT=FIXED ST=MEMORY
718+
a3 Int NOT NULL AT=FIXED ST=MEMORY
719+
-- Indexes --
720+
PRIMARY KEY(b1, c1, a1) - UniqueHashIndex
721+
uk2$unique(b2, c2, a2) - UniqueHashIndex
722+
ok3def(c3, b3, a3, d, e, f) - OrderedIndex
723+
-- ForeignKeys --
724+
XX/XX/fk11 PRIMARY KEY (c1, b1, a1) REFERENCES test.t1/PRIMARY KEY () on update noaction on delete noaction
725+
XX/XX/fk21 uk2$unique (c2, b2, a2) REFERENCES test.t1/PRIMARY KEY () on update noaction on delete noaction
726+
XX/XX/fk31 ok3def (c3, b3, a3) REFERENCES test.t1/PRIMARY KEY () on update noaction on delete noaction
727+
XX/XX/fk12 PRIMARY KEY (c1, b1, a1) REFERENCES test.t1/uk2$unique () on update noaction on delete noaction
728+
XX/XX/fk22 uk2$unique (c2, b2, a2) REFERENCES test.t1/uk2$unique () on update noaction on delete noaction
729+
XX/XX/fk32 ok3def (c3, b3, a3) REFERENCES test.t1/uk2$unique () on update noaction on delete noaction
730+
731+
NDBT_ProgramExit: 0 - OK
732+
733+
insert into t1 (a, b, c) values (11, 12, 13);
734+
insert into t2 (a, b, c) values (11, 12, 13);
735+
insert into t2 (a,b,c) values (101, 102, 103);
736+
ERROR 23000: Cannot add or update a child row: a foreign key constraint fails (Unknown error code)
737+
# show meta
738+
show create table t2;
739+
Table Create Table
740+
t2 CREATE TABLE `t2` (
741+
`a` int(11) DEFAULT '0',
742+
`b` int(11) DEFAULT '0',
743+
`c` int(11) DEFAULT '0',
744+
`d` int(11) DEFAULT '0',
745+
`e` int(11) DEFAULT '0',
746+
`f` int(11) DEFAULT '0',
747+
`b1` int(11) GENERATED ALWAYS AS (`b`) STORED NOT NULL,
748+
`c1` int(11) GENERATED ALWAYS AS (`c`) STORED NOT NULL,
749+
`a1` int(11) GENERATED ALWAYS AS (`a`) STORED NOT NULL,
750+
`b2` int(11) GENERATED ALWAYS AS (`b`) STORED NOT NULL,
751+
`c2` int(11) GENERATED ALWAYS AS (`c`) STORED NOT NULL,
752+
`a2` int(11) GENERATED ALWAYS AS (`a`) STORED NOT NULL,
753+
`b3` int(11) GENERATED ALWAYS AS (`b`) STORED NOT NULL,
754+
`c3` int(11) GENERATED ALWAYS AS (`c`) STORED NOT NULL,
755+
`a3` int(11) GENERATED ALWAYS AS (`a`) STORED NOT NULL,
756+
PRIMARY KEY (`a1`,`b1`,`c1`) USING HASH,
757+
UNIQUE KEY `uk2` (`a2`,`b2`,`c2`) USING HASH,
758+
KEY `ok3def` (`c3`,`b3`,`a3`,`d`,`e`,`f`),
759+
CONSTRAINT `fk11` FOREIGN KEY (`c1`,`b1`,`a1`) REFERENCES `t1` (`c1`,`b1`,`a1`) ON DELETE NO ACTION ON UPDATE NO ACTION,
760+
CONSTRAINT `fk12` FOREIGN KEY (`c1`,`b1`,`a1`) REFERENCES `t1` (`c2`,`b2`,`a2`) ON DELETE NO ACTION ON UPDATE NO ACTION,
761+
CONSTRAINT `fk21` FOREIGN KEY (`c2`,`b2`,`a2`) REFERENCES `t1` (`c1`,`b1`,`a1`) ON DELETE NO ACTION ON UPDATE NO ACTION,
762+
CONSTRAINT `fk22` FOREIGN KEY (`c2`,`b2`,`a2`) REFERENCES `t1` (`c2`,`b2`,`a2`) ON DELETE NO ACTION ON UPDATE NO ACTION,
763+
CONSTRAINT `fk31` FOREIGN KEY (`c3`,`b3`,`a3`) REFERENCES `t1` (`c1`,`b1`,`a1`) ON DELETE NO ACTION ON UPDATE NO ACTION,
764+
CONSTRAINT `fk32` FOREIGN KEY (`c3`,`b3`,`a3`) REFERENCES `t1` (`c2`,`b2`,`a2`) ON DELETE NO ACTION ON UPDATE NO ACTION
765+
) ENGINE=ndbcluster DEFAULT CHARSET=latin1
766+
767+
ndb_show_tables completed.....
768+
769+
select type,name from ndb_show_tables_results
770+
where type in ('''ForeignKey''','''FKParentTrigger''','''FKChildTrigger''');
771+
type name
772+
'FKChildTrigger' 'NDB$FK_XX_CHILD_XX'
773+
'FKChildTrigger' 'NDB$FK_XX_CHILD_XX'
774+
'FKChildTrigger' 'NDB$FK_XX_CHILD_XX'
775+
'FKChildTrigger' 'NDB$FK_XX_CHILD_XX'
776+
'FKChildTrigger' 'NDB$FK_XX_CHILD_XX'
777+
'FKChildTrigger' 'NDB$FK_XX_CHILD_XX'
778+
'FKParentTrigger' 'NDB$FK_XX_PARENT_XX'
779+
'FKParentTrigger' 'NDB$FK_XX_PARENT_XX'
780+
'FKParentTrigger' 'NDB$FK_XX_PARENT_XX'
781+
'FKParentTrigger' 'NDB$FK_XX_PARENT_XX'
782+
'FKParentTrigger' 'NDB$FK_XX_PARENT_XX'
783+
'FKParentTrigger' 'NDB$FK_XX_PARENT_XX'
784+
'ForeignKey' 'XX/XX/fk11'
785+
'ForeignKey' 'XX/XX/fk12'
786+
'ForeignKey' 'XX/XX/fk21'
787+
'ForeignKey' 'XX/XX/fk22'
788+
'ForeignKey' 'XX/XX/fk31'
789+
'ForeignKey' 'XX/XX/fk32'
790+
# show counts
791+
select count(*) from t1;
792+
count(*)
793+
1
794+
select count(*) from t2;
795+
count(*)
796+
1
797+
# run backup
798+
# drop tables
799+
drop table t2, t1;
800+
# restore meta (disable indexes)
801+
# restore data (disable indexes)
802+
# rebuild indexes
803+
# show meta
804+
show create table t2;
805+
Table Create Table
806+
t2 CREATE TABLE `t2` (
807+
`a` int(11) DEFAULT '0',
808+
`b` int(11) DEFAULT '0',
809+
`c` int(11) DEFAULT '0',
810+
`d` int(11) DEFAULT '0',
811+
`e` int(11) DEFAULT '0',
812+
`f` int(11) DEFAULT '0',
813+
`b1` int(11) GENERATED ALWAYS AS (`b`) STORED NOT NULL,
814+
`c1` int(11) GENERATED ALWAYS AS (`c`) STORED NOT NULL,
815+
`a1` int(11) GENERATED ALWAYS AS (`a`) STORED NOT NULL,
816+
`b2` int(11) GENERATED ALWAYS AS (`b`) STORED NOT NULL,
817+
`c2` int(11) GENERATED ALWAYS AS (`c`) STORED NOT NULL,
818+
`a2` int(11) GENERATED ALWAYS AS (`a`) STORED NOT NULL,
819+
`b3` int(11) GENERATED ALWAYS AS (`b`) STORED NOT NULL,
820+
`c3` int(11) GENERATED ALWAYS AS (`c`) STORED NOT NULL,
821+
`a3` int(11) GENERATED ALWAYS AS (`a`) STORED NOT NULL,
822+
PRIMARY KEY (`a1`,`b1`,`c1`) USING HASH,
823+
UNIQUE KEY `uk2` (`a2`,`b2`,`c2`) USING HASH,
824+
KEY `ok3def` (`c3`,`b3`,`a3`,`d`,`e`,`f`),
825+
CONSTRAINT `fk11` FOREIGN KEY (`c1`,`b1`,`a1`) REFERENCES `t1` (`c1`,`b1`,`a1`) ON DELETE NO ACTION ON UPDATE NO ACTION,
826+
CONSTRAINT `fk12` FOREIGN KEY (`c1`,`b1`,`a1`) REFERENCES `t1` (`c2`,`b2`,`a2`) ON DELETE NO ACTION ON UPDATE NO ACTION,
827+
CONSTRAINT `fk21` FOREIGN KEY (`c2`,`b2`,`a2`) REFERENCES `t1` (`c1`,`b1`,`a1`) ON DELETE NO ACTION ON UPDATE NO ACTION,
828+
CONSTRAINT `fk22` FOREIGN KEY (`c2`,`b2`,`a2`) REFERENCES `t1` (`c2`,`b2`,`a2`) ON DELETE NO ACTION ON UPDATE NO ACTION,
829+
CONSTRAINT `fk31` FOREIGN KEY (`c3`,`b3`,`a3`) REFERENCES `t1` (`c1`,`b1`,`a1`) ON DELETE NO ACTION ON UPDATE NO ACTION,
830+
CONSTRAINT `fk32` FOREIGN KEY (`c3`,`b3`,`a3`) REFERENCES `t1` (`c2`,`b2`,`a2`) ON DELETE NO ACTION ON UPDATE NO ACTION
831+
) ENGINE=ndbcluster DEFAULT CHARSET=latin1
832+
833+
ndb_show_tables completed.....
834+
835+
select type,name from ndb_show_tables_results
836+
where type in ('''ForeignKey''','''FKParentTrigger''','''FKChildTrigger''');
837+
type name
838+
'FKChildTrigger' 'NDB$FK_XX_CHILD_XX'
839+
'FKChildTrigger' 'NDB$FK_XX_CHILD_XX'
840+
'FKChildTrigger' 'NDB$FK_XX_CHILD_XX'
841+
'FKChildTrigger' 'NDB$FK_XX_CHILD_XX'
842+
'FKChildTrigger' 'NDB$FK_XX_CHILD_XX'
843+
'FKChildTrigger' 'NDB$FK_XX_CHILD_XX'
844+
'FKParentTrigger' 'NDB$FK_XX_PARENT_XX'
845+
'FKParentTrigger' 'NDB$FK_XX_PARENT_XX'
846+
'FKParentTrigger' 'NDB$FK_XX_PARENT_XX'
847+
'FKParentTrigger' 'NDB$FK_XX_PARENT_XX'
848+
'FKParentTrigger' 'NDB$FK_XX_PARENT_XX'
849+
'FKParentTrigger' 'NDB$FK_XX_PARENT_XX'
850+
'ForeignKey' 'XX/XX/fk11'
851+
'ForeignKey' 'XX/XX/fk12'
852+
'ForeignKey' 'XX/XX/fk21'
853+
'ForeignKey' 'XX/XX/fk22'
854+
'ForeignKey' 'XX/XX/fk31'
855+
'ForeignKey' 'XX/XX/fk32'
856+
# show counts
857+
select count(*) from t1;
858+
count(*)
859+
1
860+
select count(*) from t2;
861+
count(*)
862+
1
863+
select * from t1;
864+
b1 c1 a1 b2 c2 a2 a b c
865+
12 13 11 12 13 11 11 12 13
866+
select * from t2;
867+
a b c d e f b1 c1 a1 b2 c2 a2 b3 c3 a3
868+
11 12 13 0 0 0 12 13 11 12 13 11 12 13 11
869+
drop table t2,t1;

mysql-test/suite/ndb/t/ndb_fk_build.test

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,61 @@ drop foreign key fk5,
6666
drop foreign key fk6;
6767

6868
drop table parent, child;
69+
70+
# Check multi column foreign keys
71+
72+
eval create table parent (
73+
b1 int as (b) stored not null,
74+
c1 int as (c) stored not null,
75+
a1 int as (a) stored not null,
76+
b2 int as (b) stored not null,
77+
c2 int as (c) stored not null,
78+
a2 int as (a) stored not null,
79+
a int default 0,
80+
b int default 0,
81+
c int default 0,
82+
primary key pk1 (a1,b1,c1) using hash,
83+
unique key uk2 (a2,b2,c2) using hash
84+
) engine=ndbcluster;
85+
86+
eval create table child (
87+
a int default 0,
88+
b int default 0,
89+
c int default 0,
90+
d int default 0,
91+
e int default 0,
92+
f int default 0,
93+
b1 int as (b) stored not null,
94+
c1 int as (c) stored not null,
95+
a1 int as (a) stored not null,
96+
b2 int as (b) stored not null,
97+
c2 int as (c) stored not null,
98+
a2 int as (a) stored not null,
99+
b3 int as (b) stored not null,
100+
c3 int as (c) stored not null,
101+
a3 int as (a) stored not null,
102+
primary key pk1 (a1,b1,c1) using hash,
103+
unique key uk2 (a2,b2,c2) using hash,
104+
key ok3 (a3,b3,c3),
105+
key ok3def (c3,b3,a3,d,e,f)
106+
) engine=ndbcluster;
107+
108+
# Add parent row, use different values for the different columns.
109+
insert into parent (a, b, c) values (11, 12, 13);
110+
# Add valid child row.
111+
insert into child (a, b, c) values (11, 12, 13);
112+
113+
alter table child add
114+
constraint fk11 foreign key (c1,b1,a1) references parent (c1,b1,a1);
115+
alter table child add
116+
constraint fk21 foreign key (c2,b2,a2) references parent (c1,b1,a1);
117+
alter table child add
118+
constraint fk31 foreign key (c3,b3,a3) references parent (c1,b1,a1);
119+
alter table child add
120+
constraint fk12 foreign key (c1,b1,a1) references parent (c2,b2,a2);
121+
alter table child add
122+
constraint fk22 foreign key (c2,b2,a2) references parent (c2,b2,a2);
123+
alter table child add
124+
constraint fk32 foreign key (c3,b3,a3) references parent (c2,b2,a2);
125+
126+
drop table child, parent;

0 commit comments

Comments
 (0)