Finding Primary Keys in Oracle

A Query i use from time to time to find out which primary key columns are defined for a certain table:

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 = '_TABLE-NAME_'
AND cons.constraint_type = 'P' -- search for primary keys only
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner
AND cons.owner = '_SCHEMA_OWNER_'
ORDER BY cols.table_name, cols.position;

Query thanks to Ritchie

About javavolker71

when i do not write posts on this blog, don't work on or try to break my code i'm spending a reasonable amount of time
This entry was posted in Database, Oracle and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s