views:

53

answers:

1

Alright, since a client wants to automate a certain process, which includes creating a new key structure in a LIVE database, I need to create relations between tables.columns. Now I've found the tables ALL_CONS_COLS en USER_CONSTRAINTS to hold information about constraints. If I were to manually create constraints, by inserting into these tables, I should be able to recreate the original constraints. My question: are there any more tables I should look into? Do you have an alternate suggestions, as this sounds VERY dirty and error prone to begin with.

Current modus operandi:

  • Create a new column in each table for the PK;

  • Generate a guid for this PK;

  • Create a new column in each table for the FKs;

  • Fetch the guid associated with the
    FK;

....... done sofar......

  • Add new constraint based on the old
    one;

  • Remove old constraint;

  • Rename new columns;

This is kind of dodgy and I'd rather change my method, any ideas would be helpful.

To put it different, client wants to change key structure from int to guid on a live database. What's the best way to approach this

+4  A: 

First of all, you don't create/modify/drop constraints by messing with the data dictionary, but with the regular ALTER TABLE t ADD/MODIFY/DROP CONSTRAINT syntax.

In your scenario, I think you should use the following scenario:

1) Make sure no-one modifies data while this operation is going on

2) Drop the old foreign key constraints first to avoid name clashing with the new constraints.

3) Drop the old primary key constraints

4) Create the new primary key constraints on the guid columns

5) Create the new foreign key constraints

And then you are done.

Regards, Rob.

Rob van Wijk
Alright, ALTER TABLE it is, but how do I go about automating it to copying existing relations? The only way I can imagine is querying for relations in the all_cons_cols and user_constraints table, making a copy of it, for the 'new' columns and DROPping the 'old' ones.
Oxymoron
Create a script that generates the commands for you. Something like "select 'alter table ' || table_name || ' drop constraint ' || constraint_name || ';' from user_constraints where constraint_type = 'P'" to drop all primary keys. And something similar to drop the foreign keys and to create the new constraints.
Rob van Wijk
So I do need to user the user_constraints table exactly as I'm doing now ;)Bare in mind that we're talking about a live database, with records.DISABLE'ing or DROP'ing will not be easily possible due to references.
Oxymoron
What would be the best way to see in what tables a certain PF is referenced as an FK.Say I have the table 'USER' with a 'USER_ID' as a PK, how should I figure out in what other tables that 'USER_ID' column is referenced.That way I can start disabling references at 'the bottom'.
Oxymoron
Hmm, the client has a rigid naming convention which I can use to my advantage here.
Oxymoron
You can do it the "stupid" way, by repeatedly executing the constraint dropping until they are all gone, or you can do it the smart way and use the r_constraint_name column to see if a primary key is referenced by other foreign keys.
Rob van Wijk