SQL Server NULL约束

时间:2010-09-30 18:14:25

标签: sql sql-server tsql sql-server-2008 constraints

SQL Server 2008中是否可以创建这样的约束,以限制两列同时具有NULL值?那么

Column1 Column2
NULL    NULL   -- not allowed
1       NULL   -- allowed
NULL    2      -- allowed
2       3      -- allowed

1 个答案:

答案 0 :(得分:14)

ALTER TABLE MyTable WITH CHECK 
   ADD CONSTRAINT CK_MyTable_ColumNulls CHECK (Column1 IS NOT NULL OR Column2 IS NOT NULL)

作为创作的一部分

CREATE TABLE MyTable (
 Column1 int NULL,
 Column2 int NULL,

 CONSTRAINT CK_MyTable_ColumNulls CHECK (Column1 IS NOT NULL OR Column2 IS NOT NULL)
)