为什么不允许在表中使用两个主键? [英] Why two primary keys in a table is not allowed?

查看:69
本文介绍了为什么不允许在表中使用两个主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解MySql语法为什么不允许在同一表中使用两个主键的原因.我了解复合键,但与此无关.

I want to understand the reasoning behind why two primary keys are not allowed in same table by grammar of MySql. I understand composite key and this is not related to that.

我无法理解为什么不允许在同一表中使用两个单独的主键.但是我们可以将一列作为主键,将另一列作为唯一键而不是null.实际上,它具有两个不同的主键,因为我们可以使用这些键中的任何一个来清楚地标识行.为什么第一个不允许,而第二个不允许?

I could not understand why two separate primary key in same table is not allowed. But we can have one column as primary key and other column as unique and not null. Which practically acts as having two different primary keys as we can distinctly identify a row using any of these keys. Why the first one is not allowed but the later is?

我试图弄清楚逻辑,但偶然发现了Codd的12条规则中的规则2

I was trying to figure out the logic but stumbled upon Rule 2 of Codd's 12 rules which says

通过使用表名,主键值和列名的组合,可以确保在关系数据库中的每个数据(原子值)在逻辑上都是可访问的.

Each and every datum (atomic value) in a relational data base is guaranteed to be logically accessible by resorting to a combination of table name, primary key value and column name.

表名,主键值和列的组合对于单个基准值是否应该是唯一的,或者我们可以有多种组合来保证访问吗?为什么或者为什么不?

Does combination of table name, primary key value and column should be unique for a single datum value or can we have multiple combinations which guarantees access? Why or why not?

由于该问题被确定为可能重复,因此我将在下面解释它的不同之处其他类似的问题是

Since the question was identified as possible duplicate, I will explain below how it is different Other similar question was

我可以在一个表中有多个主键吗?

Can I have multiple primary keys in a single table?

答案是.

我的问题是为什么?这是什么原因?

科德打算根据第二条规则说什么?

如果允许使用多个单独的主键会发生什么问题?

推荐答案

您可以在数据库系统允许的范围内拥有任意数量的UNIQUE KEY约束,许多关系主义者现在认为将提升是一个错误.>这些键并将其涂为PRIMARY.

You can have as many UNIQUE KEY constraints as your database system allows, and many relational purists now see it as a mistake to elevate one of these keys and anoint it as PRIMARY.

从逻辑上讲,唯一键约束和主键都实现相同的目的-定义可唯一标识行的列子集.它们还应该有资格作为外键约束的目标.

Logically, both unique key constraints and primary keys fulfil the same purposes - defining a subset of columns by which rows may be uniquely identified. They also should be eligible as targets for foreign key constraints.

某些默认值(例如可空性)由主键约束自动应用,但是没有理由不能将相同的约束手动应用于唯一键约束中包含的列.

Certain defaults (such as nullability) are automatically applied by primary key constraints but there's no reason you cannot apply the same constraints manually to columns included in unique key constraints.

这篇关于为什么不允许在表中使用两个主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆