Home > SQL server Basics > TSQL alter table add new column – constraint

TSQL alter table add new column – constraint

March 2, 2011 1 Comment

This is very basic sample for adding new column to a existing table and set default value by adding constraints.
You can do this via SQL Server Management Studio but this is TSQL example!

/*
This script will add new field IsManager to table Employee
and set default value to 0.
After creation of the field, run update statement to populate
IsManager =0 for existing record.
*/

Go
Alter Table tbl_Employee add [IsManager] [bit] NULL

Go
ALTER TABLE tbl_Employee ADD  CONSTRAINT [DF_Employee_IsManager]  DEFAULT ((0)) FOR [IsManager]

Go
UPDATE Employee
SET [IsManager]=0

Tags: defaultl value SQL server Basics TSQL alter table add new column - constraint

Comments:1

  1. SAM
    March 12, 2011 at 1:54 pm

    really useful

Leave a Reply

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

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>