Skip to content
Advertisement

constraints foreign key pros

so besides just naming a foreign key, is there any real advantage creating a constraint foreign key instead of a normal foreign key?

Advertisement

Answer

The syntax for adding an foreign key using references inside a create table statement is:

[ CONSTRAINT constraint_name ]
REFERENCES reftable [ ( refcolumn ) ]
[ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
[ ON DELETE referential_action ] [ ON UPDATE referential_action ]

and using alter table ... add constraint:

[ CONSTRAINT constraint_name ]
FOREIGN KEY ( column_name [, ... ] ) REFERENCES reftable [ ( refcolumn [, ... ] ) ]
[ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
[ ON DELETE referential_action ] [ ON UPDATE referential_action ]

That only functional difference is that the alter table form lets you create foreign keys that use more than one column.

Of course you can use the alter table form inside create table as a table constraint rather than a column constraint:

create table T (
    ...
    foreign key (c1, c2) references T2(c1, c2)
)

In all cases, the underly foreign keys are the same, the only difference is where you define them.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement