Can I add a column and set the default value in one statement?

Can I add a column and set the default value in one statement?
Mr_ShawAsked:
Who is Participating?
 
mayank_joshiConnect With a Mentor Commented:
e.g.:-

ALTER TABLE Table1
ADD Col1 varchar(10)  NULL CONSTRAINT Def1 DEFAULT 'test'

Open in new window

0
 
mayank_joshiCommented:
yes

ALTER TABLE {TABLENAME} 
ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL} 
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}

Open in new window


0
 
mayank_joshiConnect With a Mentor Commented:
you must specify not null if you want the default value to be set in existing data.
0
 
Mr_ShawAuthor Commented:
thanks
0
 
mayank_joshiCommented:
anytime
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.