Can a database table be without a primary key?

前端 未结 11 556
谎友^
谎友^ 2021-01-31 14:39

Can anyone tell me if a table in a relational database (such as MySQL / SQL SERVER) can be without a primary key?

For example, I could have table day_temperature

11条回答
  •  闹比i
    闹比i (楼主)
    2021-01-31 15:18

    You don't need a PK, but it's recommended that you have one. It's the best way to identify unique rows. Sometimes you don't want an auto incremental int PK, but rather create the PK on something else. For example in your case, if there's only one unique row per time, you should create the PK on the time. It makes looks up based on time faster, plus it ensures that they're unique (you can be sure that the data integrity isn't violated):

提交回复
热议问题