user157629
user157629

Reputation: 634

Are this foreign key instances the same?

Is there any difference in declaring foreign keys on tables between this two options?

OPTION 1

create table Table1 (

    name varchar(255),
    id_fkey int references Table2 (id)
);

OPTION 2

create table Table1 (

    name varchar(255),
    id_fkey int,
    foreign key (id_fkey) references Table2 (id)
);

Are both declarations of a proper foreign key or do they have any difference?

Upvotes: 0

Views: 79

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 246598

These are two ways to do the same thing. The first syntax is called column constraint, the second table constraint.

The only real difference is that a foreign key over more than one column can only be written as a table constraint.

Upvotes: 1

Related Questions