Sunday, September 7, 2014

Foreign Key



The foreign key is a column in a table that references the primary key of another table.


A NULL foreign key means that there is no matching primary key in the parent table.

Referential Integrity

You will only be able to insert values into your foreign key that exist in the table the key came from, the parent table.


You can use foreign key to reference a unique value in the parent table.
It does not have to be the primary key of the parent table, but it must be unique.

One-to-One: exactly one row of a parent table is related to one row of a child table.
It is not used that often.

One-to-many: a record in a table A can have many matching records in an other table B, but record in table B can only match ONE record in table A.

Many-to-Many: needs a junction table to avoid duplicates
It is usually made up of two one-to-many relationships with a junction table in between




No comments:

Post a Comment