gpt4 book ai didi

SQL Server : how to force NOT NULL in a column only if another column in the same table is a specific value?

转载 作者:行者123 更新时间:2023-12-03 23:15:52 24 4
gpt4 key购买 nike

我对 SQL 很陌生,因此我们将不胜感激。

我有下表:

enter image description here

我需要在 BusinessName 上强制 NOT NULL和 BusinessType ,但前提是 CustomerType = 'Business' .我已经在 CustomerType 上添加了一个约束。列使只有BusinessPersonal将被接受。

如果用户想设置个人账户,在BusinessName中将接受NULL。和 BusinessType列。

最佳答案

像这样向现有表添加约束

ALTER TABLE Customers ADD CONSTRAINT Con_First check
(
CustomerType = 'Business' AND BusinessName IS NOT NULL AND BusinessType IS NOT NULL
OR CustomerType <> 'Business' -- BusinessName and BusinessType can be null or not null, we omit AND here
)

关于SQL Server : how to force NOT NULL in a column only if another column in the same table is a specific value?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50896220/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com