如果恰好是 MySQL 类型,如何修改列?

How to modify a column if it happens to be a MySQL type?

我有一个名为 sidebar_items 的 table,它有一个 Type 列,类型为 enum('image', 'html')。我想将此列更改为 enum('image', 'html', 'structure') 类型。我试过这个:

alter table sidebar_items modify Type Type enum('image', 'html', 'structure');

它给我的错误是

alter table sidebar_items modify Type Type enum('image', 'html', 'structure')   Error Code: 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 'Type enum('image', 'html', 'structure')' at line 1

我也尝试过使用 `Type`。我可以使这个查询工作吗?

或者,我可以通过以下方式解决问题:

但是,我很想知道是否有任何更简单的解决方案,可能只需要一个 alter table 命令。

正确的语法是:

ALTER TABLE sidebar_items MODIFY `Type` ENUM('image', 'html', 'structure');

使用 ALTER TABLE ... MODIFY 命令,您不需要指定两次列名,但 ALTER TABLE ... CHANGE.

需要它

You can rename a column using a CHANGE old_col_name new_col_name column_definition clause. To do so, specify the old and new column names and the definition that the column currently has. For example, to rename an INTEGER column from a to b, you can do this:

ALTER TABLE t1 CHANGE a b INTEGER;

To change a column's type but not the name, CHANGE syntax still requires an old and new column name, even if they are the same. For example:

ALTER TABLE t1 CHANGE b b BIGINT NOT NULL;

You can also use MODIFY to change a column's type without renaming it:

ALTER TABLE t1 MODIFY b BIGINT NOT NULL;

MODIFY is an extension to ALTER TABLE for Oracle compatibility.