alter table中多个更改列的语法错误?

时间:2016-08-07 00:46:45

标签: mysql sql

我正在尝试改变这样的表:

ALTER TABLE book 
    ADD COLUMN `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP AFTER `id`,
    CHANGE COLUMN `id` bigint unsigned NOT NULL AUTO_INCREMENT,
    CHANGE COLUMN `author_id` bigint unsigned NOT NULL;

但它一直给出这种语法错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'bigint unsigned NOT NULL AUTO_INCREMENT, CHANGE COLUMN `author_id` bigint un' at line 3

我不知道我在哪里错了。 SQL似乎对我很好,每个表的更改都可以单独成功执行:

ALTER TABLE book ADD COLUMN `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP AFTER `id`;
ALTER TABLE book CHANGE COLUMN `id` bigint unsigned NOT NULL AUTO_INCREMENT;
ALTER TABLE book CHANGE COLUMN `author_id` bigint unsigned NOT NULL;

我可以使用各个版本,但是我需要更改一些大型表,所以如果我使用组合查询会更快,那会更好。

为什么组合的SQL不起作用?

1 个答案:

答案 0 :(得分:1)

问题出在下面的代码行中,原因是文档CHANGE中的每个语法需要一个新的列名,例如CHANGE [COLUMN] old_col_name new_col_name column_definition

CHANGE COLUMN `id` bigint unsigned NOT NULL AUTO_INCREMENT,

所以你可以将其更改为

CHANGE COLUMN `id` `new_id` bigint unsigned NOT NULL AUTO_INCREMENT,

(或)考虑使用MODIFY而非MODIFY [COLUMN] col_name column_definition

MODIFY [COLUMN] `id` bigint unsigned NOT NULL AUTO_INCREMENT