Question

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.

Was it helpful?

Solution

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))

);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top