Is ID column required in SQL?

前端 未结 8 1535
爱一瞬间的悲伤
爱一瞬间的悲伤 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:09

    Yes, you could have many attributes (values) in a record (row) that you could use to make a record unique. This would be called a composite primary key.

    However it will be much slower in general because the construction of the primary index will be much more expensive. The primary index is used by relational database management systems (RDBMS) not only to determine uniqueness, but also in how they order and structure records on disk.

    A simple primary key of one incrementing value is generally the most performant and the easiest solution for the RDBMS to manage.

    0 讨论(0)
  • 2020-12-30 00:16

    If you really do have some pre-existing column in your data set that already does uniquely identify your row - then no, there's no need for an extra ID column. The primary key however must be unique (in ALL circumstances) and cannot be empty (must be NOT NULL).

    In my 20+ years of experience in database design, however, this is almost never truly the case. Most "natural" ID's that appear to be unique aren't - ultimately. US Social Security Numbers aren't guaranteed to be unique, and most other "natural" keys end up being almost unique - and that's just not good enough for a database system.

    So if you really do have a proper, unique key in your data already - use it! But most of the time, it's easier and more convenient to have just a single surrogate ID that you can guarantee will be unique over all rows.

    0 讨论(0)
提交回复
热议问题