When two tables are linked by a third table which contains one constraint built with the two fk, is it a good practice to use on delete cascade or a join delete ?
create T1 (
id1 ...
...
CONSTRAINT pk_id1 PRIMARY KEY (id1) );
create T2 (
id2 ...
...
CONSTRAINT pk_id2 PRIMARY KEY (id2) );
create T3 (
id1 ...,
id2 ...,
CONSTRAINT pk_T3 PRIMARY KEY (id1, id2),
CONSTRAINT fk_T3_1 FOREIGN KEY (id2)
REFERENCES T2 (id2) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT fk_T3_2 FOREIGN KEY (id1)
REFERENCES T1(id1) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE )
Some people say it's dangerous to use on delete cascade, then is a join delete less dangerous ?
Edit: I didn't find the correct query to delete:
if I do
delete from T1 where id1=$id1
then the row $id1 is deleted from T1 and the row $id1,$id2 from T3 BUT not $id2 from T2.
If I do
delete from T3 where id1=$id1 and id2=$id2
then the row $id1 is not deleted in T1, the row $id2 is not deleted in T2, the couple ($id1,$id2) is deleted in T3.
How can I delete in ONE query ?
Edit: I thought to find a trick by moving one of the constraint in the T2 table, like so:
create T1 (
id1 ...
...
CONSTRAINT pk_id1 PRIMARY KEY (id1) );
create T2 (
id2 ...
...
CONSTRAINT pk_id2 PRIMARY KEY (id2) ),
CONSTRAINT fk_T2 FOREIGN KEY (id2)
REFERENCES T3 (id2) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE);
create T3 (
id1 ...,
id2 ...,
CONSTRAINT pk_T3 PRIMARY KEY (id1, id2),
CONSTRAINT fk_T3_2 FOREIGN KEY (id1)
REFERENCES T1(id1) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE );
But I have the message "No unique constraint ... for the referenced T3 table"