什么时候我应该使用键,主键,唯一键和索引?


当前回答

唯一密钥:

可以有多个值为空。 任何两个元组在唯一键中都不能有相同的值。 一个或多个唯一键可以组合成一个主键,反之则不行。

主键

可以包含多个唯一键。 唯一表示一个元组。

其他回答

主键不允许空值,但唯一键允许空值。

在一个表中只能声明一个主键,但是一个表可以有多个唯一键(列赋值)。

唯一密钥:

可以有多个值为空。 任何两个元组在唯一键中都不能有相同的值。 一个或多个唯一键可以组合成一个主键,反之则不行。

主键

可以包含多个唯一键。 唯一表示一个元组。

主键——我们只能在一个表中放入一个主键,当我们向表中输入值时,我们不能让该列为空。

唯一键——我们可以在一个表中放置多个唯一键,当我们向表中输入值时,我们可以将该列留空。 当我们应用主键和唯一键时,列取唯一值(不相同)。

唯一键:没有两行相似的列

Primary Key: Collection of minimum number of columns which can uniquely identify every row in a table (i.e. no two rows are similar in all the columns constituting primary key). There can be more than one primary key in a table. If there exists a unique-key then it is primary key (not "the" primary key) in the table. If there does not exist a unique key then more than one column values will be required to identify a row like (first_name, last_name, father_name, mother_name) can in some tables constitute primary key.

索引:用于优化查询。如果你要搜索或排序的基础上的一些列的结果很多次(例如。大多数人会通过名字而不是他们的卷号来搜索学生),那么如果列的值都被“索引”了,比如用二叉树算法,就可以优化。

主键用于处理不同的表。这是关系数据库的基础。如果你有一个图书数据库,最好创建2个表- 1)图书和2)作者,INT主键“id”。然后在books中使用id而不是作者的名字。

如果不希望有重复的条目,则使用唯一键。例如,您可能在图书表中有标题,并希望确保每个标题只有一个条目。