I have two tables. Those tables have two relation between them.
Table 1 * ID_XPTO (PK) * Detail Table 2 * ID_XPTO (FK) (PK) * ID_XPTO2 (FK) (PK)
Those two relations exists.
Table 1 -< Table2 Table 1 -< Table2
My question is that I need to delete some row in table 1. I’m currently doing,
declare @table Table (xptoTable2 int) insert into @table select ID_XPTO2 from Table2 where ID_XPTO = @ID_XPTO delete from Table2 where ID_XPTO = @ID_XPTO delete from Table where ID_XPTO in (select xptoTable2from @table)
I know that I could use ON DELETE SET NULL on table2. On that way I could then search for all rows with null value on ID_XPTO2 and delete them, but DBA does not wants to use it.
Is there some better solution to do this process?
You have these options:
Delete in two statements, as you are doing now. Delete from Table2 first.
Delete from two tables in one statement, if your brand of database supports multi-table
DELETEsyntax (e.g. MySQL). This is not standard SQL, but it is handy.Use cascading referential integrity constraints (I understand your DBA has nixed this option).
Write a trigger
BEFORE DELETEon Table1, to delete or set NULL any reference in Table2. Check with your DBA to see if this is any more acceptable than the cascading RI constraints.Finally, I would advise talking to your DBA and asking the same question you asked here. Find out what solution he/she would prefer you to use. Folks on StackOverflow can answer technical questions, but it sounds like you are dealing with an IT policy question.