How do add a field – DOC with Date type to the Registration table. Also DOC (Date of Completion) should be greater than DOJ (Date of joining).
I wrote this query but it is showing error : Column check constraint cannot reference other column
ALTER TABLE Registration add DOC date CHECK(DOC > DOJ);
Advertisement
Answer
Add both the DOC column and the CHECK constraint with a single ALTER TABLE statement (see DBfiddle here) :
-- table for testing create table atable( doj date ) ; alter table atable add ( doc date , constraint checkdoc check( doc > doj ) -- cannot be coded *inline* ); -- testing ----------------------------------------------------- -- this insert must succeed insert into atable( doj, doc ) values ( sysdate, sysdate + 1 ); -- this insert must fail due to the CHECK constraint insert into atable( doj, doc ) values ( sysdate + 1, sysdate ) ; -- ORA-02290: check constraint (...CHECKDOC) violated -- table/data -------------------------------------------------- select * from atable ; DOJ DOC 03-JUN-20 04-JUN-20