检查三列是否全部为非空或空值。

8

我有一个包含4列的表:

create table dbo.Table ( 
  Id int not null,
  A int null,
  B int null,   
  C nvarchar (4000) null
)

如何确保ABC三个变量都是null或者都不是null

2个回答

13

你可以设置一个检查约束

constraint [check_abc] check ( ([A] is null and [B] is null and [C] is null) or
                               ([A] is not null and [B] is not null and [C] is not null) )

1

您也可以考虑将这些相关列分解到第二个表中,其中它们被声明为非空,并且只在适用时插入一行。

create table dbo.Table1( 
  Id int not null primary key
)


create table dbo.Table2( 
  Id int not null primary key references Table1,
  A int not null,
  B int not null,   
  C nvarchar (4000) not null
)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接