SQL oracle add check constraint to an existing table SQL oracle add check constraint to an existing table oracle oracle

SQL oracle add check constraint to an existing table


Your constraint looks right, I have tested it:

create table appeal ( OpenDate date,  CloseDate date);alter table appealadd constraint Check_Datescheck (OpenDate < CloseDate);insert into appeal values ( sysdate, sysdate - 1 );

And here the result:

Schema Creation Failed: ORA-02290: check constraint (USER_4_44096.CHECK_DATES) violated

Problem is than you have already rows with OpenDate < CloseDate values in your database. Fix it before create constraint. Look behavior changing sentences order:

create table appeal ( OpenDate date,  CloseDate date);insert into appeal values ( sysdate, sysdate - 1 );alter table appealadd constraint Check_Datescheck (OpenDate < CloseDate);

And here your issue:

Schema Creation Failed: ORA-02293: cannot validate (USER_4_E4450.CHECK_DATES) - check constraint violated


Try thisalter table appealadd constraint Check_Datescheck (OpenDate < CloseDate) ENABLE NOVALIDATE;

You will have check the previous data for errors but any new Data will fall under the CHECK