数据库表可以没有主键吗?

编程入门 行业动态 更新时间:2024-10-28 21:28:48
本文介绍了数据库表可以没有主键吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有人可以告诉我关系数据库中的表(例如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.

更多推荐

数据库表可以没有主键吗?

本文发布于:2023-10-22 08:51:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1517035.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主键   数据库

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!