简体   繁体   English

如何在多于1列上更改表列数据类型?

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

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.

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. 使用CHANGE或MODIFY时, column_definition必须包含应该应用于新列的数据类型和所有属性,而不是索引属性(如PRIMARY KEY或UNIQUE)。 Attributes present in the original definition but not specified for the new definition are not carried forward. 原始定义中存在但未为新定义指定的属性不会继续使用。

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: 注意:

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

This is the standard format of the MODIFY statement for an ALTER command on multiple columns in a MySQL table. 这是MySQL表中多列上ALTER命令的MODIFY语句的标准格式。

Take a look at the following: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html and Alter multiple columns in a single statement 请查看以下内容: http//dev.mysql.com/doc/refman/5.1/en/alter-table.html在单个语句中更改多个列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更改表列数据类型 - Alter table column datatype 如何将具有 varchar 数据类型列的表更改为日期时间/时间戳 - How to alter a table with a column of varchar datatype to Datetime/ Timestamp 如何使用 ALTER 命令将具有 int 数据类型的列添加到 SQL 中的表中? - How can I add a column with int datatype to a table in SQL using the ALTER command? 如何更改mysql表列的默认值? - How do I alter a mysql table column defaults? MySQL-如何更改所有数据库中的列数据类型? - MySQL - How to alter column datatype in all databases? 如何更改列数据类型以包含特殊字符? - How to alter a column datatype to contain special characters? 如何更改列以支持unicode - How do I alter column to support unicode 对于超过 4000 万行的表,如何向 MySQL 中的 ENUM 类型列添加更多成员? - How do I add more members to my ENUM-type column in MySQL for a table size of more than 40 million rows? 如何在sqlalchemy中更改现有数据库表的两个不同的列标题? - How do I alter two different column headers of a pre-existing database table in sqlalchemy? 如何检查一列是否有两个以上的项目,以及它是否比 SQL 中的 avg() 多? - How do I check if a column have more than two items and if it have than do the avg() on it in SQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM