重命名 MySQL 中的列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30290880/
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
Rename a column in MySQL
提问by Michael Peter
I am trying to rename a column in MySQL community server 5.5.27 using this SQL expression:
我正在尝试使用以下 SQL 表达式重命名 MySQL 社区服务器 5.5.27 中的列:
ALTER TABLE table_name RENAME COLUMN old_col_name TO new_col_name;
I also tried
我也试过
ALTER TABLE table_name RENAME old_col_name TO new_col_name;
But it says:
但它说:
Error: check the Manual that corresponds to your MySQL server version
错误:检查与您的 MySQL 服务器版本相对应的手册
回答by Rizky Fakkel
Use the following query:
使用以下查询:
ALTER TABLE tableName CHANGE `oldcolname` `newcolname` datatype(length);
The RENAME
function is used in Oracle databases.
该RENAME
函数用于 Oracle 数据库。
ALTER TABLE tableName RENAME COLUMN "oldcolname" TO "newcolname" datatype(length);
Notice the backticks used for MySQL, whereas double quotes are used for Oracle's syntax. Also note that MySQL 8.0 might not accept backticks. In that case, execute the query without backticks and it will probably work.
注意用于 MySQL 的反引号,而双引号用于 Oracle 的语法。另请注意,MySQL 8.0 可能不接受反引号。在这种情况下,执行没有反引号的查询,它可能会起作用。
@lad2025mentions it below, but I thought it'd be nice to add what he said. Thank you @lad2025!
@lad2025 在下面提到了它,但我认为添加他所说的内容会很好。谢谢@lad2025!
You can use the RENAME COLUMN
in MySQL 8.0 to rename any column you need renamed.
您可以使用RENAME COLUMN
MySQL 8.0 中的 来重命名您需要重命名的任何列。
ALTER TABLE table_name RENAME COLUMN old_col_name TO new_col_name;
RENAME COLUMN:
Can change a column name but not its definition.
More convenient than CHANGE to rename a column without changing its definition.
重命名列:
可以更改列名称,但不能更改其定义。
比 CHANGE 更方便地重命名列而不更改其定义。
回答by Kanke
In Server version: 5.6.34 MySQL Community Server
在服务器版本:5.6.34 MySQL 社区服务器
ALTER TABLE table_name
CHANGE COLUMN old_column_name new_column_name data_type;
回答by Ashu_FalcoN
From MySQL 5.7 Reference Manual.
来自MySQL 5.7 参考手册。
Syntax :
句法 :
ALTER TABLE t1 CHANGE a b DATATYPE;
ALTER TABLE t1 CHANGE ab DATATYPE;
e.g. : for CustomerTABLE having COLUMN customer_name, customer_street, customercity.
例如:对于具有 COLUMN customer_name、customer_street、customercity 的CustomerTABLE 。
And we want to change customercityTO customer_city:
我们要改变customercityTO customer_city:
alter table customer change customercity customer_city VARCHAR(225);
回答by Lukasz Szozda
From MySQL 8.0 you could use
从 MySQL 8.0 你可以使用
ALTER TABLE table_name RENAME COLUMN old_col_name TO new_col_name;
RENAME COLUMN:
Can change a column name but not its definition.
More convenient than CHANGE to rename a column without changing its definition.
重命名列:
可以更改列名称,但不能更改其定义。
比 CHANGE 更方便地重命名列而不更改其定义。
回答by sam
You can use following code:
您可以使用以下代码:
ALTER TABLE `dbName`.`tableName` CHANGE COLUMN `old_columnName` `new_columnName` VARCHAR(45) NULL DEFAULT NULL ;
回答by Dinesh Vaitage
Rename column name in mysql
重命名mysql中的列名
alter table categories change type category_type varchar(255);
回答by Radagast_Brown
https://dev.mysql.com/doc/refman/8.0/en/alter-table.html
https://dev.mysql.com/doc/refman/8.0/en/alter-table.html
For MySQL 8
对于 MySQL 8
alter table creditReportXml_temp change column applicationID applicantID int(11);
回答by mohimenul
Syntax: ALTER TABLE table_name CHANGE old_column_name new_column_name datatype;
语法:ALTER TABLE table_name CHANGE old_column_name new_column_name 数据类型;
If table name is Studentand column name is Name. Then, if you want to change Nameto First_Name
如果表名是Student并且列名是Name。然后,如果您想将Name更改为First_Name
ALTER TABLE Student CHANGE Name First_Name varchar(20);
回答by unknown
for mysql version 5
对于 mysql 版本 5
alter table *table_name* change column *old_column_name* *new_column_name* datatype();
alter table *table_name* change column *old_column_name* *new_column_name* datatype();
回答by Ish
In mysql your query should be like
在 mysql 你的查询应该是这样的
ALTER TABLE table_name change column_1 column_2 Data_Type;
you have written the query in Oracle.
您已经在 Oracle 中编写了查询。