Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Foreign Key Constraints: Reverse Engineering
On 27 Jan 1998 17:43:20 GMT, "Harjit S. Batra" <hbatra_at_NOSPAM_cciosi.com> wrote:
>Does anybody know of a way to build a PL-SQL script from an existing
>database. I need a way to build a script that will contain ONLY the
>definitions of all the Foreign Key constraints that exist in a current
>database. I am using Oracle 7.3.
>
>An e-mail direct to me in addition to the NG post will be greatly
>appreciated !
>
>TIA
The following query will extract the foreign keys given a single parent table,
for example if you save the script to a file called cons.sql you can then:
SQL> create table p ( x int primary key ); Table created.
SQL> create table c ( a int primary key, b int references p ); Table created.
SQL> @cons p
old 53: and parent.parent_tname = upper('&1')
new 53: and parent.parent_tname = upper('p')
FKEY
So, the query should get you going...
column fkey format a80 word_wrapped
select
'alter table "' || child_tname || '"' || chr(10) || 'add constraint "' || child_cons_name || '"' || chr(10) || 'foreign key ( ' || child_columns || ' ) ' || chr(10) || 'references "' || parent_tname || '" ( ' || parent_columns || ');' fkeyfrom
b.r_constraint_name parent_cons_name, max(decode(position, 1, '"'||column_name||'"',NULL)) || max(decode(position, 2,', '||'"'||column_name||'"',NULL)) || max(decode(position, 3,', '||'"'||column_name||'"',NULL)) || max(decode(position, 4,', '||'"'||column_name||'"',NULL)) || max(decode(position, 5,', '||'"'||column_name||'"',NULL)) || max(decode(position, 6,', '||'"'||column_name||'"',NULL)) || max(decode(position, 7,', '||'"'||column_name||'"',NULL)) || max(decode(position, 8,', '||'"'||column_name||'"',NULL)) || max(decode(position, 9,', '||'"'||column_name||'"',NULL)) || max(decode(position,10,', '||'"'||column_name||'"',NULL)) || max(decode(position,11,', '||'"'||column_name||'"',NULL)) || max(decode(position,12,', '||'"'||column_name||'"',NULL)) || max(decode(position,13,', '||'"'||column_name||'"',NULL)) || max(decode(position,14,', '||'"'||column_name||'"',NULL)) || max(decode(position,15,', '||'"'||column_name||'"',NULL)) || max(decode(position,16,', '||'"'||column_name||'"',NULL)) child_columns
max(decode(position, 1, '"'||column_name||'"',NULL)) || max(decode(position, 2,', '||'"'||column_name||'"',NULL)) || max(decode(position, 3,', '||'"'||column_name||'"',NULL)) || max(decode(position, 4,', '||'"'||column_name||'"',NULL)) || max(decode(position, 5,', '||'"'||column_name||'"',NULL)) || max(decode(position, 6,', '||'"'||column_name||'"',NULL)) || max(decode(position, 7,', '||'"'||column_name||'"',NULL)) || max(decode(position, 8,', '||'"'||column_name||'"',NULL)) || max(decode(position, 9,', '||'"'||column_name||'"',NULL)) || max(decode(position,10,', '||'"'||column_name||'"',NULL)) || max(decode(position,11,', '||'"'||column_name||'"',NULL)) || max(decode(position,12,', '||'"'||column_name||'"',NULL)) || max(decode(position,13,', '||'"'||column_name||'"',NULL)) || max(decode(position,14,', '||'"'||column_name||'"',NULL)) || max(decode(position,15,', '||'"'||column_name||'"',NULL)) || max(decode(position,16,', '||'"'||column_name||'"',NULL)) parent_columns
Thomas Kyte
tkyte_at_us.oracle.com
Oracle Government
Bethesda MD
http://govt.us.oracle.com/ -- downloadable utilities
Anti-Anti Spam Msg: if you want an answer emailed to you, you have to make it easy to get email to you. Any bounced email will be treated the same way i treat SPAM-- I delete it. Received on Wed Jan 28 1998 - 00:00:00 CST