Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

SQL help

The document provides SQL queries for identifying primary and foreign keys in Oracle tables, listing tables and their constraints, and finding references to specific tables. It includes instructions to ensure table names are in uppercase and offers examples of SQL statements for various tasks, such as finding common columns between two tables. Additionally, it outlines how to find tables based on column names and how to work with multiple tables for key identification.

Uploaded by

Nihon Jin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL help

The document provides SQL queries for identifying primary and foreign keys in Oracle tables, listing tables and their constraints, and finding references to specific tables. It includes instructions to ensure table names are in uppercase and offers examples of SQL statements for various tasks, such as finding common columns between two tables. Additionally, it outlines how to find tables based on column names and how to work with multiple tables for key identification.

Uploaded by

Nihon Jin
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

OBJECTIVE ACTION

Identify Primary key Make sure to type the table_name in uppercase, as Oracle stores all table names in
in a table uppercase:
-----------------------------------------------------------------------------------------------------
SELECT cols.table_name, cols.column_name, cols.position, cons.status, cons.owner
FROM all_constraints cons, all_cons_columns cols
WHERE cols.table_name = 'CASE_ENTRIES'
AND cons.constraint_type = 'P'
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner
ORDER BY cols.table_name, cols.position;
Identify Foreign key Make sure to type the table_name in uppercase, as Oracle stores all table names in
in a table uppercase:

Also replace, CONSTRAINT_TYPE as mentioned in below table


P Primary Key Object
R Referential AKA Foreign Key Column
U Unique Key Column

-----------------------------------------------------------------------------------------------------

SELECT * FROM ALL_CONS_COLUMNS A JOIN ALL_CONSTRAINTS C ON


A.CONSTRAINT_NAME = C.CONSTRAINT_NAME WHERE C.TABLE_NAME =
'BEN_TYPES' AND C.CONSTRAINT_TYPE = 'R';
List all tables, This statement lists all tables, constraint names, and foreign key table names
constraint names & (don’t change anything in below query):
foreign key table
names -----------------------------------------------------------------------------------------------------

select c.table_name,c.constraint_name,
cc.table_name
from all_constraints c
inner join all_constraints cc on c.r_constraint_name = cc.constraint_name
How can I find which To find which table references a given table in Oracle
tables reference a
given table in Oracle -----------------------------------------------------------------------------------------------------
SQL Developer?
select
owner,constraint_name,constraint_type,table_name,r_owner,r_constraint_nam
e
from all_constraints
where constraint_type='R'
and r_constraint_name in (select constraint_name
from all_constraints
where constraint_type in ('P','U')
and table_name='SCHED_EXP');
To Find Primary key This SQL query finds out primary key of a table.
of a table
-----------------------------------------------------------------------------------------------------
SELECT a.owner, a.table_name, b.column_name
FROM all_constraints a, all_cons_columns b
WHERE a.constraint_type='P'
AND a.constraint_name=b.constraint_name
AND a.table_name = 'CASE_BILLS';
Find Primary / Change table_name to add more tables
Unique / Foreign key
in multiple tables -----------------------------------------------------------------------------------------------------
(SELECT a.owner, a.table_name, b.column_name
FROM all_constraints a, all_cons_columns b
WHERE a.constraint_type IN ('P','R','U')
AND a.constraint_name=b.constraint_name
AND a.table_name in ('DD_HIST', 'CASE_BILLS'));
Finding a TABLE Finding TABLE(s) based on a COLUMN NAME
based on a -----------------------------------------------------------------------------------------------------
COLUMN NAME select distinct table_name from ALL_TAB_COLUMNS
where COLUMN_NAME = 'CASE_MBR_KEY'
and OWNER='COMPASS';
Need to work on Select a.column_name, b.table_name
from all_constraints b, all_cons_columns a
where b.constraint_type in ('P','R','U')
AND a.constraint_name = b.constraint_name
AND b.table_name in ('CASE_BILLS','DD_HIST') order by a.column_name
having count(a.column_name) > 1;
Find common Change table_name to add more tables
columns between -----------------------------------------------------------------------------------------------------
two tables SELECT column_name FROM all_tab_columns WHERE table_name =
'CASE_ENTRIES'
intersect
SELECT column_name FROM all_tab_columns WHERE table_name =
'TRANSACT_SCHED';

You might also like