MySQL CREATE TABLE语句中的PRIMARY KEY定义 [英] PRIMARY KEY definition in MySQL CREATE TABLE statement
问题描述
这段代码有什么区别?
CREATE TABLE samples (
sampleid INT(11) NOT NULL AUTO_INCREMENT,
sampledate DATE NOT NULL,
location VARCHAR(25) NOT NULL,
PRIMARY KEY (sampleid)
)
ENGINE=InnoDB;
和这个:
CREATE TABLE samples (
sampleid INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
sampledate DATE NOT NULL,
location VARCHAR(25) NOT NULL,
)
ENGINE=InnoDB;
代码?
因此,单独的PRIMARY KEY语句或作为列定义的一部分.列定义中对UNIQUE INDEX和UNIQUE关键字有相同的问题.
So a separate PRIMARY KEY statement or as part of a column definition. Same question for UNIQUE INDEX and UNIQUE keyword in column definition.
推荐答案
第二种语法仅仅是一种快捷方式,允许您指定列并在单个子句中为其添加索引.
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.
这篇关于MySQL CREATE TABLE语句中的PRIMARY KEY定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!