表中有 2 个主键

发布于 2024-12-11 09:40:48 字数 239 浏览 0 评论 0原文

在数据库的表中设置主键就可以了。制作复合初级也很好。但为什么表中不能有 2 个主键呢?如果我们有2个主键可能会出现什么样的问题。

假设我有一个学生表。我不希望每个学生的卷号和姓名都是唯一的。那为什么我不能在一个表中创建2个主键呢?我现在看不出有什么逻辑问题。但我肯定错过了一个严重的问题,这就是它不存在的原因。

我是数据库新手,所以没有太多想法。它还可能会产生技术问题。如果有人能在这方面教育我,我会很高兴。

谢谢。

Making a primary key in a table in database is fine. Making a Composite Primary is also fine. But why cant I have 2 primary keys in a table? What kind of problems may occur if we have 2 primary keys.

Suppose I have a Students table. I don't want Roll No. and Names of each student to be unique. Then why can't I create 2 primary keys in a table? I don't see any logical problem in it now. But definitely I am missing a serious issue that's the reason it does not exist.

I am new in databases, so don't have much idea. It may also create a technical issue rather. Will be happy if someone can educate me on this.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夏九 2024-12-18 09:40:48

您可以为两列 UNIQUE(roll,name) 创建 UNIQUE 约束。

You can create a UNIQUE constraint for both columns UNIQUE(roll,name).

木落 2024-12-18 09:40:48

根据定义,PK 是唯一的,因为它用于从其他行中识别行,例如,当外键引用该表时,它就是引用 PK。

如果您需要另一列像 PK 一样“起作用”,请为其指定属性 uniquenot null

The PK is unique by definition, cause it is used to identify a row from the others, for example, when a foreign key references that table, it is referencing the PK.

If you need another column to 'act' like a PK, give it the attributes unique and not null.

潦草背影 2024-12-18 09:40:48

嗯,这只是根据定义。不能有两个“主要”条件,就像不能有两个“最新”版本一样。

每个表可以包含多个唯一键,但如果您决定有一个主键,那么这只是这些唯一键之一,您认为的“一个” “最重要”,它唯一地标识每条记录。

如果您有一个表并得出主键不能唯一标识每个记录的结论(也意味着不能有两个记录具有相同的主键值),则您选择了错误的主键,如下所示根据定义,主键的字段必须唯一地定义每条记录。

然而,这并不意味着不能有其他字段组合来唯一标识该记录!这是第二个功能发挥作用的地方:引用完整性。

您可以使用表的主键“链接”表。例如:如果您有一个 Customer 表和一个 Orders 表,其中 Customers 表的主键为客户编号,< code>Orders 表有一个关于订单号和客户编号的主键,这意味着:

  1. 每个客户都可以通过其客户编号唯一标识
  2. 每个订单都由订单号和客户编号唯一标识

您可以将两个表链接到客户编号上。然后,数据库系统会确保几件事,其中一个事实是,如果不先删除订单,您就无法删除数据库中有订单的客户。否则,您将无法找到客户数据而收到订单,这将违反数据库的引用完整性。

如果您有两个主键,系统将不知道使用哪个主键来确保引用完整性,因此您必须告诉系统使用哪个主键 - 这将使主键之一更加重要,这将使其成为“主键”(!)的主键。

Well, this is simply by definition. There can not be two "primary" conditions, just like there can not be two "latest" versions.

Every table can contain more than one unique keys, but if you decide to have a primary key, this is just one of these unique keys, the "one" you deem the "most important", which identifies every record uniquely.

If you have a table and come to the conclusion that your primary key does not uniquely identify each record (also meaning that there can't be two records with the same values for the primary key), you have chosen the wrong primary key, as by definition, the fields of the primary key must uniquely define each record.

That, however, does not mean there can be no other combination of fields uniquely identifying the record! This is where a second feature kicks in: referential integrity.

You can "link" tables using their primary key. For example: If you have a Customer table and an Orders table, where the Customers table has a primary key on the customer number and the Orders table has a primary key on the order number and the customer number, that means:

  1. Every customer can be identified uniquely by his customer number
  2. Every order is uniquely identified by the order number and the customer number

You can then link the two tables on the customer number. The DB system then ensures several things, among which is the fact that you can not remove a customer who has orders in your database without first removing the orders. Otherwise, you would have orders without being able to find out the customer data, which would violate your database's referential integrity.

If you had two primary keys, the system would not know on which to ensure referential integrity, so you'd have to tell the system which key to use - which would make one of the primary keys more important, which would make it the "primary key" (!) of the primary keys.

心凉怎暖 2024-12-18 09:40:48

一个表中可以有多个候选键,但按照惯例,每个表只有一个键称为“主”。但这只是一个约定,它对按键的功能没有任何真正的影响。主键与任何其他候选键没有什么不同。如果您发现将多个键称为“主要”键很方便,那么我建议您这样做。在我看来(我不是唯一一个),指定“主”键的想法本质上是一个过时的概念,在数据库设计中几乎不重要。

您可能有兴趣知道,有关关系数据库模型的早期论文(例如关系模型的发明者 EFCodd 所著)实际上使用术语“主键”来描述关系的所有键,而不仅仅是一。因此,每个表有多个主键是一个非常好的先例。精确指定一个主键的想法是最近出现的,并且可能通过 ER 建模技术的流行而得到普遍使用。

You can have multiple candidate keys in a table but by convention only one key per table is called "primary". That's just a convention though and it doesn't make any real difference to the function of the keys. A primary key is no different to any other candidate key. If you find it convenient to call more than one key "primary" then I suggest you do so. In my opinion (I'm not the only one) the idea of designating a "primary" key at all is essentially an outdated concept of very little importance in database design.

You might be interested to know that early papers on the relational database model (e.g. by E.F.Codd, the relational model's inventor) actually used the term "primary key" to describe all the keys of a relation and not just one. So there is a perfectly good precedent for multiple primary keys per table. The idea of designating exactly one primary key is more recent and probably came into common use through the popularity of ER modelling techniques.

浊酒尽余欢 2024-12-18 09:40:48

在第二个属性(名称)上创建唯一索引,它与具有另一个名称的主键几乎相同。

来自维基百科(http://en.wikipedia.org/wiki/Unique_key):

一张表最多可以有一个主键,但可以有多个唯一键
钥匙。主键是唯一指定的列的组合
一行。这是唯一键的特例。一个区别是
主键具有隐式 NOT NULL 约束,而唯一键则具有隐式 NOT NULL 约束
不是。因此,唯一键列中的值可能为 NULL,也可能不为 NULL,
事实上,这样的列最多可以包含一个 NULL 字段。
另一个区别是主键必须使用另一个定义
语法。

Create an unique index on the 2nd attribute (Names), it's almost the same as primary key with another name.

From Wikipedia (http://en.wikipedia.org/wiki/Unique_key):

A table can have at most one primary key, but more than one unique
key. A primary key is a combination of columns which uniquely specify
a row. It is a special case of unique keys. One difference is that
primary keys have an implicit NOT NULL constraint while unique keys do
not. Thus, the values in unique key columns may or may not be NULL,
and in fact such a column may contain at most one NULL fields.
Another difference is that primary keys must be defined using another
syntax.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文