简体   繁体   English

自动递增 ID 作为所有表的主键

[英]Auto incrementing ID as primary key on all tables

I am designing a database for an e-Commerce website which uses MySQL.我正在为一个使用 MySQL 的电子商务网站设计一个数据库。 I have made the list of tables necessary and all the fields necessary for the table.我已经制作了必要的表格列表和表格所需的所有字段。 I have a total of 9 tables.我总共有9张桌子。

What I have done is include an auto incrementing ID as primary key on all the tables.我所做的是在所有表上包含一个自动递增 ID 作为主键。

All my tables except 2 are normalized to 3NF.除了 2 个表之外,我的所有表都标准化为 3NF。 Two tables 'users' and 'outlets' are not normalized to 2NF.两个表 'users' 和 'outlets' 没有标准化为 2NF。

Along the way I realized that normalization is cumbersome when using auto incrementing ID as a primary key.在此过程中,我意识到使用自动递增 ID 作为主键时规范化很麻烦。 As normalization is not strictly required, I want to know if there are any downsides of using auto incrementing ID as primary key on all the tables?由于不严格要求规范化,我想知道在所有表上使用自动递增 ID 作为主键是否有任何缺点?

Its good to use auto incrementing ID as primary key in all the table. 在所有表中使用自动递增ID作为主键很好。 This will help you to auto indexing data. 这将帮助您自动索引数据。 If you are planning to us any ORM (Doctrine2 as example) you must need primary key for each table. 如果您计划向我们提供任何ORM(例如Doctrine2),则每个表都必须有主键。

Your question is tagged for MySQL, so I'll point out that MySQL's InnoDB storage engine uses the primary key as its clustered index for all tables. 你的问题被标记为MySQL,所以我要指出MySQL的InnoDB存储引擎使用主键作为所有表的聚簇索引。

It's more efficient to query by the clustered index when possible. 在可能的情况下,通过聚簇索引进行查询会更有效。 But if you have an arbitrary rule that all tables must use an auto-increment primary key even if there is another column or set of columns that could serve as the primary key, and you always run queries searching by those columns and not by the auto-increment column, then you'll never gain the advantage of querying by the clustered index. 但是,如果您有一个任意规则,即使有另一列或一组列可以作为主键,所有表都必须使用自动增量主键,并且您始终运行按这些列而不是自动搜索的查询-increment列,那么你永远不会获得聚集索引查询的优势。

I have created a lot of tables in my time. 我的时间里创造了很多桌子。 I have used AUTO_INCREMENT in only 1/3 of them. 我只使用了1/3的AUTO_INCREMENT The rest had what seemed like a "perfectly good 'natural' PK", so I went that way. 剩下的就像是一个“非常好”的自然'PK',所以我走了那条路。

"Normal Form" is a textbook way to get you started. “普通表格”是一本让你入门的教科书。 In real life (in my opinion), NF later takes a back seat to performance and other considerations. 在现实生活中(在我看来),NF后来在性能和其他考虑方面退居二线。

For InnoDB tables, you really should have an explicit PRIMARY KEY (either auto_inc or natural). 对于InnoDB表,你真的应该有一个显式的 PRIMARY KEY (auto_inc或natural)。

A generic pattern where auto_inc slows things down is a many:many mapping table, as Renzo points out, and which I discuss here: http://mysql.rjweb.org/doc.php/index_cookbook_mysql#many_to_many_mapping_table 一个通用模式,其中auto_inc减慢了速度是一个很多:许多映射表,正如Renzo指出的那样,我在这里讨论: http//mysql.rjweb.org/doc.php/index_cookbook_mysql#many_to_many_mapping_table

In InnoDB, the PRIMARY KEY is stored (clustered) with the data, so the index structure (a BTree) occupies virtually no extra space. 在InnoDB中, PRIMARY KEY与数据一起存储(聚集),因此索引结构(BTree)几乎不占用额外的空间。 Each secondary index occupies a separate BTree that implicitly includes the PK column(s). 每个二级索引占用一个单独的BTree,隐含地包括PK列。

I agree with @Arun.我同意@Arun。 Use UUIDs instead of auto incrementing INT IDs.使用 UUID 而不是自动递增的 INT ID。 This allows for better caching of DB writes.这允许更好地缓存数据库写入。 If you create new records they would require a new ID.如果您创建新记录,它们将需要一个新 ID。 If the ID is allocated by the DB then the logic layer must talk to the DB first to get the newly incremented ID.如果 ID 是由 DB 分配的,那么逻辑层必须首先与 DB 对话以获取新增加的 ID。 UUIDs can be generated by the logic layer or even the front end without talking to the DB. UUID 可以由逻辑层甚至前端生成,而无需与 DB 对话。 Yes, it uses more bytes but the fact that you separate your layers from the DB is a win in my books是的,它使用了更多的字节,但是将层与数据库分开的事实在我的书中是一个胜利

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM