且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MySQL表结构,我需要主键吗?

更新时间:2023-11-24 12:52:16

  1. 您应该使用主键.没有理由避免主键

Doc

表的主键表示一列或一组列您在最重要的查询中使用的.它有一个关联的索引,以实现快速查询性能.查询性能受益于 NOTNULL 优化,因为它不能包含任何 NULL 值.随着InnoDB 存储引擎,表数据是物理组织起来的基于主键列的超快速查找和排序或列.

The primary key for a table represents the column or set of columns that you use in your most vital queries. It has an associated index, for fast query performance. Query performance benefits from the NOT NULL optimization, because it cannot include any NULL values. With the InnoDB storage engine, the table data is physically organized to do ultra-fast lookups and sorts based on the primary key column or columns.

  1. 如果要存储特定值,则应使用日期时间字段,否则应使用时间戳.例如,您希望在未来更新记录,其数据类型应该加时间戳,因为时间戳会自动为您更改