MySQL CREATE TABLE 语句中的 PRIMARY KEY 定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7350401/
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:02:17  来源:igfitidea点击:

PRIMARY KEY definition in MySQL CREATE TABLE statement

mysql

提问by waanders

What's the difference between this code:

这段代码有什么区别:

CREATE TABLE samples (
  sampleid INT(11) NOT NULL AUTO_INCREMENT,
  sampledate DATE NOT NULL,
  location VARCHAR(25) NOT NULL,
  PRIMARY KEY (sampleid)
)
ENGINE=InnoDB;

and this:

和这个:

CREATE TABLE samples (
  sampleid INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  sampledate DATE NOT NULL,
  location VARCHAR(25) NOT NULL,
)
ENGINE=InnoDB;

code?

代码?

So a separate PRIMARY KEY statement or as part of a column definition. Same question for UNIQUE INDEX and UNIQUE keyword in column definition.

所以单独的 PRIMARY KEY 语句或作为列定义的一部分。列定义中 UNIQUE INDEX 和 UNIQUE 关键字的相同问题。

采纳答案by thomasrutter

The second syntax is merely a shortcut allowing you to specify the column and add an index on it in a single clause.

第二种语法只是一种快捷方式,允许您在单个子句中指定列并在其上添加索引。

This works out fine in cases where you simply want to create a column and add an index on it.

如果您只想创建一个列并在其上添加索引,这很有效。

You'll need to use the first syntax if you want to do something more complicated, such as adding an index based on multiple columns rather than a single column, or if you are adding or changing an index on an existing column; that is, you are not creating the column and the index on it at the same time.

如果您想做一些更复杂的事情,例如添加基于多列而不是单列的索引,或者如果您要添加或更改现有列的索引,则需要使用第一种语法;也就是说,您不会同时创建列和索引。

回答by htmldrum

MySQL allows uses the PRIMARY KEY directive to allow you to set the Primary Key dynamically. Supplying PRIMARY KEY as an argument to the constructor can only be called on creating the column. PRIMARY KEY(X), PRIMARY KEY(Y), PRIMARY KEY(Z) allows for changing the primary keys on subsequent queries.

MySQL 允许使用 PRIMARY KEY 指令来允许您动态设置主键。将 PRIMARY KEY 作为参数提供给构造函数只能在创建列时调用。PRIMARY KEY(X)、PRIMARY KEY(Y)、PRIMARY KEY(Z) 允许在后续查询中更改主键。

回答by user4826347

The way I see it is.. The first method is used to create composite keys. While the second method (more readable to me) is primarily used if there is only primary key in the table.

我看到的方式是.. 第一种方法用于创建复合键。如果表中只有主键,则主要使用第二种方法(对我来说更具可读性)。

The second method cannot be used if you want to implement composite key

如果要实现复合键,则不能使用第二种方法

回答by TIHan

They are literally the same. Here is a quick site that shows you the different ways (3) to do it. http://www.java2s.com/Code/SQL/Key/Defineanduseprimarykey.htm

它们实际上是相同的。这是一个快速站点,向您展示了执行此操作的不同方法 (3)。http://www.java2s.com/Code/SQL/Key/Defineanduseprimarykey.htm

回答by Icarus

There are many ways to skin a cat and above 2 examples are just 2 of them. They are identical. There's no difference.

给猫剥皮的方法有很多种,上面的两个例子只是其中的两个。它们是相同的。没有区别。