How do I rename column in w3schools sql?

Chibak picture Chibak · Aug 15, 2013 · Viewed 22.2k times · Source

I am trying to rename a column name in w3schools website

ALTER TABLE customers
  RENAME COLUMN contactname to new_name;

However, the above code throws syntax error. What am I doing wrong?

Answer

Rahul Tripathi picture Rahul Tripathi · Aug 15, 2013

You can try this to rename the column in SQL Server:-

sp_RENAME 'TableName.[OldColumnName]' , '[NewColumnName]', 'COLUMN'

sp_rename automatically renames the associated index whenever a PRIMARY KEY or UNIQUE constraint is renamed. If a renamed index is tied to a PRIMARY KEY constraint, the PRIMARY KEY constraint is also automatically renamed by sp_rename. sp_rename can be used to rename primary and secondary XML indexes.

For MYSQL try this:-

ALTER TABLE table_name CHANGE [COLUMN] old_col_name new_col_name