Rename column name with php and MySql

12,105

Solution 1

alter table tablename change oldColumn newColumn varchar(10) ; 

Reference : Alter Table - MySQL Command

Solution 2

if(isset($_GET["rename"])){ 
    mysql_query("ALTER TABLE myTable CHANGE c  novaC varchar(9999)"); 
}

The MySQL documentation

Share:
12,105
Nrc
Author by

Nrc

Updated on June 04, 2022

Comments

  • Nrc
    Nrc almost 2 years

    I am trying to change the name of the column c to novaC with php and mysql. Everywhere I look seems to give the same solution but it doesn't seem to work:

    if(isset($_GET["rename"])){
        mysql_query("ALTER TABLE myTable
        RENAME COLUMN c to novaC");
    }
    

    If I type: ALTER TABLE aaa RENAME COLUMN c to novaC directly in MySql it gives:

     #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'COLUMN c to novaC' at line 2
    
  • Aaron W.
    Aaron W. over 11 years
    The CHANGE requires you to specify the "column_definition" as well. So that query would cause an error.
  • Nrc
    Nrc over 11 years
    This is perfect. Thank you.
  • Muhammad Talha Akbar
    Muhammad Talha Akbar over 11 years
    @AaronW. oh sorry i forgot it!