In this post explains how to use the ALTER TABLE statement in SQL Server (Transact-SQL) to add a column, modify a column, drop a column, rename a column or rename a table with syntax and examples.
The SQL Server (Transact-SQL) ALTER TABLE statement is used to add, modify, or drop columns in a table.
You can use the ALTER TABLE statement in SQL Server to add a column to a table.
The syntax to add a column in a table in SQL Server (Transact-SQL) is:
Let's look at an example that shows how to add a column in an SQL Server table using the ALTER TABLE statement.
For example:
This SQL Server ALTER TABLE example will add a column to the employees table called last_name.
You can use the ALTER TABLE statement in SQL Server to add multiple columns to a table.
The syntax to add multiple columns to an existing table in SQL Server (Transact-SQL) is:
Let's look at an example that shows how to add multiple columns to a table in SQL Server using the ALTER TABLE statement.
For example:
This SQL Server ALTER TABLE example will add two columns, last_name as a VARCHAR(50) field and first_name as a VARCHAR(40) field to the employees table.
You can use the ALTER TABLE statement in SQL Server to modify a column in a table.
The syntax to modify a column in an existing table in SQL Server (Transact-SQL) is:
Let's look at an example that shows how to modify a column in a table in SQL Server using the ALTER TABLE statement.
For example:
This SQL Server ALTER TABLE example will modify the column called last_name to be a data type of VARCHAR(75) and force the column to not allow null values.
You can use the ALTER TABLE statement in SQL Server to drop a column in a table.
The syntax to drop a column in an existing table in SQL Server (Transact-SQL) is:
Let's look at an example that shows how to drop a column in a table in SQL Server using the ALTER TABLE statement.
For example:
This SQL Server ALTER TABLE example will drop the column called last_name from the table called employees.
You can not use the ALTER TABLE statement in SQL Server to rename a column in a table. However, you can use sp_rename, though Microsoft recommends that you drop and recreate the table so that scripts and stored procedures are not broken.
The syntax to rename a column in an existing table in SQL Server (Transact-SQL) is:
Let's look at an example that shows how to rename a column in a table in SQL Server using sp_rename.
For example:
This SQL Server example will use sp_rename to rename the column in the employees table from last_name to lname.
You can not use the ALTER TABLE statement in SQL Server to rename a table. However, you can use sp_rename, though Microsoft recommends that you drop and recreate the table so that scripts and stored procedures are not broken.
The syntax to rename a table in SQL Server (Transact-SQL) is:
Let's look at an example that shows how to rename a table in SQL Server using sp_rename.
For example:
This SQL Server example will use sp_rename to rename the employees table to emps.