How to use ALTER Statement In SQL
To rename the column of a database in SQL Server, we can also use the ALTER table command. ALTER is a Data Definition Language(DDL) command that is used to update the schema of tables, views, and indexes in SQL Server. Following is the syntax to rename the column in SQL Server using the ALTER command:
Syntax:
ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name
where:
- table_name: is the name of the table containing the column you want to rename.
- old_column_name: is the current name of the column that you want to rename.
- new_column_name: is the desired new name for the column.
Example
To change the column name from CustomerName to User of the above table Customers we will have to run the following query
Query:
ALTER TABLE Customers RENAME COLUMN CustomerName TO Users
Output:
CustomerID |
Users |
City |
State |
Age |
---|---|---|---|---|
1 |
Amit Kumar |
Mumbai |
Maharashtra |
28 |
2 |
Kavya Sharma |
Delhi |
Delhi |
35 |
3 |
Amit Singh |
Bangalore |
Karnataka |
42 |
4 |
Rohan Kumar |
Kolkata |
West Bengal |
33 |
Explanation: The ‘ALTER TABLE Customers RENAME COLUMN CustomerName TO Users‘ command attempts to rename the CustomerName column to Users. However, SQL Server does not support this syntax; use sp_rename instead for renaming columns in SQL Server.
Rename Column in SQL Server
SQL Server is a widely used Relational Database Management System (RDBMS) that allows users to create and manage databases effectively. Renaming a column in a database is a common task usually required when users want to change the database schema. In this article, we will explore different methods to rename columns in SQL Server.
Contact Us