简体   繁体   English

没有主键的数据库表

[英]Database Table without Primary Key

I have a table used for a message board (See below). 我有一个用于留言板的桌子(见下文)。 I know best practices dictate creating a Primary Key but I can see no reason to create one. 我知道最佳实践决定创建Primary Key但是我看不出要创建Primary Key理由。 I will be searching mainly on ( UID, GRP_ID ) and will create an index on this. 我将主要在( UID, GRP_ID )上进行搜索UID, GRP_ID并在此上创建索引。 Deleting will be based on Last_timestp . 删除将基于Last_timestp In this scenario, should there be a PK ? 在这种情况下,应该有PK吗?

CREATE TABLE CP.CHAT
(UID BIGINT NOT NULL, GRP_ID BIGINT NOT NULL, CHAT VARCHAR(200) NOT NULL, LAST_TIMESTP TIMESTAMP);

Primary keys are not required, and your table will work just fine. 主键不是必需的,您的表也可以正常工作。

Here are some reasons why I almost always use primary keys. 我几乎总是使用主键的一些原因如下。

  1. They let you uniquely target a row. 它们使您可以唯一地定位一行。 If you have multiple rows with the exact same data, it can be tedious to delete it out. 如果您有多行数据完全相同的数据,则将其删除可能很繁琐。

  2. They provide an implicit ordering to the table. 它们为表提供隐式排序。 If you are troubleshooting your database, the sequence of the keys tells you the order they were created. 如果要对数据库进行故障排除,则按键顺序会告诉您它们的创建顺序。

Ultimately, the room taken up by PK are not going to be a huge overhead for the database in terms of storage space. 最终,就存储空间而言,PK占用的空间不会为数据库带来巨大的开销。 It won't hurt to have it, but it could help to have it. 拥有它不会有任何伤害,但是拥有它会有所帮助。

I see no need to tell you what a Primary Key is and what it is used for. 我认为不需要告诉您什么是主键以及它的用途。 It seems you already know it, and figured out the structure just fine. 看来您已经知道了,并弄清楚了结构。

The thing is, if you won't have any other tables which depends on this table, then your table setup will be sufficent for you. 关键是,如果您没有任何其他依赖于该表的表,那么您的表设置就足够了。 As you already have a UID column there, I say I would make that a PK, since it won't cost you much in terms of storage, but also could help you sort your table (by auto-incrementing integers) and would work as a selective for delete and update operations in the future. 由于您已经在此处拥有一个UID列,我想说这是一个PK,因为它在存储方面不会花很多钱,而且还可以帮助您对表进行排序(通过自动递增整数),并且可以有选择地用于将来的删除和更新操作。

Still couldn't figure out how you'll manage to delete records by Last_timestp values, though... 仍然无法弄清楚如何通过Last_timestp值删除记录...

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

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