Thursday, 13 March 2014

Query to Alter,Add,modify,delete columns in table - sql server

Introduction:

In this article I will explain how to write SQL Query to add new column to existing table in sql server and query to delete column from table in sql and query to modify column in existing table.



Query to add new column to table

If we want to add new column to existing table that syntax will be like this


ALTER TABLE Table_Name ADD COLUMN_NAME DATATYPE
Ex:


ALTER TABLE emp_table ADD Manager_Name VARCHAR(50)
From the above query declaration new column “Manager_Name” will add to emp_table with data typeVARCHAR(50)

Query to Drop or delete column from existing table

If we want to drop column from existing table that syntax will be like this


ALTER TABLE Table_Name DROP COLUMN COLUMN_NAME
Ex:


ALTER TABLE emp_table DROP COLUMN Manager_Name
From the above query declaration column “Manager_Name” will drop from emp_table

Query to modify existing column datatype in table

If we want to modify existing column datatype from data table that syntax will be like this


ALTER TABLE Table_Name ALTER COLUMN COLUMN_NAME DATATYPE
Ex:


ALTER TABLE emp_table ALTER COLUMN Manager_Name nvarchar(max)

From the above query declaration I changed “Manager_Name” column datatype from VARCHAR(50) tonvarchar(max)

No comments:

Post a Comment