I would like to design a table named arguments whose an attribute name is linked to another attribute name in a table called names.
I see two ways to express it in SQL:
-
by creating a constraint on the table:
CREATE TABLE names ( name text UNIQUE, short text UNIQUE, comment text); CREATE TABLE arguments ( name text UNIQUE, comment text, FOREIGN KEY (name) REFERENCES names (name)); -
by qualifying the attribute on-the-fly:
CREATE TABLE names ( name text UNIQUE, short text UNIQUE, comment text); CREATE TABLE arguments ( name text UNIQUE REFERENCES names (name), comment text);
I would like to know:
- if one of the two is commonly known as better than the other, and
- if it can have consequences that I should be aware of.
Thank you for your help.
While the first option is known as
out-of-lineconstraint declaration and the second option isin-line, both of them are functionally same.What would be better is to assign a name to the foreign key constraint. If you have a name, you can selectively enable and disable the constraint if required.
Create table
Disable constraint
Enable constraint
This is for SQL Server. Oracle has equivalent commands.