简单语法:Alter Table语句中的多个约束

时间:2011-01-23 04:32:00

标签: sql-server-2008

如果我有这样的陈述:

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

如何在此语句中添加其他约束?这甚至可能吗?

由于

使用SQL SERVER 2008 Developer Edition

4 个答案:

答案 0 :(得分:11)

添加逗号,然后添加新约束(不添加关键字),如下所示:

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)

答案 1 :(得分:6)

稍微更改布局并阅读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 (...)

编辑:使用WITH CHECK确保约束有效......

答案 2 :(得分:2)

由于此问题的标题并未引用SQL Server,因此我打开它以查找Oracle DB的解决方案,该解决方案与接受的答案不同。所以我会把它留在下面,以防其他人跟着我的步骤。

对于Oracle DB:

ALTER TABLE RecipeBox.Recipe
ADD (CONSTRAINT your_first_constraint, CONSTRAINT your_second_constraint);

答案 3 :(得分:1)

ALTER TABLE RecipeBox,Recipe WITH CHECK 
ADD   CONSTRAINT Cons_1 UNIQUE CLUSTERED
(
    Col1,
    Col2
)

如果是检查约束,您还可以使用规则