ALTER 表 - 在 MySQL 中添加 AUTOINCREMENT

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

ALTER table - adding AUTOINCREMENT in MySQL

mysqlsqlalter-table

提问by Sumit Gupta

I created a table in MySQL with on column itemID. After creating the table, now I want to change this column to AUTOINCREMENT. How can this be done using ALTER statements?

我在 MySQL 中用 on column 创建了一个表itemID。创建表后,现在我想将此列更改为AUTOINCREMENT. 如何使用 ALTER 语句完成此操作?

Table definition:

表定义:

ALLITEMS (itemid int(10) unsigned, itemname varchar(50))

ALLITEMS (itemid int(10) unsigned, itemname varchar(50))

I am using the following code but it is throwing error: syntax incorrect.

我正在使用以下代码,但它抛出错误:语法不正确

ALTER TABLE allitems
MODIFY itemid INT(10) UNSIGNED AUTOINCREMENT; 

回答by ThinkCode

CREATE TABLE ALLITEMS(
    itemid INT(10)UNSIGNED,
    itemname VARCHAR(50)
);

ALTER TABLE ALLITEMS CHANGE itemid itemid INT(10)AUTO_INCREMENT PRIMARY KEY;

DESC ALLITEMS;

INSERT INTO ALLITEMS(itemname)
VALUES
    ('Apple'),
    ('Orange'),
    ('Banana');

SELECT
    *
FROM
    ALLITEMS;

I was confused with CHANGEand MODIFYkeywords before too:

我之前也对CHANGEMODIFY关键字感到困惑:

ALTER TABLE ALLITEMS CHANGE itemid itemid INT(10)AUTO_INCREMENT PRIMARY KEY;

ALTER TABLE ALLITEMS MODIFY itemid INT(5);

While we are there, also note that AUTO_INCREMENTcan also start with a predefined number:

当我们在那里时,还要注意也AUTO_INCREMENT可以以预定义的数字开头:

ALTER TABLE tbl AUTO_INCREMENT = 100;

回答by blejzz

The syntax:

语法:

   ALTER TABLE `table1` CHANGE `itemId` `itemId` INT( 11 ) NOT NULL AUTO_INCREMENT 

But the table needs a defined key (ex primary key on itemId).

但是该表需要一个定义的键(例如 itemId 上的主键)。

回答by dee-see

ALTER TABLE `ALLITEMS`
    CHANGE COLUMN `itemid` `itemid` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT;

回答by Mark Amery

Basic syntax for adding an AUTO_INCREMENT PRIMARY KEY to the OP's existing table:

将 AUTO_INCREMENT PRIMARY KEY 添加到 OP 现有表的基本语法:

ALTER TABLE allitems
MODIFY itemid INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY;

Or for a new table, here's the syntax example from the docs:

或者对于新表,这里是文档中的语法示例:

CREATE TABLE animals (
     id MEDIUMINT NOT NULL AUTO_INCREMENT,
     name CHAR(30) NOT NULL,
     PRIMARY KEY (id)
);

Traps and things to note:

陷阱和注意事项:

  • An AUTO_INCREMENTcolumn must have an index on it. (Usually, you'll want it to be the PRIMARY KEY, but MySQL does not require this.)
  • It's usually a good idea to make your AUTO_INCREMENTcolumns UNSIGNED. From the docs:

    Use the UNSIGNED attribute if possible to allow a greater range.

  • When using a CHANGEor MODIFYclause to make a column AUTO_INCREMENT(or indeed wheneveryou use a CHANGEor MODIFYclause) you should be careful to include all modifiers for the column, like NOT NULLor UNSIGNED, that show up in the table definition when you call SHOW CREATE TABLE yourtable. These modifiers will be lost otherwise.
  • AUTO_INCREMENT列必须有一个索引。(通常,您会希望它是 PRIMARY KEY,但 MySQL 不需要这个。)
  • 制作AUTO_INCREMENT列通常是个好主意UNSIGNED。从文档:

    如果可能,请使用 UNSIGNED 属性以允许更大的范围。

  • 当使用CHANGEorMODIFY子句创建列时AUTO_INCREMENT(或者实际上无论何时使用CHANGEorMODIFY子句),您应该小心地包含该列的所有修饰符,例如NOT NULLor UNSIGNED,当您调用SHOW CREATE TABLE yourtable. 否则这些修饰符将丢失。

回答by Mark Amery

ALTER TABLE allitems
CHANGE itemid itemid INT(10) AUTO_INCREMENT;

回答by Ajay Singh Rathore

ALTER TABLE tblcatalog
    CHANGE COLUMN id id INT(11) NOT NULL AUTO_INCREMENT FIRST;

回答by Rahul Yadav

ALTER TABLE t_name modify c_name INT(10) AUTO_INCREMENT PRIMARY KEY;