我使用的是MySQL数据库。

在哪些情况下我应该创建唯一键或主键?


当前回答

Think the table name is employe. Primary key Primary key can not accept null values. primary key enforces uniqueness of a column. We can have only one Primary key in a table. Unique key Unique key can accept null values. unique key also enforces uniqueness of a column.you can think if unique key contains null values then why it can be unique ? yes, though it can accept null values it enforces uniqueness of a column. just have a look on the picture.here Emp_ID is primary and Citizen ID is unique. Hope you understand. We can use multiple unique key in a table.

其他回答

Unique key :- It should be used when you have to give unique value.In the case of unique key it means null values are also allowed.Unique keys are those keys which are unique and non similar in that column like for example your pet name.it can be nothing like null and if you are asking in context of database then it must be noted that every null is different from another null in the database.EXCEPT-SQL Server where null=null is true


主键:— 它应该在必须唯一标识一行时使用。Primary是一个键,它对数据库约束中的每一行都是唯一的,它不允许它为空。你可能看到数据库中有一个列是自动递增的它是表的主键。另外,它可以在另一个表中用作外键。例如订单表中的orderId,账单表中的billId。 现在回到什么情况下使用它:-

1)主键在哪个列可以 在表中不是空,并且在另一个表中用作外键 用于创建关系的表

2)唯一键在表中所在 不影响在表中或在整个数据库中 对于特定列为Null,比如餐馆里的小吃 可能你在餐馆里不带零食

Primary Key Unique Key
A primary key can't accept NULL values Unique key can accept NULL values, so problematic in the context of being unique
A primary key cannot contain duplicate values A unique key also cannot contain duplicate values
We can have only one primary key in a table We can have more than one unique key in a table
We can make a primary key from one or more table fields We can also make a unique key from one or more table fields
By default, a primary key creates a clustered index By default, a unique key creates a non-clustered unique index
It is used to identify each record in the table It prevents storing duplicate entries in a column

主键的主要特征是:

它必须为每一行数据包含一个惟一的值。 不能包含空值。 一个表中只能有一个主键。

唯一键的主要特征是:

它还可以为每一行数据包含一个惟一的值。

它也可以包含空值。

一个表中有多个唯一键。

主键

主键的主要目的是提供一种方法来标识表中的每条记录。

主键提供了一种使用行中的数据标识行的方法。主键可以基于一个或多个列,比如名字和姓氏;但是,在许多设计中,主键是从标识列自动生成的数字。

主键有以下特征:

一个表只能有一个主键。 主键由一个或多个列组成。 主键强制表的实体完整性。 所有定义的列必须定义为NOT NULL。 主键唯一地标识一行。 默认情况下,主键会导致聚集唯一索引。

独特的钥匙

唯一键也称为唯一约束。可以使用唯一约束来确保数据库中的行是唯一的。

我们不是已经对主键做过了吗?是的,我们这样做,但一个表可能有几组列,你想要唯一的。

在SQL Server中,唯一密钥具有以下特征:

一个表上可以定义多个唯一键。 默认情况下,唯一键会导致非聚集的唯一索引。 一个或多个列组成一个唯一键。 列可以为NULL,但允许每个列上有一个NULL。 外键约束可以引用唯一的约束。

来源:这里

我知道这个问题已经有好几年了,但我想提供一个答案,解释为什么而不是如何

主键的作用:在数据库中唯一地标识一行=>一行表示表建模的实体类型的单个实例。主键强制实体的完整性,即实体完整性。主键将是一个聚集索引,即它定义了数据在表中物理存储的顺序。

Purpose of Unique Key: Ok, with the Primary Key we have a way to uniquely identify a row. But I have a business need such that, another column/a set of columns should have unique values. Well, technically, given that this column(s) is unique, it can be a candidate to enforce entity integrity. But for all we know, this column can contain data originating from an external organization that I may have a doubt about being unique. I may not trust it to provide entity integrity. I just make it a unique key to fulfill my business requirement.

好了!