简体   繁体   中英

How do I get my SQL to cascade only on certain conditions?

How do I get my SQL to cascade only on certain conditions?

I want the child tuple to be deleted on cascade when parent column1 value is deleted only if column2C of CHILD is NOT equal to NULL .

Example :

create PARENT table(
    column1 varchar2(20) primary key, 
    column2 varchar2(20)
);

create CHILD table(
    column1C varchar2(20), 
    column2C varchar2(20) default NULL, 
    primary key(column1C,column2C)
);

alter table CHILD
    add constraint fk_const
    foreign key(column1C) references parent(column1)
    delete on cascade;

you can do it by means of triggers. more specifically you must write a trigger for the after delete event of table parent to check the condition and if it is true then delete the corresponding records in child table

Did you try using triggers? It would be simpler, transparent and you can disable them if you dont need them without modifying the definition. Give a proper example if needed and try creating them.

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