0

For testing purposes, I would like to generate an insert script for all records in all tables associated with a particular record in one of the root tables. For example, I might have a "Participant" table, which has any number of associated entries in the "Documents" table, which in turn has any number of associated entries in the "PrintRequests" table and so on and so forth. I have hundreds of these tables in the database.

Is there any way to select/script out all the records in all tables that are associated with for example ParticipantId = 1? This way, for a representative participant, I can extract all the associated records in all the tables.

One of my ideas was to restore a back up of the full database, modify all foreign key constraints to have cascade delete and then delete everything that is not participantid = 1 and let the database take care of deleting everything that is not related to the participant of interest and then script out the entire database of what remains.

For this, I might have to drop and recreate all the constraints, which I am unsure about how to do across the entire database.

Alternately, are there any other tools that would be able to do this? A migration tool for example that can take a query and only migrate the records and associated child records of that query?

2 Answers 2

0

While it is entirely possible to build scripts to walk through all the primary key and foreign key constraints and, via liberal use of dynamic SQL, generate these scripts, doing so would be a non-trivial undertaking. I would strongly suspect that you would be better served using a product like DataBee to generate your data subset.

7
  • Interesting tool. One of the primary reasons for using the production subset for us is that it makes testing easier. We can look at the production application and know for example that "John Smith" has 30 documents out of which 12 are purchase orders. Also, when users/testers submit bugs that say something like "Alice Smith's post code cannot be changed" we can replicate it more easily.
    – Chaitanya
    Jun 11, 2013 at 3:11
  • @Chaitanya - I know this is years after your comment, but I'm doing research on a new Redgate tool, SQL Clone, and it would help me to know why you're not currently using a full restore of production for testing. Thanks! Aug 11, 2016 at 16:21
  • @DavidAtkinson - Size and Time. Full restore of production db is fine for CI/UAT/Test environments, but not on dev's local machine. While debugging an issue or trying out new ideas/refactors, it is a pain to have to restore a full prod database.
    – Chaitanya
    Aug 11, 2016 at 23:56
  • @Chaitanya - That's interesting. The way it works with SQL Clone is that once an "image" of database exists (which is a one-off cost), multiple database clones can be created from that instantly, and take no additional storage requirements. We're hoping that this will solve this exactly problem. If you've still got this requirement, we'd love to invite you to join the preview program! Aug 12, 2016 at 10:37
  • @DavidAtkinson: I am not working with SQL on my current project so I wouldn't be much help. The clone feature sounds great. Will make writing integration tests etc. so much easier. I still see it as orthogonal to the thing I was trying to do here though. Often even one copy of a prod database could be quite huge for testing/local installation purposes. It would be nice to be able to pull out all entries related to one aggregate root (1 customer, with all their address details, all their orders and invoices related to those orders and payment details related to those invoices etc.)
    – Chaitanya
    Aug 15, 2016 at 4:55
0

You can script it creating dynamic SQL statements, but I think it is really lots of work. I think you will be faster to just find all the tables with a column "ParticipantId" with something like this

select * from all_tab_columns where column_name = 'PARTICIPANTID'

And then do some fast edit / replace / other sort of script action to generate yourself the delete statements.

Regarding the constraints. This is similar. Getting all the constraints with

SELECT owner, table_name, constraint_name
  FROM dba_constraints
 where table_name in (select table_name from all_tab_columns where column_name = 'PARTICIPANTID')

You switch on and off constraints using

ALTER TABLE <table name> ENABLE/DISABLE constraint <constraint name>;

Maybe this you could do with a loop. Borrowing from this page

begin
  for i in 
 (select constraint_name, table_name from user_constraints where table_name in (select table_name from all_tab_columns where column_name = 'PARTICIPANTID')
 ) LOOP
   execute immediate 'alter table '||i.table_name||' disable constraint '||i.constraint_name||'';
  end loop;
end;

I am not sure about your cascading delete thing but the above gives a bit an idea how the delete would look like:

begin
  for i in 
 (select constraint_name, table_name from user_constraints where table_name in (select table_name from all_tab_columns where column_name = 'PARTICIPANTID')
 ) LOOP
   execute immediate 'delete from '||i.table_name||' where participantid = ''1'' ';
  end loop;
end;

Hope it helps.

4
  • This will only work on tables that directly reference the participant table (i.e the child tables). But what about the tables that in turn reference those tables and so on and so forth (grand children, great-grand children etc.)? With nearly 600 tables in my application the web is pretty complex. I need something that can automatically walk this tree without me having to explicitly know or specify each of these relationships.
    – Chaitanya
    Jun 13, 2013 at 4:46
  • But how do they link together, can you give an example of two tables?
    – hol
    Jun 13, 2013 at 16:30
  • I provided one in the question. For example, a participant has any number of records in the "Document" table. Each of the "Document" records has any number of records in the "PrintRequest" table. So the "PrintRequest" table doesn't have a ParticipantId column, but it is related to the Participant via the Document table. There is a "Printers" table which in turn is referred to by the "PrintRequest" table. So before I can insert records into the "PrintRequest" table, I have to insert appropriate records into the "Printer" table, and so on and so forth.
    – Chaitanya
    Jun 13, 2013 at 22:57
  • I meant that you post the description of the tables in an edit in your question. Can you run DESC Document, DESC PrintRequest, DESC Printer. You can cut out the irrelevant part. Just that one can see what is the primary / foreign key linkage.
    – hol
    Jun 14, 2013 at 3:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.