sql-server - 某些列应全部填充或全部为空
问题描述
我的表(10 列)有 4 列(A、B、C、D),应该全部为空或全部填充。
我尝试按以下方式进行:
constraint chk_same check (A is not null AND B is not null And C is not null AND is not null) OR (A is null AND B is null And C is null AND D is null)
看起来很糟糕,有没有更好/更简单的方法来做到这一点?
解决方案
你的方法很好。更通用的方法是计算NULL
值的数量并检查:
constraint chk_same
check ( ((case when A is null then 1 else 0 end) +
(case when B is null then 1 else 0 end) +
(case when C is null then 1 else 0 end) +
(case when D is null then 1 else 0 end)
) in (0, 4)
) ;
这更通用,因为您可以轻松检查 4 列中的 2 列或 4 列中的 3 列是否具有NULL
值。
推荐阅读
- docker-compose - 从另一个 docker-compose 文件导入命名卷
- linux - 在 Linux 上替换字符 x86 NASM 时出现分段错误
- mysql - LEFT JOIN WHERE 不返回结果
- colors - THREE.js 无法将渐变颜色应用于导入的 OBJ 文件
- c# - 从重定向 url 获取令牌
- dart - 如何比较两个列表中的值
- > 在飞镖?
- amazon-web-services - 附加运行 AWS ECS 任务(未分离)
- winapi - Win32:GetIpNetTable2() 可以用来返回网络上所有计算机的列表吗?
- c# - 将自定义光标添加到画布
- docker - 我想用docker启动adonisjs环境