将 MySQL 列更改为 AUTO_INCRMENT

发布于 2024-08-19 21:32:36 字数 327 浏览 2 评论 0原文

我正在尝试修改表以使其主键列在事后自动递增。我尝试了以下 SQL,但收到语法错误通知。

ALTER TABLE document
ALTER COLUMN document_id AUTO_INCREMENT

我做错了什么还是这是不可能的?

+--------------------+
| VERSION()          |
+--------------------+
| 5.0.75-0ubuntu10.2 |
+--------------------+

I’m trying to modify a table to make its primary key column AUTO_INCREMENT after the fact. I have tried the following SQL, but got a syntax error notification.

ALTER TABLE document
ALTER COLUMN document_id AUTO_INCREMENT

Am I doing something wrong or is this not possible?

+--------------------+
| VERSION()          |
+--------------------+
| 5.0.75-0ubuntu10.2 |
+--------------------+

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(21

笑叹一世浮沉 2024-08-26 21:32:37

AUTO_INCRMENT 是列数据类型的一部分,您必须再次为列定义完整的数据类型:(

ALTER TABLE document
ALTER COLUMN document_id int AUTO_INCREMENT

int 为例,您应该将其设置为列的类型以前有过)

AUTO_INCREMENT is part of the column's datatype, you have to define the complete datatype for the column again:

ALTER TABLE document
ALTER COLUMN document_id int AUTO_INCREMENT

(int taken as an example, you should set it to the type the column had before)

佼人 2024-08-26 21:32:37

你可以这样做:

 alter table [table_name] modify column [column_name] [column_type] AUTO_INCREMENT;

You can do it like this:

 alter table [table_name] modify column [column_name] [column_type] AUTO_INCREMENT;
我也只是我 2024-08-26 21:32:37

您可以使用以下查询使 document_id 自动递增

ALTER TABLE document MODIFY COLUMN document_id INT auto_increment

最好也将 document_id 设置为主键

ALTER TABLE document MODIFY COLUMN document_id INT auto_increment PRIMARY KEY;

You can use the following query to make document_id to increment automatically

ALTER TABLE document MODIFY COLUMN document_id INT auto_increment

It is preferred to make document_id primary key as well

ALTER TABLE document MODIFY COLUMN document_id INT auto_increment PRIMARY KEY;
呆橘 2024-08-26 21:32:37

下面的声明有效。请注意,您需要再次提及列名称的数据类型(重新声明列之前的数据类型)。

ALTER TABLE document
MODIFY COLUMN document_id int AUTO_INCREMENT;

Below statement works. Note that you need to mention the data type again for the column name (redeclare the data type the column was before).

ALTER TABLE document
MODIFY COLUMN document_id int AUTO_INCREMENT;
ゝ杯具 2024-08-26 21:32:37

同时设置列为主键和自动增量:

  mysql> ALTER TABLE persons MODIFY COLUMN personID INT auto_increment PRIMARY KEY;
    Query OK, 10 rows affected (0.77 sec)
    Records: 10  Duplicates: 0  Warnings: 0

    mysql>

Setting column as primary key and auto_increment at the same time:

  mysql> ALTER TABLE persons MODIFY COLUMN personID INT auto_increment PRIMARY KEY;
    Query OK, 10 rows affected (0.77 sec)
    Records: 10  Duplicates: 0  Warnings: 0

    mysql>
淡水深流 2024-08-26 21:32:37

AUTO_INCRMENT 是列数据类型的一部分,您必须再次为列定义完整的数据类型:(

ALTER TABLE document
MODIFY COLUMN document_id int AUTO_INCREMENT

以 int 为例,您应该将其设置为列之前的类型)

AUTO_INCREMENT is part of the column's datatype, you have to define the complete datatype for the column again:

ALTER TABLE document
MODIFY COLUMN document_id int AUTO_INCREMENT

(int taken as an example, you should set it to the type the column had before)

剧终人散尽 2024-08-26 21:32:37

如果以上方法都不起作用,请尝试此操作。这就是我在 MYSQL 中所做的,是的,您需要将列名(document_id)写两次。

ALTER TABLE document
CHANGE COLUMN document_id document_id INT(11) NOT NULL AUTO_INCREMENT ;

If none of the above works try this. This is what I did in MYSQL and yes, you need to write the column name (document_id) twice.

ALTER TABLE document
CHANGE COLUMN document_id document_id INT(11) NOT NULL AUTO_INCREMENT ;
抱着落日 2024-08-26 21:32:37

为了修改mysql中的列,我们使用alter和modify关键字。假设我们创建了一个表,如下所示:

create table emp(
    id varchar(20),
    ename varchar(20),
    salary float
);

现在我们想要将列 id 的类型修改为自动递增的整数。您可以使用如下命令来执行此操作:

alter table emp modify column id int(10) auto_increment;

To modify the column in mysql we use alter and modify keywords. Suppose we have created a table like:

create table emp(
    id varchar(20),
    ename varchar(20),
    salary float
);

Now we want to modify type of the column id to integer with auto increment. You could do this with a command like:

alter table emp modify column id int(10) auto_increment;
苹果你个爱泡泡 2024-08-26 21:32:37

上表语法:

CREATE TABLE apim_log_request (TransactionId varchar(50) DEFAULT NULL);

要将 TransactionId 更改为自动递增,请使用此查询

ALTER TABLE apim_log_request MODIFY COLUMN TransactionId INT auto_increment;

Previous Table syntax:

CREATE TABLE apim_log_request (TransactionId varchar(50) DEFAULT NULL);

For changing the TransactionId to auto increment use this query

ALTER TABLE apim_log_request MODIFY COLUMN TransactionId INT auto_increment;
肤浅与狂妄 2024-08-26 21:32:37
alter table tbl_user MODIFY COLUMN id int(10) auto_increment;
alter table tbl_user MODIFY COLUMN id int(10) auto_increment;
雪若未夕 2024-08-26 21:32:37

就像这样:

alter table document修改列id int(11) auto_increment;

Just like this:

alter table document modify column id int(11) auto_increment;

眼泪也成诗 2024-08-26 21:32:37

当您再次重新定义列时,您必须再次指定数据类型并向其添加 auto_increment,因为它是数据类型的一部分。

ALTER TABLE `document` MODIFY COLUMN `document_id` INT AUTO_INCREMENT;

As you are redefining the column again, you have to specify the datatype again and add auto_increment to it as it's a part of datatype.

ALTER TABLE `document` MODIFY COLUMN `document_id` INT AUTO_INCREMENT;
画骨成沙 2024-08-26 21:32:37

请尝试以下操作:

ALTER TABLE table_name MODIFY COLUMN id datatype auto_increment;

Try the following:

ALTER TABLE table_name MODIFY COLUMN id datatype auto_increment;
放我走吧 2024-08-26 21:32:37

由于 SQL 标签附加在问题上,我真的认为所有答案都错过了一个要点。

时,您将收到错误消息

MODIFY 命令在 SQL Server 中不存在 因此,当您运行ALTER TABLE Satellites MODIFY COLUMN SatelliteID INT auto_increment PRIMARY KEY; >

enter此处的图像描述

在这种情况下,您可以添加新列作为 INT IDENTITY

ALTER TABLE Satellites
   ADD ID INT IDENTITY
       CONSTRAINT PK_YourTable PRIMARY KEY CLUSTERED;

Fill the existing null index with incremental numbers using this method,

DECLARE @id INT
SET @id = 0 
UPDATE Satellites SET @id = SatelliteID = @id + 1 

Since SQL tag is attached to the question I really think all answers missed one major point.

MODIFY command does not exist in SQL server So you will be getting an error when you run the

ALTER TABLE Satellites MODIFY COLUMN SatelliteID INT auto_increment PRIMARY KEY;

enter image description here

In this case you can either add new column as INT IDENTITY

ALTER TABLE Satellites
   ADD ID INT IDENTITY
       CONSTRAINT PK_YourTable PRIMARY KEY CLUSTERED;

OR

Fill the existing null index with incremental numbers using this method,

DECLARE @id INT
SET @id = 0 
UPDATE Satellites SET @id = SatelliteID = @id + 1 
二货你真萌 2024-08-26 21:32:37

使用以下查询:

ALTER TABLE YourTable DROP COLUMN IDCol

ALTER TABLE YourTable ADD IDCol INT IDENTITY(1,1)

Use the following queries:

ALTER TABLE YourTable DROP COLUMN IDCol

ALTER TABLE YourTable ADD IDCol INT IDENTITY(1,1)
南烟 2024-08-26 21:32:36
ALTER TABLE document MODIFY COLUMN document_id INT auto_increment
ALTER TABLE document MODIFY COLUMN document_id INT auto_increment
世俗缘 2024-08-26 21:32:36

Roman 是对的,但请注意,auto_increment 列必须是主键或唯一键的一部分(并且在几乎 100% 的情况下,它应该是构成主键的唯一列):

ALTER TABLE document MODIFY document_id INT AUTO_INCREMENT PRIMARY KEY

Roman is right, but note that the auto_increment column must be part of the PRIMARY KEY or a UNIQUE KEY (and in almost 100% of the cases, it should be the only column that makes up the PRIMARY KEY):

ALTER TABLE document MODIFY document_id INT AUTO_INCREMENT PRIMARY KEY
风筝在阴天搁浅。 2024-08-26 21:32:36

就我而言,只有当我输入 not null 时,它才有效。我认为这是一个限制。

ALTER TABLE document MODIFY COLUMN document_id INT NOT NULL AUTO_INCREMENT;

In my case it only worked when I put not null. I think this is a constraint.

ALTER TABLE document MODIFY COLUMN document_id INT NOT NULL AUTO_INCREMENT;
丿*梦醉红颜 2024-08-26 21:32:36

您可以通过以下查询将atuto_increment约束应用于数据列:

ALTER TABLE customers MODIFY COLUMN customer_id BIGINT NOT NULL AUTO_INCREMENT;

但是,如果这些列是外键约束的一部分,您很可能会收到错误。因此,建议使用以下查询关闭foreign_key_checks:

SET foreign_key_checks = 0;

因此,请改用以下查询:

SET foreign_key_checks = 0;
ALTER TABLE customers MODIFY COLUMN customer_id BIGINT NOT NULL AUTO_INCREMENT;
SET foreign_key_checks = 1;

You can apply the atuto_increment constraint to the data column by the following query:

ALTER TABLE customers MODIFY COLUMN customer_id BIGINT NOT NULL AUTO_INCREMENT;

But, if the columns are part of a foreign key constraint you, will most probably receive an error. Therefore, it is advised to turn off foreign_key_checks by using the following query:

SET foreign_key_checks = 0;

Therefore, use the following query instead:

SET foreign_key_checks = 0;
ALTER TABLE customers MODIFY COLUMN customer_id BIGINT NOT NULL AUTO_INCREMENT;
SET foreign_key_checks = 1;
拥抱我好吗 2024-08-26 21:32:36

执行此操作的 SQL 是:

ALTER TABLE `document` MODIFY COLUMN `document_id` INT AUTO_INCREMENT;

您的 SQL 可能无法工作有几个原因。首先,您必须重新指定数据类型(在本例中为INT)。此外,您尝试更改的列必须已建立索引(它不必是主键,但通常这就是您想要的)。此外,每个表只能有一个 AUTO_INCRMENT 列。因此,您可能希望运行以下 SQL(如果您的列未建立索引):

ALTER TABLE `document` MODIFY `document_id` INT AUTO_INCREMENT PRIMARY KEY;

您可以在 MySQL 文档中找到更多信息:http://dev.mysql.com/doc/refman/5.1/en/alter-table.html 用于修改列语法和 http://dev.mysql.com/doc/refman/5.1/en/ create-table.html 了解有关指定列的更多信息。

The SQL to do this would be:

ALTER TABLE `document` MODIFY COLUMN `document_id` INT AUTO_INCREMENT;

There are a couple of reasons that your SQL might not work. First, you must re-specify the data type (INT in this case). Also, the column you are trying to alter must be indexed (it does not have to be the primary key, but usually that is what you would want). Furthermore, there can only be one AUTO_INCREMENT column for each table. So, you may wish to run the following SQL (if your column is not indexed):

ALTER TABLE `document` MODIFY `document_id` INT AUTO_INCREMENT PRIMARY KEY;

You can find more information in the MySQL documentation: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html for the modify column syntax and http://dev.mysql.com/doc/refman/5.1/en/create-table.html for more information about specifying columns.

淡淡的优雅 2024-08-26 21:32:36

您必须在auto_increment指令之前指定列的类型,即ALTER TABLE document MODIFY COLUMN document_id INT AUTO_INCRMENT

You must specify the type of the column before the auto_increment directive, i.e. ALTER TABLE document MODIFY COLUMN document_id INT AUTO_INCREMENT.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文