简体   繁体   English

如何主键只能是一个表中的一个

[英]How Primary key can be only one in a table

After googling a lot, I found 2 statements for Primary Key. 经过大量搜索之后,我发现了2条关于主键的语句。

Some says, Primary key can be only one in a table. 有人说,主键只能是表中的一个。

Some says, Primary key can consists of more than one column. 有人说,主键可以包含多个列。

And also says, it is the difference between Primary key and Unique key. 并且还说,这是主键和唯一键之间的区别。

I read this post: difference between primary key and unique key 我读了这篇文章: 主键和唯一键之间的区别

But, this post also confuses this difference. 但是,这篇文章也混淆了这种差异。

My Question is: 我的问题是:

When Primary key can consists of multiple columns, then how can it be only 
1 for a table and then  how this can differ from Unique Key except from   
null value difference?

A primary key is used by the optimiser to create a clustered index around that key. 优化程序使用主键在该键周围创建聚簇索引。 If that key happens to be a combination of columns (for instance in a table that handles a many to many relationship) then that is fine. 如果该键碰巧是列的组合(例如,在处理多对多关系的表中),那很好。 A unique key is an attribute of something that is unique to that thing but not an attribute that you want to use as the primary key. 唯一键是某事物的唯一属性,而不是您要用作主键的属性。

One example that i work with is cars. 我工作的一个例子是汽车。 The vin is unique to the car. VIN是汽车特有的。 However because the vin and body number are used at different stages in the vehicles life. 但是,由于在车辆寿命的不同阶段使用了车辆号和车身号。 Whilst it is unique to the car, it isn't a good primary key candidate. 虽然它是汽车特有的,但它并不是很好的主键候选。 So we have a unique identifier per car as a primary key. 因此,我们有每辆车唯一的标识符作为主键。

Similarly we handle user group tables with primary keys over multiple columns. 同样,我们使用多列上的主键来处理用户组表。 So the combination of the userid and the usergroupid is a primary key preventing the entry of people in to the same group more than once. 因此,userid和usergroupid的组合是一个主键,可以防止人们多次进入同一组。 The use of a primary key here rather than a unique key is to conform to the 3rd normal form mostly. 这里使用主键而不是唯一键主要是为了符合第三范式。

I hope that helps but if you would like further clarification please let me know. 希望对您有所帮助,但如果您想进一步澄清,请告诉我。

You can have only one primary key in a table, but it can consist of multiple columns. 一个表中只能有一个主键,但是它可以由多个列组成。 This doesn't mean that each column is a primary key, but that the combination of all these columns' values are unique. 这并不意味着每一列都是主键,而是所有这些列的值的组合都是唯一的。

A unique constraint is similar to a primary in preventing duplicate values, but a unique constraint allows null s (since they aren't values). 唯一约束与防止重复值的主要约束类似,但是唯一约束允许null (因为它们不是值)。 A primary key does not. 主键则没有。 You could think of a primary key as a unique not null constraint. 您可以将主键视为唯一的非null约束。

In SQL: Any number of sets of one or more columns can be declared UNIQUE. 在SQL中:一个或多个列的任意数量的集合都可以声明为UNIQUE。 One set of columns can be declared PK (PRIMARY KEY). 可以将一组列声明为PK(PRIMARY KEY)。 PK means UNIQUE NOT NULL. PK表示UNIQUE NOT NULL。 A set of one column is "simple" & a set of more than one is "composite". 一组单列是“简单的”,一组以上是“复合的”。

In standard SQL a UNIQUE column set has a distinct subrow value in every row--where NULL<>NULL. 在标准SQL中,UNIQUE列集在每行中都有一个不同的子行值-其中NULL <> NULL。 MySQL does that--but some DBMSs evaluate that "distinct" treating NULL equal to NULL. MySQL做到了-但是某些DBMS评估该“区别”对待NULL等于NULL。 Read the manuals. 阅读手册。

SQL DBMSs use UNIQUE & PK declarations for other things--typically for default INDEXes. SQL DBMS将UNIQUE和PK声明用于其他用途-通常用于默认INDEX。 INDEX is not standard SQL & is a different notion. INDEX不是标准的SQL,而是另一种概念。 Read the manuals. 阅读手册。

PK & other terms have different definitions in the RM (relational model)--even for tables that have an obvious interpretation as relations because they have no duplicate rows & no NULLs. PK和其他术语在RM(关系模型)中具有不同的定义-即使对于表具有明显解释为关系的表,因为它们没有重复的行且没有NULL。 Read some published academic textbook(s). 阅读一些出版的学术教科书。

There's no need for PK in SQL or the RM--it's just a tradition. 不需要SQL或RM中的PK,这只是一个传统。

Re related terms in SQL & the RM. SQL和RM中的相关术语。

Primary keys can consist of any number of columns. 主键可以包含任意数量的列。 They then have three properties: 然后,它们具有三个属性:

  • The combination of values in the columns is unique. 列中值的组合是唯一的。
  • The values in each column are never NULL . 每列中的值永远不会为NULL
  • There is only one primary key per table. 每个表只有一个主键。

The first two conditions can be true on any number of columns or combinations of columns in a table. 前两个条件在表中的任何数量的列或列组合上都可以成立。 These are called candidate primary keys. 这些称为候选主键。 You would implement them with unique and not null constraints. 您将使用unique not null约束来实现它们。

The third condition is simply states that at most one of these candidates can be chosen as the primary key. 第三个条件只是简单地说,这些候选者中最多可以选择一个作为键。

One way to think about this is that the primary key is special. 考虑这一点的一种方法是主键是特殊的。 For example, in MySQL, the data is actually ordered on the data pages by primary key (this is called a clustered index ). 例如,在MySQL中,数据实际上是通过主键在数据页上排序的(这称为聚簇索引 )。 However, nothing in SQL in general requires that the primary key also be a clustered index, and not all databases implement this. 但是,在SQL中,通常没有什么要求主键也必须是聚集索引,并且并非所有数据库都可以实现此目的。

There is no inconsistency in what you read 您阅读的内容没有矛盾之处

A primary key is "one or more columns, whose combination of values must appear on only one row" 主键是“一列或多列,其值的组合必须仅出现在一行上”

A primary key can be single column, and then the value must be unique. 主键可以是单列,然后值必须是唯一的。 It can be multiple columns, and all values considers together must be unique: 它可以是多列,并且所有值一起考虑必须是唯一的:

Key1, Key2
1   , A
1   , B
2   , A
2   , B

Here it's true that both key1 column and key2 column have repeated values but when we consider the combinations of key 1 and key 2, they are unique: 1A, 1B, 2A, 2B 在这里,key1列和key2列都具有重复的值是正确的,但是当我们考虑键1和键2的组合时,它们是唯一的:1A,1B,2A,2B

Regarding the PK vs Unique Key query, see this answer. 关于PK与唯一键查询,请参见此答案。 It goes into more detail about primary keys, the basic principle of which is that none of the columns making up the primary key allows a null value: What's wrong with nullable columns in composite primary keys? 它更详细地介绍了主键,其基本原理是组成主键的所有列都不允许使用空值: 复合主键中的可空列怎么了?

the main differences between a primary key and a unique key is the primary key main job is to uniquely identify a row in a table and the main job for a unique key is to allow you to place additional unique conditions on your columns 主键和唯一键之间的主要区别是主键主要工作是唯一地标识表中的行,唯一键的主要工作是允许您在列上放置其他唯一条件

For example say you have a employees table with employee ID as the primary key and a accountNumber.. the accountNumber would be set as the unique ID... so you wouldn't have the accountNumber set as the primary key incase accountNumber is generated by another organisation outside of your database.. 例如,假设您有一个employee表,其中employee ID为主键和一个accountNumber .. accountNumber将被设置为唯一ID ...,因此如果accountNumber是由生成的,则不会将accountNumber设置为主键数据库之外的另一个组织。

When Primary key can consists of multiple columns, then how can it be only 1 for a table and then how this can differ from Unique Key except from null value difference? 当主键可以由多个列组成时,一个表如何只能为1,那么除了唯一值与空值差异之外,它又如何与唯一键有所不同?

Its true that a table can only have one primary key. 的确,一个表只能有一个主键。 This key can contains multiple columns(combination of columns). 该键可以包含多个列(列的组合)。 That combination of columns is collective is a Primary Key. 列的组合是集体的,是主键。

Lets take an example 让我们举个例子

Table A 表A

Id, Clientid, Branchid, Name, Address, ....... ID,客户ID,分支ID,名称,地址...

Here I created a primary key with combination of "Id, Clientid, Branchid" 在这里,我创建了一个主键,其中包含“ Id,Clientid,Branchid”

so its is possible to have duplicate Id or duplicate CLientid or duplicate Branchid but its not possible to have the whole combination asa duplicate 因此可能有重复的ID或重复的CLientid或重复的Branchid,但不可能将整个组合作为重复的对象

for example 例如

Id    Clientid    Branchid  
1        1           1  
1        2           1  
1        1           2  
2        1           1

the above combination is true but below combination is not allowed as its a primary key and cann't have duplicate values 上面的组合是正确的,但是下面的组合不允许作为主键,并且不能有重复的值

Id    Clientid    Branchid  
 1        1           1  
 1        1           1  

and all columns as null is also not possible as primary key doesn't allow null 并且所有列都不为null也是不可能的,因为主键不允许null

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么一张数据库表可以只有一个主键? - Why can a database table have only one primary key? 如何在一个表上按名称排序,但仅返回唯一的主键? - How can I sort by a name on one table but return only a unique primary key? SQL中的表能否将多列作为仅引用另一表的一个主键的外键? - Can a table in SQL have multiple columns as foreign keys that refer only to one primary key of another table? 页面视图表:我只想每页每个ip一行,但只能有一个主键。 我该如何解决这个问题? - Page view table: I only want one row per ip per page, but can only have one primary key. How do I get around this? 表只有一列或添加数字主键? - Table with only one column or add a numeric primary key? 在SQL中,如何有两个不能完全相同的字段,只有一个是主键 - In SQL how can I have two fields that can't both be identical, only one is a primary key 一个表中的主键也可以作为主键出现在另一表中吗? - Can primary key in one table appear in another table as primary key as well? 如何使一个coulmn作为自动递增另一个可以在同一张表中的主键 - how to make one coulmn as auto increment other can primary key in the same table 当一个表的主键与另一个表的主键包含但不包含在一起时,如何连接表? - How to join tables when primary key of one table is not the same as but contained within the primary key of the other table? 如何使用 Hibernate 将一个表的主键用作另一个表的主键 - How to use the Primary Key of one table as Primary Key of another using Hibernate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM