Is ID column required in SQL?

前端 未结 8 1571
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-29 23:50

Traditionally I have always used an ID column in SQL (mostly mysql and postgresql).

However I am wondering if it is really necessary if the rest of the columns in ea

8条回答
  •  不思量自难忘°
    2020-12-30 00:08

    You should have one column in every table that is unique.

    EDITED...

    This is one of the fundamentals of database table design. It's the row identifier - the identifier identifies which row(s) are being acted upon (updated/deleted etc). Relying on column combinations that are "unique", eg (first_name, last_name, city), as your key can quickly lead to problems when two John Smiths exist, or worse when John Smith moves city and you get a collision.

    In most cases, it's best to use a an artificial key that's guaranteed to be unique - like an auto increment integer. That's why they are so popular - they're needed. Commonly, the key column is simply called id, or sometimes _id. (I prefer id)

    If natural data is available that is unique and present for every row (perhaps retinal scan data for people), you can use that, but all-to-often, such data isn't available for every row.

    Ideally, you should have only one unique column. That is, there should only be one key.

提交回复
热议问题