Adding or Modifying a Column of a Table in SQL Server Using Command


You can add or modify columns in sql server using simple queries given below:

Adding a Column to a table:

ALTER TABLE Table_Name
ADD NewColumnName CHAR(50) DEFAULT '' NOT NULL

MODIFYING A COLUMN IN A TABLE:

ALTER TABLE Table_Name
ALTER COLUMN Table_Column_Name VARCHAR(50) NULL

Below are the points which you need to consider before altering table column:

  • Reducing length of column for eg. changing length of column from varchar(50) to varchar(20) will lead to data loss ie. data will be truncated. You should be aware of impact on data after altering table column.
  • You cannot alter column which is a primary key.

 

Tags: , , , ,

Comments & Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

*


5 + = 6

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>