MySql - 默认情况下主键是唯一的吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30911898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:06:33  来源:igfitidea点击:

MySql - Is primary key unique by default?

mysqlprimary-keyunique-key

提问by Roee Gavirel

If I define a column as a primary key in MySql, is it also unique key by default or do I need to also define it as unique key (in case I want it to be unique)?

如果我将一列定义为 MySql 中的主键,默认情况下它也是唯一键还是我还需要将其定义为唯一键(以防我希望它是唯一键)?

I saw this question What is the difference b/w Primary Key and Unique Keythat explain the difference between the two but doesn't exactly answer my question. Does PK is UK by default or I need to explicitly define it.

我看到这个问题黑白主键和唯一键什么区别,它解释了两者之间的区别,但并没有完全回答我的问题。默认情况下PK是英国还是我需要明确定义它。

回答by Rahul Tripathi

Primary key is always uniquein every SQL. You dont have to explicitly define it as UNIQUE.

主键在每个 SQL 中始终是唯一的。您不必将其明确定义为 UNIQUE。

On a side note:You can only have onePrimary key in a table and it never allows null values. Also you can have only one primary key constraint in the table(as the point of creating a primary key is to uniquely identify the row in your table) but you can more than one unique key constraint in your table.

附带说明:表中只能有一个主键,并且它永远不允许空值。此外,表中只能有一个主键约束(因为创建主键的目的是唯一标识表中的行),但表中可以有多个唯一键约束。

Example:

例子:

An employee details table having EmpID as Primary key and EmpPhoneNo as unique key.

以 EmpID 作为主键和 EmpPhoneNo 作为唯一键的员工详细信息表。

回答by Jakub Matczak

Primary key is always unique by definition. Not only in MySQL. So you don't need any additional unique key.

根据定义,主键始终是唯一的。不仅在 MySQL 中。所以你不需要任何额外的唯一键。

回答by Skippy le Grand Gourou

Note that composite keys may lead to confusion?: indeed a primary key can be a composite key, and DESCRIBEwill show all of the composite key components as primary keys?:

请注意,复合键可能会导致混淆?:确实主键可以是复合键,并将DESCRIBE所有复合键组件显示为主键?:

> DESCRIBE foobar;
+----------------------+------------------+------+-----+---------+-------+
| Field                | Type             | Null | Key | Default | Extra |
+----------------------+------------------+------+-----+---------+-------+
| column_A             | int(10) unsigned | NO   | PRI | NULL    |       |
| column_B             | int(10) unsigned | NO   | PRI | NULL    |       |
+----------------------+------------------+------+-----+---------+-------+

However SHOW CREATE TABLEwill show the reality?:

然而SHOW CREATE TABLE会显示现实吗?:

> SHOW CREATE TABLE foobar;
+--------+---------------------------…+
| Table  | Create Table              …|
+--------+---------------------------…+
| foobar | CREATE TABLE `foobar` (
  `column_A` int(10) unsigned NOT NULL,
  `column_B` int(10) unsigned NOT NULL,
  PRIMARY KEY (`column_A`,`column_B`),
  KEY `column_B` (`column_B`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+--------+---------------------------…+