MySQL 如何更改 1 列以上的表列数据类型?

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

How do I Alter Table Column datatype on more than 1 column?

mysqlalter-table

提问by JoJo

For example:

例如:

ALTER TABLE webstore.Store MODIFY COLUMN (
  ShortName VARCHAR(100),
  UrlShort VARCHAR(100)
);

The above however does not work. I am using MySql 5.x

然而,上述方法不起作用。我正在使用 MySql 5.x

回答by Daniel Vandersluis

ALTER TABLEcan do multiple table alterations in one statement, but MODIFY COLUMNcan only work on one column at a time, so you need to specify MODIFY COLUMNfor each column you want to change:

ALTER TABLE可以在一个语句中进行多个表更改,但MODIFY COLUMN一次只能处理一列,因此您需要为MODIFY COLUMN要更改的每一列指定:

ALTER TABLE webstore.Store
  MODIFY COLUMN ShortName VARCHAR(100),
  MODIFY COLUMN UrlShort VARCHAR(100);

Also, note this warning from the manual:

另外,请注意手册中的此警告:

When you use CHANGE or MODIFY, column_definitionmust include the data type and all attributes that should apply to the new column, other than index attributes such as PRIMARY KEY or UNIQUE. Attributes present in the original definition but not specified for the new definition are not carried forward.

当您使用 CHANGE 或 MODIFY 时,column_definition必须包括数据类型和所有应应用于新列的属性,而不是 PRIMARY KEY 或 UNIQUE 等索引属性。原始定义中存在但未为新定义指定的属性不会被继承。

回答by Choudhury Saadmaan Mahmid

Use the following syntax:

使用以下语法:

  ALTER TABLE your_table
  MODIFY COLUMN column1 datatype,
  MODIFY COLUMN column2 datatype,
  ... ... ... ... ... 
  ... ... ... ... ...

Based on that, your ALTERcommand should be:

基于此,您的ALTER命令应该是:

  ALTER TABLE webstore.Store
  MODIFY COLUMN ShortName VARCHAR(100),
  MODIFY COLUMN UrlShort VARCHAR(100)

Note that:

注意:

  1. There are no second brackets around the MODIFYstatements.
  2. I used two separate MODIFYstatements for two separate columns.
  1. MODIFY语句周围没有第二个括号。
  2. MODIFY对两个单独的列使用了两个单独的语句。

This is the standard format of the MODIFYstatement for an ALTERcommand on multiple columns in a MySQL table.

这是MySQL 表中多列命令MODIFY语句的标准格式ALTER

Take a look at the following: http://dev.mysql.com/doc/refman/5.1/en/alter-table.htmland Alter multiple columns in a single statement

看看以下内容:http: //dev.mysql.com/doc/refman/5.1/en/alter-table.html在单个语句中更改多个列