Oracle/SQL -- self referential tables

Status
Not open for further replies.

jaziniho

Smack-Fu Master, in training
51
Hi,<BR><BR>I'm in the process of building a SQL clean-up script that will be removing lots of now redundant data from our Oracle DB.<BR><BR>It seems that for tables which contain a FK pointing back to themselves, there is a difficulty regarding this FK constraint. As far as I'm aware the best option here is to temporarily disable the constraint, perform the delete, then re-enable.<BR><BR>So, my first question:<BR><BR>o Is there a better way to go about doing this?<BR>I read briefly about CASCADE on delete, however it seems that once this has been added the only way to remove it is by deleting and recreating the constraint.<BR><BR><BR>My second question:<BR><BR>o What's the easiest way for me to identify all self referential tables?<BR>I'm aware that Oracle maintains a lot of internal data in tables which can be read, but I know little about these. It occurred to me that there might be a table listing constraints and I might be able to do a SELECT / WHERE on such a table if available.<BR><BR><BR>If anyone takes an interest I'd also like to post up a small sample of the clean-up code for someone to take a look at -- I'd really like to know whether you think I'm going about this the right way! This will certainly be the most complicated SQL i've implemented by far. (In fact I actually have started the script itself in PL/SQL...)
 
If all else fails, you can write a routine that starts out with a set of IDs of rows to delete and computes the "closure" of the set with respect to references; that is, recursively adds to the set all the IDs of rows in the same table referenced by elements of the set. Then, you should be able to delete all rows whose IDs are in the "closed" set in one shot.
 

technophile

Ars Legatus Legionis
23,768
<BLOCKQUOTE class="ip-ubbcode-quote"><div class="ip-ubbcode-quote-title">quote:</div><div class="ip-ubbcode-quote-content">Originally posted by newwb:<BR>If all else fails, you can write a routine that starts out with a set of IDs of rows to delete and computes the "closure" of the set with respect to references; that is, recursively adds to the set all the IDs of rows in the same table referenced by elements of the set. Then, you should be able to delete all rows whose IDs are in the "closed" set in one shot. </div></BLOCKQUOTE><BR>You can also do a loop that deletes all the items that don't have any references (i.e. leaf nodes) as long as the last operation found any to delete. (pseudocode below.) That may work in cases where the closure is too large or results in too big a performance hit.<BR><BR><pre class="ip-ubbcode-code-pre">
do
delete all nodes that do not have children
set @num = count of nodes deleted
while @num > 0
</pre><BR>In T-SQL I would use @@ROWCOUNT for the second step, in Oracle I'm not sure if SQL%ROWCOUNT would work; if not, you can add a SELECT COUNT(1) above the delete with the same where clause to get the count.
 

jaziniho

Smack-Fu Master, in training
51
newwb, I see what you mean yes, that would be possible. Current intention is to run the script during the quarterly outage window, so temporarily removing constraints could be okay. If the script takes a long time to run then it won't fit in outage window.<BR><BR>Trombone8vb: In the order of tens of thousands records.<BR><BR>It will be used very much as one off, run once on each system.<BR><BR>just prepping a sample of my intentions to include.
 

jaziniho

Smack-Fu Master, in training
51
So, maybe this is going slightly at a tangent to my original question. This code is my general tactic for tracking down removing FK dependencies. <BR><BR>I'm still considering method for self referential tables.<BR><BR><BR>There may be some syntax errors, but not too concerned there atm. <BR><BR> <pre class="ip-ubbcode-code-pre">
/****** deleteCarSubtype ******/
CREATE OR REPLACE PROCEDURE deleteCarSubtype(carType IN t_zz_car_type.code%TYPE) AS
CURSOR carSubtypeCursor IS
SELECT code, id FROM t_zz_car_subtype
WHERE t_zz_car_type_code = carType
FOR UPDATE;

subtypeCode t_zz_car_subtype.code%TYPE;
this_id t_zz_car_subtype.id%TYPE;

BEGIN
/* deleteCarSubtype procedure body */
OPEN carSubtypeCursor;
LOOP
FETCH carSubtypeCursor INTO subtypeCode, this_id;
EXIT WHEN carSubtypeCursor%NOTFOUND;
/* Further calls to procedures for deleting dependencies here */
DELETE FROM t_zz_car_subtype where id=this_id;
END LOOP;
CLOSE carSubtypeCursor;
END deleteCarSubtype;
/


/****** deleteCarType ******/
CREATE OR REPLACE PROCEDURE deleteCarType(cc IN t_zz_manufacturers.t_zz_manufacturer_code%TYPE) AS
/* all car types for manufacturer cc */
CURSOR carTypeCursor IS
SELECT code FROM t_zz_car_type
WHERE t_zz_manufacturer_code = cc
FOR UPDATE;

carType t_zz_car_type.code%TYPE;
carSubtype t_zz_car_subtype.code%TYPE;

BEGIN
/* procedure body here */
OPEN carTypeCursor;
LOOP
FETCH carTypeCursor INTO carType;
EXIT WHEN carTypeCursor%NOTFOUND;
/* delete ecarh subtype associated with this type */
deleteCarSubtype(carType);
END LOOP;
DELETE FROM t_zz_car_type WHERE t_zz_manufacturer_code = cc;
CLOSE carTypeCursor;
END deleteCarType;
/

DECLARE

manufacturerCode t_zz_manufacturers.t_zz_manufacturer_code%TYPE := '6X';


/* define functions for use */
BEGIN

deleteCarType(manufacturerCode);
END;

/

</pre><BR><BR>Just a quick cap on what it does:<BR><BR>Define two procedures: deleteCarType, deleteCarSubtype.<BR><BR>Car Subtype table has an FK referencing car type.<BR><BR>The deleteCarType procedure calls deleteCarSubtype to ensure child dependencies are delete, before deleting its own records. Likewise when fully implemented the car sub type record will call procedures will call many procedures to do the same for itself and so and so forth.
 
<BLOCKQUOTE class="ip-ubbcode-quote"><div class="ip-ubbcode-quote-title">quote:</div><div class="ip-ubbcode-quote-content">Originally posted by technophile:<BR>You can also do a loop that deletes all the items that don't have any references (i.e. leaf nodes) as long as the last operation found any to delete. (pseudocode below.) That may work in cases where the closure is too large or results in too big a performance hit.<BR><BR><pre class="ip-ubbcode-code-pre">
do
delete all nodes that do not have children
set @num = count of nodes deleted
while @num > 0
</pre><BR>In T-SQL I would use @@ROWCOUNT for the second step, in Oracle I'm not sure if SQL%ROWCOUNT would work; if not, you can add a SELECT COUNT(1) above the delete with the same where clause to get the count. </div></BLOCKQUOTE><BR>Yes, but if you initially know the IDs of rows you want to delete, you'll have to trace the references back to the "leaf nodes", which effectively computes the "closure". Therefore you're kind of specifying an algorithm to delete the closure, although using less memory than computing the entire set first and then deleting it (which may be a valuable optimization).
 
<BLOCKQUOTE class="ip-ubbcode-quote"><div class="ip-ubbcode-quote-title">quote:</div><div class="ip-ubbcode-quote-content">Originally posted by jaziniho:<BR>Current intention is to run the script during the quarterly outage window, so temporarily removing constraints could be okay. </div></BLOCKQUOTE><BR>The problem with this approach is that you may end up with dangling references. I suppose that with foreign key checking disabled, you can delete any row and then follow its references to other rows and delete them too recursively, thereby deleting a "closed" set of rows incrementally.
 

jaziniho

Smack-Fu Master, in training
51
<BLOCKQUOTE class="ip-ubbcode-quote"><div class="ip-ubbcode-quote-title">quote:</div><div class="ip-ubbcode-quote-content"> quote:<BR> Originally posted by jaziniho:<BR> Current intention is to run the script during the quarterly outage window, so temporarily removing constraints could be okay. <BR><BR><BR>The problem with this approach is that you may end up with dangling references. I suppose that with foreign key checking disabled, you can delete any row and then follow its references to other rows and delete them too recursively, thereby deleting a "closed" set of rows incrementally. </div></BLOCKQUOTE><BR><BR>Yes, actually I suppose with only minor modifications the structure I have above could work for this. Opening a cursor 'WHERE parent_id=id' and recursively call the same delete function on each?
 
Status
Not open for further replies.