MySql-默认情况下主键是唯一的吗? [英] MySql - Is primary key unique by default?
问题描述
如果我在MySql中将一列定义为主键,则默认情况下它也是唯一键还是需要将其定义为唯一键(如果我希望它是唯一的)?
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)?
我看到了这个问题 b有什么区别/w主键和唯一键可以解释两者之间的区别,但不能完全回答我的问题. PK默认是英国还是我需要对其进行明确定义.
I saw this question What is the difference b/w Primary Key and Unique Key that 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.
推荐答案
Primary key is always unique in every SQL. You dont have to explicitly define it as 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.
示例:
以EmpID作为主键而EmpPhoneNo作为唯一键的员工详细信息表.
An employee details table having EmpID as Primary key and EmpPhoneNo as unique key.
这篇关于MySql-默认情况下主键是唯一的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!