in general, should every table in a database have an identity field to use as a PK?

前端 未结 10 1915
旧时难觅i
旧时难觅i 2020-12-07 15:36

This seems like a duplicate even as I ask it, but I searched and didn\'t find it. It seems like a good question for SO -- even though I\'m sure I can find it on many blogs e

10条回答
  •  我在风中等你
    2020-12-07 16:20

    I can't think of any drawback about having an ID field in each table. Providing your the type of your ID field provides enough space for your table to grow.

    However, you don't necessarily need a single field to ensure the identity of your rows. So no, a single ID field is not mandatory.

    Primary and Foreign Keys can consist not only of one field, but of multiple fields. This is typical for tables implementing a N-N relationship.

    You can perfectly have PRIMARY KEY (fa, fb) on your table:

    CREATE TABLE t(fa INT , fb INT);
    ALTER TABLE t ADD PRIMARY KEY(fa , fb);
    

提交回复
热议问题