我有一张桌子

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/

10-11 04:51