简体   繁体   中英

Constraint for a column(1) that checks the values in another column(2) are equal to 'String1' and 'String2', setting column(1) to not null

How do you create a constraint for a column(1) that checks the values in another column(2)and is equal to 'String1' and 'String2' but not 'String3', therefore setting column(1) to not null This is what I have tried so far

    Create Table ER(ERNo INTEGER,
ERSta Varchar2(8 CHAR) Default 'PENDING' Not Null,  
AUserNo Integer,
CONSTRAINT ERNoPK PRIMARY KEY(ERNO),
CONSTRAINT ERSta_check CHECK (ERSta IN ('PENDING', 'APPROVED', 'DENIED')),
CONSTRAINT AUserNoFK FOREIGN KEY (AUserNo) REFERENCES Users(UserNo),
CONSTRAINT AUserNo CHECK (WHEN (ERSta like('APPROVED')or ('DENIED')) THEN AUserNo not null);

So my question is: As AuserNo allows nulls already, How can I Set AUserNo to not null when either 'APPROVED' or 'DENIED' has been entered in ERSta?

Thanks in advance for your help.

check (ERSta NOT IN ('APPROVED', 'DENIED') or AUserNo is not null)
  1. The check clause will be fulfilled if ERSta is another value than APPROVED/DENIED.

  2. If ERSta is APPROVED or DENIED, then AUserNo must be not null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM