数据库表可以没有主键吗? [英] Can a database table be without a primary key?
问题描述
有人可以告诉我关系数据库中的表(例如MySQL/SQL SERVER)是否可以没有主键吗?
Can anyone tell me if a table in a relational database (such as MySQL / SQL SERVER) can be without a primary key?
例如,我可以有表day_temperature
,在其中注册temperature
和time
.我看不出要有这样的表的主键的原因.
For example, I could have table day_temperature
, where I register temperature
and time
. I don't see the reason to have a primary key for such a table.
推荐答案
从技术上讲,您可以声明这样的表.
Technically, you can declare such a table.
但是在您的情况下,应将time
设置为PRIMARY KEY
,因为同时具有不同的温度可能是错误的,并且多次具有相同的温度可能是没有用的.
But in your case, the time
should be made the PRIMARY KEY
, since it's probably wrong to have different temperatures for the same time and probably useless to have same more than once.
从逻辑上讲,每个表都应有一个PRIMARY KEY
,以便您可以区分两个记录.
Logically, each table should have a PRIMARY KEY
so that you could distinguish two records.
如果您的数据中没有候选键,只需创建一个替代键(AUTO_INCREMENT
,SERIAL
或数据库提供的任何替代键).
If you don't have a candidate key in you data, just create a surrogate one (AUTO_INCREMENT
, SERIAL
or whatever your database offers).
没有PRIMARY KEY
的唯一借口是日志或类似的表,该表受DML
的影响很大,并且具有索引会影响性能,超出容忍度.
The only excuse for not having a PRIMARY KEY
is a log or similar table which is a subject to heavy DML
and having an index on it will impact performance beyond the level of tolerance.
这篇关于数据库表可以没有主键吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!