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
How do I Alter Table Column datatype on more than 1 column?
提问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 TABLE
can do multiple table alterations in one statement, but MODIFY COLUMN
can only work on one column at a time, so you need to specify MODIFY COLUMN
for 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_definition
must 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 ALTER
command should be:
基于此,您的ALTER
命令应该是:
ALTER TABLE webstore.Store
MODIFY COLUMN ShortName VARCHAR(100),
MODIFY COLUMN UrlShort VARCHAR(100)
Note that:
注意:
- There are no second brackets around the
MODIFY
statements. - I used two separate
MODIFY
statements for two separate columns.
MODIFY
语句周围没有第二个括号。- 我
MODIFY
对两个单独的列使用了两个单独的语句。
This is the standard format of the MODIFY
statement for an ALTER
command 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并 在单个语句中更改多个列