Where should I store a foreign key?

后端 未结 4 574
猫巷女王i
猫巷女王i 2021-01-31 09:52

If I have a relationship between two tables (both tables have their own primary keys) what should guide my decision as to which table should store the foreign key? I understand

4条回答
  •  孤街浪徒
    2021-01-31 10:24

    Like a primary key, a foreign key is also a type of constraint placed on one or more columns in a table.

    The foreign key establishes a link between the key columns and related columns in another table. (You can also link the foreign key columns to columns within the same table.)

    The table that contains the foreign key is considered the child table, and the table that the foreign key references is the parent table.

    Key Points

    1. The foreign key must reference a primary key or unique constraint, although that reference can be on the same table or on a different table
    2. A foreign key must also have the same number of columns as the number of columns in the referenced constraint, and the data types must match between corresponding columns.
    3. Unlike Primary key, Foreign key columns can contain NULL values.

提交回复
热议问题