2010-09-30 11 views
10

¿Es posible en SQL Server 2008 crear una restricción que restrinja que dos columnas tengan valores NULL al mismo tiempo? De manera queRestricción NULL de SQL Server

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

Respuesta

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

Como parte de la creación de

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

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

+1: Alternativamente, 'COALESCE (columna 1, columna 2) no es null'. – RedFilter

+0

¡Genial! Muchas gracias. – Max

Cuestiones relacionadas