gpt4 book ai didi

sql - SQL Server 中的条件约束

转载 作者:搜寻专家 更新时间:2023-10-30 19:47:47 25 4
gpt4 key购买 nike

我在创建包含在 Microsoft SQL Server 表创建中的条件约束时遇到问题。我就是想不通。

我试过 case 语句、if then 语句,似乎都不起作用。

这是否可能因为我不断收到语法错误?

CREATE TABLE SECTION
(
c# int FOREIGN KEY REFERENCES Course(c#),
se# int,
emp# int FOREIGN KEY REFERENCES Professor(EMP#),
class_time time,
controlNumber int DEFAULT 20,
CONSTRAINT pk_SectionID PRIMARY KEY (c#, se#),
CONSTRAINT chk_controlNumber CHECK (controlNumber >= 40 AND controlNumber <=60),

CONSTRAINT chk_c# CHECK (when c# between 3000 and 5000 then controlNumber <=40)
);

谢谢。

最佳答案

你需要结合你的两个检查,否则它们是矛盾的,然后用简单的 bool 逻辑将它们结合起来:

CREATE TABLE SECTION(
c# int FOREIGN KEY REFERENCES Course(c#),
se# int,
emp# int FOREIGN KEY REFERENCES Professor(EMP#),
class_time time,
controlNumber int DEFAULT 20,
CONSTRAINT pk_SectionID PRIMARY KEY (c#, se#),
CONSTRAINT chk_controlNumber CHECK (
controlNumber between 40 AND 60 and (c# < 3000 or c# > 5000) or
(c# between 3000 and 5000 and controlNumber <= 40))
);

关于sql - SQL Server 中的条件约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36349591/

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