Subject Re: [firebird-support] Re: Poor selectivity in foreign keys
Author Salvatore Besso
hello John,

thank you for your reply, but maybe I was not so clear in my message. I'm using
these foreign keys for the sole scope to have cascaded deletes and effectively
some of the foreign tables are only lookup tables. For example:

ALTER TABLE Qso
ADD CONSTRAINT FK_Qso
FOREIGN KEY(Log_Owner) REFERENCES Anagraphics(ID_Anagraphic)
ON DELETE CASCADE;

ALTER TABLE Province_Totals
ADD CONSTRAINT FK_Province_Totals
FOREIGN KEY(Log_Owner) REFERENCES Anagraphics(ID_Anagraphic)
ON DELETE CASCADE;

ALTER TABLE Region_Totals
ADD CONSTRAINT FK_Region_Totals
FOREIGN KEY(Log_Owner) REFERENCES Anagraphics(ID_Anagraphic)
ON DELETE CASCADE;

ALTER TABLE Log_Totals
ADD CONSTRAINT FK_Log_Totals
FOREIGN KEY(Log_Owner) REFERENCES Anagraphics(ID_Anagraphic)
ON DELETE CASCADE;

Doing this with a simple:

DELETE FROM ANAGRAPHICS WHERE ID_ANAGRAPHIC = :SOMEVALUE;

or

DELETE FROM ANAGRAPHICS;

I have deleted from or emptied five tables with only one statement.

But I think that I can achieve the same result with two SP's with separate
DELETE's statements:

DECLARE PROCEDURE DELETE_ALL
AS
BEGIN
DELETE FROM LOG_TOTALS;
DELETE FROM REGION_TOTALS;
DELETE FROM PROVINCE_TOTALS;
DELETE FROM QSO;
DELETE FROM ANAGRAPHICS;
END ^

DECLARE PROCEDURE DELETE_ONE(ID INTEGER)
AS
BEGIN
DELETE FROM LOG_TOTALS WHERE LOG_OWNER = :ID;
DELETE FROM REGION_TOTALS WHERE LOG_OWNER = :ID;
DELETE FROM PROVINCE_TOTALS WHERE LOG_OWNER = :ID;
DELETE FROM QSO WHERE LOG_OWNER = :ID;
DELETE FROM ANAGRAPHICS WHERE ID_ANAGRAPHIC = :ID;
END ^

Isn't it? In this SP's example integrity should not be broken because no orphan
records are left after the deletion.

Regards
Salvatore