문제

I am creating a table in oracle.

I have condition that data should be present at least one of columns and both columns cannot be empty.

CREATE TABLE testaccount
(
    request   VARCHAR2 (2048 BYTE) NOT NULL,
    response1  CLOB,   
    response2  CLOB,  
    CONSTRAINT pk_testaccount PRIMARY KEY
        (request)
);

Response1 and Response2 both cannot be null. Data must be there in one of the columns

Any condition to add it in oracle.

도움이 되었습니까?

해결책

CREATE TABLE testaccount
(
    request   VARCHAR2 (2048 BYTE) NOT NULL,
    response1  CLOB,   
    response2  CLOB,  
    CONSTRAINT pk_testaccount PRIMARY KEY (request), 
    constraint one_must_be_there check (not (response1 is null and response2 is null))
);

This will however allow both to be not null as well. It's not clear from your question if you also want to prohibit that as well.

To make sure only one of the columns is filled (and the other is NULL) you can use this:

CREATE TABLE testaccount
(
    request   VARCHAR2 (2048 BYTE) NOT NULL,
    response1  integer,   
    response2  integer,  
    CONSTRAINT pk_testaccount PRIMARY KEY (request), 
    constraint one_must_be_there check 
       (  (response1 is null and response2 is not null) or 
          (response1 is not null and response2 is null))

);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top