将 MySQL 列更改为 AUTO_INCRMENT
我正在尝试修改表以使其主键列在事后自动递增。我尝试了以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
AUTO_INCRMENT
是列数据类型的一部分,您必须再次为列定义完整的数据类型:(以
int
为例,您应该将其设置为列的类型以前有过)AUTO_INCREMENT
is part of the column's datatype, you have to define the complete datatype for the column again:(
int
taken as an example, you should set it to the type the column had before)你可以这样做:
You can do it like this:
您可以使用以下查询使 document_id 自动递增
最好也将 document_id 设置为主键
You can use the following query to make document_id to increment automatically
It is preferred to make document_id primary key as well
下面的声明有效。请注意,您需要再次提及列名称的数据类型(重新声明列之前的数据类型)。
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).
同时设置列为主键和自动增量:
Setting column as primary key and auto_increment at the same time:
AUTO_INCRMENT
是列数据类型的一部分,您必须再次为列定义完整的数据类型:(以 int 为例,您应该将其设置为列之前的类型)
AUTO_INCREMENT
is part of the column's datatype, you have to define the complete datatype for the column again:(int taken as an example, you should set it to the type the column had before)
如果以上方法都不起作用,请尝试此操作。这就是我在 MYSQL 中所做的,是的,您需要将列名(document_id)写两次。
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.
为了修改mysql中的列,我们使用alter和modify关键字。假设我们创建了一个表,如下所示:
现在我们想要将列 id 的类型修改为自动递增的整数。您可以使用如下命令来执行此操作:
To modify the column in mysql we use alter and modify keywords. Suppose we have created a table like:
Now we want to modify type of the column id to integer with auto increment. You could do this with a command like:
上表语法:
要将 TransactionId 更改为自动递增,请使用此查询
Previous Table syntax:
For changing the TransactionId to auto increment use this query
就像这样:
alter table document修改列id int(11) auto_increment;
Just like this:
alter table document modify column id int(11) auto_increment;
当您再次重新定义列时,您必须再次指定数据类型并向其添加 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.
请尝试以下操作:
Try the following:
由于
SQL
标签附加在问题上,我真的认为所有答案都错过了一个要点。时,您将收到错误消息
MODIFY
命令在 SQL Server 中不存在 因此,当您运行ALTER TABLE Satellites MODIFY COLUMN SatelliteID INT auto_increment PRIMARY KEY; >
在这种情况下,您可以添加新列作为
INT IDENTITY
或
Fill the existing null index with incremental numbers using this method,
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 theALTER TABLE Satellites MODIFY COLUMN SatelliteID INT auto_increment PRIMARY KEY;
In this case you can either add new column as
INT IDENTITY
OR
Fill the existing null index with incremental numbers using this method,
使用以下查询:
Use the following queries:
Roman 是对的,但请注意,auto_increment 列必须是主键或唯一键的一部分(并且在几乎 100% 的情况下,它应该是构成主键的唯一列):
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):
就我而言,只有当我输入
not null
时,它才有效。我认为这是一个限制。In my case it only worked when I put
not null
. I think this is a constraint.您可以通过以下查询将atuto_increment约束应用于数据列:
但是,如果这些列是外键约束的一部分,您很可能会收到错误。因此,建议使用以下查询关闭foreign_key_checks:
因此,请改用以下查询:
You can apply the atuto_increment constraint to the data column by the following query:
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:
Therefore, use the following query instead:
执行此操作的 SQL 是:
您的 SQL 可能无法工作有几个原因。首先,您必须重新指定数据类型(在本例中为
INT
)。此外,您尝试更改的列必须已建立索引(它不必是主键,但通常这就是您想要的)。此外,每个表只能有一个AUTO_INCRMENT
列。因此,您可能希望运行以下 SQL(如果您的列未建立索引):您可以在 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:
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 oneAUTO_INCREMENT
column for each table. So, you may wish to run the following SQL (if your column is not indexed):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.
您必须在
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
.