[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 281
  • Last Modified:

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?
0
Mr_Shaw
Asked:
Mr_Shaw
  • 4
2 Solutions
 
mayank_joshiCommented:
yes

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

Open in new window


0
 
mayank_joshiCommented:
e.g.:-

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

Open in new window

0
 
mayank_joshiCommented:
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

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now