主键和唯一键有什么区别
我试图在谷歌中找到它,但没有给出令人满意的答案。谁能解释一下明显的区别。
实际上,如果使用主键来唯一地选择数据,那么还需要唯一键吗?
什么时候应该使用主键,什么时候应该使用唯一键?
I tried to find it out in google but not satisfactory answer is given out there. Can anybody explain the solid difference.
actually if Primary key is used to select data uniquely then what is the need of Unique key?
When should I use a Primary key and when to use a Unique key?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
主键和唯一键用于不同的用途 - 了解它们的用途将帮助您决定何时使用它们。
主键用于标识表中的一行数据。每当您需要引用特定行时就可以使用它,例如。在其他表中或通过应用程序代码等。为了标识行,PK 的值必须是唯一的。此外,它们不能为 null,因为大多数 DBMS 将 null 视为不等于 null(因为 null 通常意味着“未知”)。一张桌子只能有一个PK。数据库中的所有表都应该有一个 PK(尽管大多数 DBMS 不强制执行),并且 PK 可以跨越多个列。
唯一键约束用于确保数据库中两行中的数据不重复。数据库中的一行允许唯一键约束的值为空。尽管表应该有 PK,但它不需要任何额外的唯一键。然而,如果满足您的需要,表可以有多个唯一键。与 PK 一样,唯一键可以跨越多个列。
还值得了解的是,默认情况下,许多 DBMS 索引和物理顺序表在磁盘上使用 PK。这意味着通过 PK 查找值比连续使用其他值更快。但是,通常情况下,您可以根据需要覆盖此行为。
Primary Key and Unique Key are used for different things - understanding what they are for will help you decide when to use them.
The primary key is used to identify a row of data in a table. It is used whenever you need to refer to a particular row, eg. in other tables or by application code etc. In order to identify a row, the values of a PK must be unique. Furthermore, they can't be null, because most DBMS treat null as not equal to null (since null typically means "unknown"). A table can only have one PK. All tables in your database should have a PK (although this is not enforced by most DBMS), and PK can span multiple columns.
Unique key constraints are used to ensure that data is not duplicated in two rows in the database. One row in the database is allowed to have null for the value of the unique key constraint. Although a table should have a PK, it need not have any additional unique keys. However, tables can have more than one unique key if that meets your needs. Like PKs, unique keys can span multiple columns.
It is also worth knowing that, by default, many DBMS index and physically order tables on disk using the PK. This means that looking up values by their PK is faster than using other values in a row. Typically, however, you can override this behaviour if required.
术语“唯一密钥”既含糊又重复。在关系模型中,“键”表示候选键,根据定义,它无论如何都是唯一的。主键只是关系的任何一个候选键。因此,“唯一键”的含义与“候选键”完全相同,而“候选键”的含义与“主键”完全相同。没有什么区别。
然而,SQL 有一种称为 UNIQUE 约束的东西,它与 SQL PRIMARY KEY 约束略有不同 - 两者都强制唯一性,但 PRIMARY KEY 每个表只能使用一次。 UNIQUE 约束也允许空值,而 PRIMARY KEY 约束则不允许。
因此,可能令人困惑的术语“唯一键”最常用于表示由唯一约束强制执行的键。它甚至可能用于表示可空列上的 UNIQUE 约束,尽管在我看来,该术语的使用非常可疑,因为一组包含空值的列不能作为候选键,因此使用“key”一词来表示可空列并不真正正确,必然会引起混乱。
The term "unique key" is both ambiguous and tautologous. In the relational model, a "key" means a candidate key, which by definition is unique anyway. A primary key is just any one of the candidate keys of a relation. Therefore "unique key" means exactly the same as "candidate key" which means exactly the same as "primary key". There is no difference.
However, SQL has something called a UNIQUE constraint which is subtly different to a SQL PRIMARY KEY constraint - both enforce uniqueness but PRIMARY KEY can only be used once per table. UNIQUE constraints also allow nulls whereas PRIMARY KEY constraints don't.
So the potentially confusing term "unique key" is most often used to mean a key enforced by a UNIQUE constraint. It might even be used to mean a UNIQUE constraint on nullable columns, although that's a very dubious use of the term in my opinion because a set of columns that include nulls cannot be a candidate key so the use of the word "key" for nullable columns isn't really correct and is bound to cause confusion.
在讨论主键和唯一键之间的区别之前,重要的是要确定什么是键,它在业务中如何发挥作用以及它在SQL / Oracle中如何实现等。
根据业务前景:
对于一个组织或企业来说,有很多物理实体(例如人员、资源、机器等)和虚拟实体(他们的任务、事务、活动)。
通常,业务需要记录和处理这些业务实体的信息。
这些业务实体在整个业务域内通过Key来标识。
根据 RDBMS 的预期:
键(又名候选键),唯一标识实体的一个值或一组值。对于数据库表,存在很多键并且可能符合主键条件。
这样所有的键、主键、唯一键等统称为候选键。
对于一个表,DBA选择的候选键称为主键,其他候选键称为辅助键。
主键和唯一键的区别
1.行为:主键用于标识表中的行(记录),而唯一键用于防止列中的重复值。
2.索引:默认情况下,Sql 引擎在主键上创建聚集索引(如果不存在),并在唯一键上创建非聚集索引。
3.可空性:主键不包含空值,而唯一键可以。
4.存在性:一张表最多可以有一个主键,但可以有多个Unique-key。
5.可修改性:您无法更改或删除主值,但唯一键值可以。
有关示例的更多信息:
http://dotnetauthorities.blogspot.in/2013/11/Microsoft-SQL-Server-Training-Online-Learning-Classes-Integrity-Constraints-PrimaryKey-Unique-Key_27.html
Before discussing about difference between Primary Key and Unique Key, it is important to determine what is key, how it plays a role in business and how it is implemented in SQL / Oracle etc.
As per business prospective:
For an organization or a business, there are so many physical entities (such as people, resources, machines etc.) and virtual entities (their Tasks, transactions, activities).
Typically, business need to record and process information for those business entities.
These business entities are identified within whole business domain by a Key.
As per RDBMS prospective:
Key(a.k.a Candidate Key), a value or set of values that uniquely identifies entity. For a Db-Table, there are so many keys are exists and might be eligible for Primary Key.
So that all keys, primary key, unique key, etc are collectively called as Candidate Key.
For a table DBA selected Candidate Key is called Primary Key, other candidate keys are called secondary keys.
Difference between Primary Key and Unique key
1. Behavior: Primary Key is used to identify a row (record) in a table whereas Unique-key is to prevent duplicate values in a column.
2. Indexing: By default Sql-engine creates Clustered Index on primary-key if not exists and Non-Clustered Index on Unique-key.
3. Nullability: Primary key does not include Null values whereas Unique-key can.
4. Existence: A table can have at most one primary key but can have multiple Unique-key.
5. Modifiability: You can’t change or delete primary values but Unique-key values can.
For more information with examples:
http://dotnetauthorities.blogspot.in/2013/11/Microsoft-SQL-Server-Training-Online-Learning-Classes-Integrity-Constraints-PrimaryKey-Unique-Key_27.html
主键是唯一的键。两种类型的键都用于唯一标识表中的单行。出于多种不同的实现原因,许多 RDBMS 要求将表中的唯一键之一指定为“主键”。就数据完整性而言,没有区别。
A primary key is a unique key. Both types of key serve to uniquely identify a single row in a table. Many RDBMSs require that one of the unique keys on a table be designated as the "primary key", for several different implementation reasons. In terms of data integrity, there is no difference.
主键不允许为空,唯一键允许一个空(在 sql server 上,在 Oracle 上允许多个空)
一张表只能有一个主键,但当
您要设置外键关系时,许多唯一键都使用主键
这是一个小示例,每个表中只有一列
在主键表中插入一行
在外键表中插入一行主键表中存在值的键表
现在这将失败,因为主键表中不存在值 2
Msg 547,级别 16,状态 0,第 1 行
INSERT 语句与 FOREIGN KEY 约束“FK_ForeignTest_PrimaryTest”冲突。冲突发生在数据库“aspnetdb”、表“dbo.PrimaryTest”、列“id”中。
该声明已终止。
A primary key does not allow nulls, a unique key will allow one null (on sql server and multiple nulls on Oracle)
A table can only have one primary key but many unique keys
use primary keys when you want to set up foreign key relationships
Here is a small example with just one column in each table
insert a row in the primary key table
insert a row in the foreign key table with a value that exist in the primary key table
Now this will fail because value 2 does not exist in the primary key table
Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ForeignTest_PrimaryTest". The conflict occurred in database "aspnetdb", table "dbo.PrimaryTest", column 'id'.
The statement has been terminated.
再举一个例子:
想象一个保存用户数据的表,其中每个用户都有一个电子邮件地址。两个用户不能拥有相同的电子邮件地址,因此该列成为唯一键。虽然它可能是主键(我从未将字符串作为主键),但它不一定是。
Just to add another example:
Think of a table holding user data, where each user has an email address. No two users can have the same email address, so that column becomes a unique key. While it could be the primary key (I have never made a string a primary key), it doesn't have to be.
两者都代表表中行的唯一标识,但有一点区别:
而
这是主要的区别..
Both are representing a unique identification to a row in a table but there is little bit difference is that
while
that is the main difference ..
主键约束
1、主键不能为空。
2. 不允许有多个主键。
3. 在某些 RDBMS 上,主键默认生成聚集索引。
唯一约束
1. 可以在允许空值的列上定义唯一约束。
2. 允许有多个唯一键。
3. 在某些 RDBMS 上,唯一键默认生成非聚集索引。
来源维基百科
Primary Key constraint
1. A primary key cannot allow null.
2. Multiple primary keys are NOT allowed.
3. On some RDBMS a primary key generates a clustered index by default.
Unique constraint
1. A unique constraint can be defined on columns that allow nulls.
2. Multiple unique keys are allowed.
3. On some RDBMS a unique key generates a nonclustered index by default.
Source Wikipedia
NOT NULL 表示该特定列中的任何条目都不应为空。
UNIQUE 意味着列中的每个条目都应该是不同的。
PRIMARY KEY 表示列中的任何条目都应该是不同的且不为空。
那么简单..
NOT NULL means Any entry in that particular column should not be null.
UNIQUE means Each entry in the column should be distinct.
PRIMARY KEY means Any entry in the column should be distinct and not null.
So simply..
每个表可以有多个 UNIQUE 约束,但每个表只能有一个 PRIMARY KEY 约束。更多信息可以在此处找到
you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table. More info can be found here
唯一键将与其他键一起使用,而主键不与任何其他键一起使用。
使用主键时无需与任何其他键进行任何关联。
A unique key will served with other key while a primary key does not serve any other key with it.
The primary key is used without any association of any other key.