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?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With