30.04.2013 Views

Q&A with Juan Loaiza Stay Focused on the Five Steps ... - NoCOUG

Q&A with Juan Loaiza Stay Focused on the Five Steps ... - NoCOUG

Q&A with Juan Loaiza Stay Focused on the Five Steps ... - NoCOUG

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Most DBAs will know how to find out what foreign<br />

keys are defined <strong>on</strong> a table, and if <strong>the</strong>re<br />

are o<strong>the</strong>r tables in <strong>the</strong> database having foreign<br />

keys referencing <strong>the</strong> particular table. This informati<strong>on</strong><br />

is available in <strong>the</strong> all_c<strong>on</strong>straints and all_c<strong>on</strong>s_columns<br />

data dicti<strong>on</strong>ary views in every Oracle database. In a<br />

complex schema <str<strong>on</strong>g>with</str<strong>on</strong>g>out data model documentati<strong>on</strong> or any<br />

development tools such as TOAD around, sorting out foreign<br />

key relati<strong>on</strong>ships can be a bit challenging.<br />

It is not difficult to c<strong>on</strong>struct a SQL script to find this informati<strong>on</strong>,<br />

and indeed <strong>the</strong>re are many such scripts available<br />

<strong>on</strong> <strong>the</strong> Internet. A handy script will definitely help to keep <strong>the</strong><br />

DBA in good health, so we’ll present <strong>on</strong>e here. There is no<br />

software to install <strong>on</strong> your PC to use this script—just a quick<br />

login to <strong>the</strong> Oracle database <str<strong>on</strong>g>with</str<strong>on</strong>g> SQL*Plus; run <strong>the</strong> script<br />

shown below, and a “quick questi<strong>on</strong>” will be a resp<strong>on</strong>ded to<br />

by a quick answer. This script works <str<strong>on</strong>g>with</str<strong>on</strong>g> all releases of Oracle<br />

since Oracle 8.<br />

SET LINESIZE 132<br />

SET FEEDBACK OFF<br />

SET VERIFY OFF<br />

COLUMN c<strong>on</strong>_name FORMAT a30 HEADING “Foreign Key”<br />

COLUMN c<strong>on</strong>_col FORMAT a30 HEADING “Local Column”<br />

COLUMN r_c<strong>on</strong> FORMAT a65 WRAPPED HEADING “Refers To”<br />

COLUMN ref_tab FORMAT a30 HEADING “Referenced By”<br />

ACCEPT 1 PROMPT ‘Input table owner: ‘<br />

ACCEPT 2 PROMPT ‘Input table name: ‘<br />

SET HEADING OFF<br />

SELECT global_name || ‘ at ‘ || TO_CHAR (SYSDATE,<br />

‘DD-MON-YY HH24:MI:SS’)<br />

FROM global_name;<br />

SELECT ‘Foreign Key C<strong>on</strong>straints <strong>on</strong> ‘ || UPPER (‘&1’)<br />

|| ‘.’ || UPPER (‘&2’) || ‘:’<br />

FROM dual;<br />

SET HEADING ON<br />

BREAK ON CON_NAME<br />

SELECT A.c<strong>on</strong>straint_name c<strong>on</strong>_name, B.column_name<br />

c<strong>on</strong>_col, C.table_name || ‘.’ || C.c<strong>on</strong>straint_name ||’<br />

(‘ || C.c<strong>on</strong>straint_type || ‘)’ r_c<strong>on</strong><br />

FROM all_c<strong>on</strong>straints A, all_c<strong>on</strong>s_columns B, all_<br />

c<strong>on</strong>straints C<br />

WHERE A.owner = UPPER (‘&1’)<br />

AND A.table_name = UPPER (‘&2’)<br />

AND A.c<strong>on</strong>straint_type = ‘R’<br />

The <strong>NoCOUG</strong> Journal<br />

Bag ‘o Tricks<br />

by Danny Chow<br />

TIPS & TRICKS<br />

Danny Chow<br />

AND B.owner = A.owner<br />

AND B.table_name = A.table_name<br />

AND B.c<strong>on</strong>straint_name = A.c<strong>on</strong>straint_name<br />

AND C.owner = A.r_owner (+)<br />

AND C.c<strong>on</strong>straint_name = A.r_c<strong>on</strong>straint_name (+)<br />

ORDER BY c<strong>on</strong>_name, B.positi<strong>on</strong>;<br />

SET HEADING OFF<br />

SELECT ‘Table ‘ || UPPER (‘&1’) || ‘.’ || UPPER<br />

(‘&2’) || ‘ is Referenced by:’<br />

FROM dual;<br />

SET HEADING ON<br />

BREAK ON c<strong>on</strong>_col SKIP 1<br />

SELECT B.column_name c<strong>on</strong>_col, C.table_name ref_tab,<br />

C.c<strong>on</strong>straint_name c<strong>on</strong>_name<br />

FROM all_c<strong>on</strong>straints A, all_c<strong>on</strong>s_columns B, all_<br />

c<strong>on</strong>straints C<br />

WHERE A.owner = UPPER (‘&1’)<br />

AND A.table_name = UPPER (‘&2’)<br />

AND A.c<strong>on</strong>straint_type IN (‘P’, ‘U’)<br />

AND B.owner = A.owner<br />

AND B.c<strong>on</strong>straint_name = A.c<strong>on</strong>straint_name<br />

AND A.owner = C.r_owner (+)<br />

AND A.c<strong>on</strong>straint_name = C.r_c<strong>on</strong>straint_name (+)<br />

AND C.c<strong>on</strong>straint_type = ‘R’<br />

ORDER BY B.column_name, C.table_name;<br />

Sample output from this script is as follows.<br />

SQL> @table_fkey.sql<br />

Input table owner: test<br />

Input table name: invoices<br />

TESTDB.DBSPECIALISTS.COM at 01-DEC-06 15:39:41<br />

Foreign Key C<strong>on</strong>straints <strong>on</strong> TEST.INVOICES:<br />

Foreign Key Local Column Refers To<br />

------------ ------------ --------------------------<br />

INVOICES_FK1 USER_ID USERS.USERS_PK (P)<br />

INVOICES_FK2 PAY_TERMS PAY_TERMS.PAY_TERMS_PK (P)<br />

Table TEST.INVOICES is Referenced by:<br />

Local Column Referenced By Foreign Key<br />

------------ ------------- -----------------<br />

INVOICE_ID INVOICE_LINES INVOICE_LINES_FK1<br />

I hope that you find this script worthy of additi<strong>on</strong> to your<br />

toolbox. <br />

Danny Chow is a senior DBA <str<strong>on</strong>g>with</str<strong>on</strong>g> Database Specialists, a nati<strong>on</strong>wide<br />

provider of c<strong>on</strong>sulting and Oracle-managed services<br />

for companies <str<strong>on</strong>g>with</str<strong>on</strong>g> missi<strong>on</strong>-critical Oracle database systems.<br />

His email address is dchow@dbspecialists.com.<br />

Copyright © 2007, Danny Chow

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!