Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I rename column in w3schools sql?

Tags:

sql

alter

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?

like image 770
Chibak Avatar asked Sep 20 '25 06:09

Chibak


2 Answers

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
like image 138
Rahul Tripathi Avatar answered Sep 21 '25 23:09

Rahul Tripathi


From "Learning PHP, MySQL & JavaScript" by Robin Nixon pg 185. I tried it and it worked.

ALTER TABLE tableName CHANGE oldColumnName newColumnName TYPE(#);

note that TYPE(#) is, for example, VARCHAR(20) or some other data type and must be included even if the data type is not being changed.

like image 21
Hannah Avatar answered Sep 22 '25 01:09

Hannah