How do Constraints Work?, FOREIGN KEY (Part 2 of 5)
A foreign key has the following features:> A table can have zero, one, or many foreign keys. It uses the reserved words REFERENCE or FOREIGN KEY in the CREATE TABLE statement.
> A simple key (one column) can be defined as a column constraint or a table constraint. As a simple key column constraint, REFERENCES is placed in the column definition.
> A composite key (multiple columns) can only be defined as a table constraint. As a table constraint, the REFERENCES and PRIMARY KEY definitions have the column names specified.
> A FOREIGN KEY column can reference a PRIMARY KEY or a UNIQUE column in another table, and this references column is called the parent key. The FOREIGN KEY and parent key columns must be of the same or convertibly similar data type.
> Foreign key constraints make updates and deletes easier, because inconsistencies are not introduced into the database. For example, you can not place an employee into the payroll table, until that employee exists in the employee table, and when you delete the employee from the employee table, the employee will also be deleted from the payroll table. This prevents orphan rows.
> The general practice is to use a CONSTRAINT clause to explicitly name all foreign keys. This gives a foreign key constraint name to make maintenance easier.