One foreign key with multiple columns VS multiple foreign keys with single column One foreign key with multiple columns VS multiple foreign keys with single column oracle oracle

One foreign key with multiple columns VS multiple foreign keys with single column


My question: are SQL code 1 and SQL code 2 equal?

Not, they are not equivalent.

By the definition

Foreign Key Constraints

A foreign key constraint (also called a referential integrity constraint) designates a column as the foreign key and establishes a relationship between that foreign key and a specified primary or unique key, called the referenced key. A composite foreign key designates a combination of columns as the foreign key.


According to the above this declaration:

CONSTRAINT FK_E      FOREIGN KEY (E1, E2, E3)      REFERENCES F (E1, E2, E3),

assumes that there is either a primary key or unique constraint created on F table

CREATE TABLE F(  .....  .....  CONSTRAINT my_pk PRIMARY KEY(E1, E2, E3))

while this declaration

 CONSTRAINT FK_E1      FOREIGN KEY (E1)      REFERENCES F (E1), CONSTRAINT FK_E2      FOREIGN KEY (E2)      REFERENCES F (E2), CONSTRAINT FK_E3      FOREIGN KEY (E3)      REFERENCES F (E3),

is in a need of existence of three constraints, either primary key or unique indexses/constraints:

CREATE TABLE F(  .....  .....  CONSTRAINT my_pk1 PRIMARY KEY(E1),  CONSTRAINT my_uq2 UNIQUE(E2),  CONSTRAINT my_uq3 UNIQUE(E3))

Note 1- the table can only have one primary key, so only one constraint in your example could be the primary key, the rest 2 (or all 3) must be unique keys.

Note 2 - there is a slight semantic difference between the primary key constraint and the unique key constraint. The primary key values must be unique and must not contain null values while the unique key values can be NULL.


In the first case the table F can contain these values

E1  E2  E3 1   1   1 1   1   2 2   2   1

and the child table can contain only these records:

E1  E2  E3 1   1   1 1   1   2 2   2   1

but you cannot insert to the child table these combination of values because they don't exists in the parent table:

 E1  E2  E3 1   2   1 2   2   2

In the secondcase the table F can contain these values

E1  E2  E3 1   1   1 2   2   2 3   3   3

but cannot contain these values, because each column must be unique:

E1  E2  E3 1   1   1 1   1   2 2   1   3

while the child table can contain these records:

E1  E2  E3 1   2   3 3   1   2 2   1   3