10

If I have this statement:

ALTER TABLE RecipeBox.Recipe ADD CONSTRAINT AKRecipeBox_Recipe_Name 
UNIQUE NONCLUSTERED (Name)

How do I add another constraint to this statement? Is this even possible?

Thanks

Using SQL SERVER 2008 Developer Edition

BenV
  • 12,052
  • 13
  • 64
  • 92

4 Answers4

13

Add a comma, then add the new constraint (without add keyword) like this:

ALTER TABLE RecipeBox.Recipe ADD CONSTRAINT AKRecipeBox_Recipe_Name 
UNIQUE NONCLUSTERED (Name), 
CONSTRAINT your_constraint UNIQUE NONCLUSTERED (yourColumn) -- (or whatever type of constraint you're looking for)
jlnorsworthy
  • 3,914
  • 28
  • 34
7

Change the layout slightly and read ALTER TABLE

ALTER TABLE RecipeBox.Recipe WITH CHECK ADD
    CONSTRAINT AKRecipeBox_Recipe_Name UNIQUE NONCLUSTERED (Name),
    CONSTRAINT FK_foo_bar FOREIGN KEY ..., 
    CONSTRAINT CK_foo_bar CHECK (...)

Edit: use WITH CHECK to ensure the constraints are valid...

gbn
  • 422,506
  • 82
  • 585
  • 676
4

Since the title of this question does not refer to SQL Server, I opened it to find a solution for Oracle DB which is different from accepted answer. So I will leave it below in case someone else follows my steps.

For Oracle DB:

ALTER TABLE RecipeBox.Recipe
ADD (CONSTRAINT your_first_constraint, CONSTRAINT your_second_constraint);
G. Urikh
  • 85
  • 7
1
ALTER TABLE RecipeBox,Recipe WITH CHECK 
ADD   CONSTRAINT Cons_1 UNIQUE CLUSTERED
(
    Col1,
    Col2
)

In case of a check constraint you can also use a rule

Florent
  • 12,310
  • 10
  • 49
  • 58
Abhimanyu
  • 11
  • 1