When we don't need a primary key for our table?

后端 未结 4 883
無奈伤痛
無奈伤痛 2021-02-04 10:06

Will it ever happen that we design a table that doesn\'t need a primary key?

4条回答
  •  天命终不由人
    2021-02-04 10:46

    Primary key not mandatory but it is not a good practice to create tables without primary key. DBMS creates auto-index on PK, but you can make a column unique and index it, e.g. user_name column in users table are usually made unique and indexed, so you may choose to skip PK here. But it is still a bad idea because PK can be used as foreign key for referential integrity.

    In general, you should almost always have PK in a table unless you have very strong reason to justify not having a PK.

    Link tables (in many to many relationship) may not have a primary key. But, I personally like to have PK in those tables as well.

提交回复
热议问题