UNIQUE和UNIQUE KEY mySQL之间的区别

时间:2016-04-28 21:00:32

标签: mysql database

UNIQUE和UNIQUE KEY有什么区别?哪个更好?

例如:

CREATE TABLE `table1` (
attr1 INT PRIMARY KEY AUTO_INCREMENT,
attr2 INT,
attr3 INT,
attr4 INT,
UNIQUE (attr3,attr4)
);

CREATE TABLE `table1` (
attr1 INT PRIMARY KEY AUTO_INCREMENT,
attr2 INT,
attr3 INT,
attr4 INT,
UNIQUE KEY `constraintName` (attr3,attr4)
);

我的问题特别是关于UNIQUE和UNIQUE KEY关键字的使用以及执行每个关键字的结果差异。

1 个答案:

答案 0 :(得分:2)

UNIQUE和UNIQUE KEY完全相同。 KEY运算符是语法定义中的非必要关键字:

create_definition:
    col_name column_definition
  | [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)
      [index_option] ...
  | {INDEX|KEY} [index_name] [index_type] (index_col_name,...)
      [index_option] ...
  | [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY]
      [index_name] [index_type] (index_col_name,...)
      [index_option] ...
  | {FULLTEXT|SPATIAL} [INDEX|KEY] [index_name] (index_col_name,...)
      [index_option] ...
  | [CONSTRAINT [symbol]] FOREIGN KEY
      [index_name] (index_col_name,...) reference_definition
  | CHECK (expr)

有关详细信息,请参阅documentation