简体   繁体   中英

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. I will be searching mainly on ( UID, GRP_ID ) and will create an index on this. Deleting will be based on Last_timestp . In this scenario, should there be a 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. 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.

Still couldn't figure out how you'll manage to delete records by Last_timestp values, though...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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