Rename a column in MySQL

前端 未结 10 1895
广开言路
广开言路 2020-11-29 15:46

I am trying to rename a column in MySQL community server 5.5.27 using this SQL expression:

ALTER TABLE table_name R         


        
相关标签:
10条回答
  • 2020-11-29 16:31

    In Server version: 5.6.34 MySQL Community Server

    ALTER TABLE table_name
    CHANGE COLUMN old_column_name new_column_name data_type;
    
    0 讨论(0)
  • 2020-11-29 16:32

    Use the following query:

    ALTER TABLE tableName CHANGE `oldcolname` `newcolname` datatype(length);
    

    The RENAME function is used in Oracle databases.

    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.


    @lad2025 mentions it below, but I thought it'd be nice to add what he said. Thank you @lad2025!

    You can use the RENAME COLUMN in MySQL 8.0 to rename any column you need renamed.

    ALTER TABLE table_name RENAME COLUMN old_col_name TO new_col_name;
    

    ALTER TABLE Syntax:

    RENAME COLUMN:

    • Can change a column name but not its definition.

    • More convenient than CHANGE to rename a column without changing its definition.

    0 讨论(0)
  • 2020-11-29 16:33

    https://dev.mysql.com/doc/refman/8.0/en/alter-table.html

    For MySQL 8

    alter table creditReportXml_temp change column applicationID applicantID int(11);
    
    0 讨论(0)
  • 2020-11-29 16:40

    You can use following code:

    ALTER TABLE `dbName`.`tableName` CHANGE COLUMN `old_columnName` `new_columnName` VARCHAR(45) NULL DEFAULT NULL ;
    
    0 讨论(0)
  • 2020-11-29 16:43

    Syntax: ALTER TABLE table_name CHANGE old_column_name new_column_name datatype;

    If table name is Student and column name is Name. Then, if you want to change Name to First_Name

    ALTER TABLE Student CHANGE Name First_Name varchar(20);
    
    0 讨论(0)
  • 2020-11-29 16:44

    In mysql your query should be like

    ALTER TABLE table_name change column_1 column_2 Data_Type;
    

    you have written the query in Oracle.

    0 讨论(0)
提交回复
热议问题