MySQL:在CREATE TABLE语句中命名主键 [英] MySQL: Name primary key in CREATE TABLE statement
问题描述
在创建表时如何设置主键的名称?
How do I set the name of a primary key when creating a table?
例如,在这里我试图创建名称为'id'的主键,但这是无效的SQL.你能告诉我正确的方法吗?
For example here I'm trying to create a primary key with the name 'id', but this is invalid SQL. Can you tell me the correct way to do this?
CREATE TABLE IF NOT EXISTS `default_test`
(
`default_test`.`id` SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY `id`,
`default_test`.`name` LONGTEXT NOT NULL
)
澄清
我想指定主键的名称-而不是默认名称"PRIMARY",我希望将其命名为"id"或"primary_id",所以如果以后要运行SHOW,从default_test索引,索引名将是我指定的.
Clarification
I'd like to specify the name of the primary key - rather than the default name of "PRIMARY" I'd like it to be called "id" or perhaps "primary_id", so if I were to later run SHOW INDEXES FROM default_test, the Key_name will be something I have specified.
推荐答案
或者广泛支持:
CREATE TABLE IF NOT EXISTS `default_test` (
`default_test`.`id` SMALLINT NOT NULL AUTO_INCREMENT,
`default_test`.`name` LONGTEXT NOT NULL,
PRIMARY KEY (`id`)
)
更新
根据说明,如果要指定索引名称,则可以用以下内容替换上面的最后一个定义:
UPDATE
Based on the clarification, you could replace the last definition above with the following if you are to specify the index name:
CONSTRAINT `pk_id` PRIMARY KEY (`id`)
这篇关于MySQL:在CREATE TABLE语句中命名主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!