Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have create table scripts and alter table scripts with some constraints for that database.

It take time as twice to create the table and alter the table .. Is this possible to combine

create table and alter table constraints with create table sql server 2008

share|improve this question
Please show your create and alter scripts. – Martin Smith Jan 21 '12 at 10:14

Here's an example of creating a constraint at the same time as a table:

create table Table1
multiplier1 int,
multiplier2 int,
answer int,
constraint PK_Table1 primary key(multiplier1, multiplier2),
constraint CHK_Table1_Answer check (answer = multiplier1 * multiplier2),
check (answer / multiplier1 = multiplier2)

Per @Martin Smith's comment, the constraint name is optional. The database will generate a name for the third constraint, like CK__Table1__22AA2996.

Constraints that are introduced on their own line can can refer to multiple columns.

share|improve this answer
They don't have to be named. The [constraint constraint_name] bit is optional. – Martin Smith Jan 21 '12 at 12:04
Also, whether column level or table level, the constraint name is always optional – gbn Jan 21 '12 at 12:07
@MartinSmith: Interesting, info added to the question – Andomar Jan 21 '12 at 12:08
Just want to interject that it's usually a best practice to name your constraints. It's much easier of course to use the simpler syntax and leave them out, but if you need to build change scripts it is much easier to rely on a standard naming scheme than having to determine the system-generated name from the metadata. The standard you use doesn't matter, as long as it's consistent and documented. – Aaron Bertrand Jan 21 '12 at 17:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.