gpt4 book ai didi

sql - 检查多列约束

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

我有一个依赖的表

CREATE TABLE DEPENDANT (DEPENDANT_NM VARCHAR2(15) PRIMARY KEY,
DEP_GENDER CHAR(1), DEP_BDAT DATE, RELATED_HOW VARCHAR2 (8), ESSNO CHAR(9));
ALTER TABLE DEPENDANT ADD CONSTRAINT FK_ESSNO FOREIGN KEY (ESSNO)
REFERENCES HW_EMPLOYEE(SSNO);
ALTER TABLE DEPENDANT ADD CONSTRAINT CK_DEP_GENDER CHECK
(DEP_GENDER IN ('M','F'));

我需要添加一个约束,以确保女性家属只能被称为“女儿”或“配偶”,而男性家属只能被称为“儿子”或“配偶”

我是否使用像...这样的检查约束

ALTER TABLE DEPENDANT ADD CONSTRAINT CK_RELATEDF CHECK (DEP_GENDER = 'F' AND 
RELATED_HOW IN('Daughter' OR 'Spouse'));

最佳答案

你必须这样写:

ALTER TABLE DEPENDANT 
ADD CONSTRAINT CK_RELATEDF
CHECK (DEP_GENDER = 'F' AND RELATED_HOW IN ('Daughter', 'Spouse'));

编辑:您必须在一次检查中完成:

ALTER TABLE DEPENDANT ADD CONSTRAINT CK_RELATEDF
CHECK ((DEP_GENDER = 'F' AND RELATED_HOW IN ('Daughter', 'Spouse'))
OR (DEP_GENDER = 'M' AND RELATED_HOW IN ('Son', 'Spouse')));

关于sql - 检查多列约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19799907/

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