Code snippets to find constraints and table relationships in an Oracle database.
-- make the current session case-insensitive (in case your tables or columns have FancyNamesLikeThis)
alter session set nls_sort=BINARY_CI;
alter session set nls_comp=LINGUISTIC;
-- list all tables in the database accessible by the current user
SELECT
owner
, table_name
, num_rows
FROM
all_tables
;
-- list all tables that contain a column with name
SELECT
table_name
, column_name
, data_type
, nullable
FROM
all_tab_columns
WHERE
column_name = 'PICK_COLUMN'
;
-- select all primary keys
SELECT
all_constraints.constraint_type
, all_cons_columns.table_name
, all_cons_columns.column_name
, all_constraints.constraint_name
, all_cons_columns.position
, all_constraints.status
, all_cons_columns.owner as schema_name
FROM
all_constraints
JOIN
all_cons_columns
ON
all_constraints.constraint_name = all_cons_columns.constraint_name
and all_constraints.owner = all_cons_columns.owner
WHERE
all_constraints.constraint_type = 'P'
ORDER BY
all_cons_columns.owner
, all_cons_columns.table_name
, all_cons_columns.position
;
-- select related tables
SELECT
p.table_name
, c.table_name
, c.column_name
, 'Select count(*) from '
|| F.table_name
|| ' where '
|| c.column_name
|| ' = ' strSql
FROM
user_constraints P
, user_constraints F
, user_cons_columns c
WHERE
P.constraint_name = F.r_constraint_name
AND f.constraint_name = C.constraint_name
AND F.table_name = C.table_name
AND p.table_name IN UPPER(('TABLE_NAME_GOES_HERE'))
;
-- list Schema Objects
SELECT
object_type
, object_name
, status
FROM user_objects
ORDER BY
object_type
, object_name
;
-- generate insert script
SELECT
'insert into (' ||
user_tab_columns.table_name ||
LISTAGG(column_name, ', ') WITHIN GROUP (ORDER BY column_name ) ||
') values (' ||
LISTAGG(nullable || '_' || data_type, ', ') WITHIN GROUP (ORDER BY column_name ) ||
');' as insert_command
FROM user_tab_columns
WHERE table_name = upper('TABLE_NAME_GOES_HERE')
GROUP BY table_name;
-- Find field usage in procedures, functions, views...
SELECT DISTINCT
a.type, a.name, a.owner, a.text
FROM all_source a -- or --FROM dba_source
WHERE
a.type IN ('PROCEDURE','FUNCTION','PACKAGE BODY', 'TRIGGER', 'VIEW') -- types
--and
--a.owner = 'SCHEMA_NAME' -- schema
AND
a.text LIKE '%TABEL_NAME%' -- table name
AND
text LIKE '%FIELD_NAME%' -- field name
ORDER BY a.name, a.type;
SELECT *
FROM user_constraints natural join user_cons_columns
WHERE table_name = UPPER('e_table_name');