Do MySQL tables need an ID? [duplicate]

狂风中的少年 提交于 2020-01-30 04:34:33

问题


Possible Duplicate:
Should each and every table have a primary key?

If you never refer to the ID anywhere is it necessary to include one? Does a table need an ID or primary key?


回答1:


No you do not need a primary key to make a table work in MySQL. That said, a primary key allows for a unique value to refer to a row in a table from another table, or in any code using the table.

You do need a primary key to make a table work well in MySQL though. Indexes (which the primary key is one of) allow MySQL to search through small, highly optimized subsets of the table to process relationships and searches. In general, any fields that you use in a WHERE clause or use to link two tables together should be indexed.




回答2:


Actually, InnoDB uses its own row id as PK for the table in case you didn't create one, so it can use it for indexing. And that has some negative effects on performance.

See a very good explanation here: http://blog.johnjosephbachir.org/2006/10/22/everything-you-need-to-know-about-designing-mysql-innodb-primary-keys/

To sum it up, there are 3 rules:

  1. Do explicitly define a primary key, and make it numeric.
  2. The primary key should be sequential.
  3. The primary key should be as small as possible.

As a side note: some SQL editors and tools may have issues if there is no PK on a table.

When you are manually editing result sets or table data in such a tool, the tool runs an UPDATE command. In case there is no unique key, several identical records may be inserted, and then there is no way to update only one/some of them. In an SQL editor you can manually edit one of those records, but when the update command is sent to the mysql - it will either fail, or update all identical records instead of that one record.




回答3:


By default MySQL (InnoDB engine) uses primary key to determine the order in which the data is physically stored in the main data file. If there is no primary key MySQL will automatically add a hidden AUTOINCREMENT column to act as pkey. This might cause performance issues because during inserting the unique autoincrement value acts like a global lock for all inserts. Also primary keys are used to associate all of a table’s indexes with the main data file. So the primary key is replicated in every row of every index.




回答4:


Its not a REQUIREMENT, but if you may ever in the future reference any data in this table, its a very good idea to have one as a primary key.




回答5:


ID is not a requirement, and neither is primary key, but it is a fundamental concept of the relational database model. You wouldn't usually want to try and use name as a key, for example.

also found : ID fields in SQL tables: rule or law?




回答6:


By "ID", I think you mean "identifier" - something that uniquely identifies a row in a database table. They are also called keys, which is the more concise technical term used in database design.

Implementing keys achieves at least two important things: keys prevent redundant and therefore potentially anomalous data from getting into your table; keys ensure that consumers of the data can accurately identify, use and update the information in the table. For these reasons, yes, you ought to create keys in your tables.



来源:https://stackoverflow.com/questions/5187643/do-mysql-tables-need-an-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!