144

I need to get the name of the primary key column.

In the input, I only have the table name.

James Brown
  • 36,089
  • 7
  • 43
  • 59
Kirill A.
  • 1,695
  • 3
  • 13
  • 14

5 Answers5

247
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'
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner
ORDER BY cols.table_name, cols.position;

Make sure that 'TABLE_NAME' is in upper case since Oracle stores table names in upper case.

Richie
  • 9,006
  • 5
  • 25
  • 38
29

Same as the answer from 'Richie' but a bit more concise.

  1. Query for user constraints only

    SELECT column_name FROM all_cons_columns WHERE constraint_name = (
      SELECT constraint_name FROM user_constraints 
      WHERE UPPER(table_name) = UPPER('tableName') AND CONSTRAINT_TYPE = 'P'
    );
    
  2. Query for all constraints

    SELECT column_name FROM all_cons_columns WHERE constraint_name = (
      SELECT constraint_name FROM all_constraints 
      WHERE UPPER(table_name) = UPPER('tableName') AND CONSTRAINT_TYPE = 'P'
    );
    
Mat
  • 202,337
  • 40
  • 393
  • 406
My-Name-Is
  • 4,814
  • 10
  • 44
  • 84
  • @FearlessFuture For me it worked well. Can you describe your problem a bit more expressive? – My-Name-Is Apr 17 '15 at 19:38
  • I don't get any results from this query, but I do get results from the query for the accepted answer. – FearlessFuture Apr 17 '15 at 19:41
  • 2
    @FearlessFuture I assume that the constraint you are searching for isn't a user constraint. Replace `user_constraints` by `all_constraints`. – My-Name-Is Apr 18 '15 at 18:11
  • This causes trouble if you have a table with the same name in two or more schemas - need to also include owner in the join: `SELECT owner, column_name, position FROM all_cons_columns WHERE (owner, constraint_name) in (SELECT owner, constraint_name FROM all_constraints WHERE UPPER(table_name) = UPPER('&tableName') AND CONSTRAINT_TYPE = 'P') order by owner, position;` – Mark Stewart Feb 14 '19 at 21:38
  • 1
    @MarkStewart I aggree the second Query will not work. But your solution is inefficient. Try: "SELECT cols.column_name AS KEY_STREAM, '=' AS KEY_CONDITION, cols.column_name as KEY_LOOKUP, '' AS KEY_STREAM2 FROM user_constraints cons, user_cons_columns cols WHERE cons.constraint_name = cols.constraint_name and CONSTRAINT_TYPE = 'P' and cons.table_name = '' and cons.table_name = cols.table_name;" – Alexander Heim Apr 07 '20 at 06:58
  • Why do I get tables with multiple primary keys, some of which are non-unique? I assume this is some very bad implementation of compound keys? – Maile Cupo Nov 19 '20 at 18:41
  • @MaileCup, you are working with a PK consisting of multiple columns (composite key), in order to identify a record uniquely. I wouldn't say that it's a bad practice, but my optinion is that it's much easier to work with technical generated IDs. – My-Name-Is Dec 07 '20 at 13:41
3
Select constraint_name,constraint_type from user_constraints where table_name** **= ‘TABLE_NAME’ ;

(This will list the primary key and then)

Select column_name,position from user_cons_columns where constraint_name=’PK_XYZ’; 

(This will give you the column, here PK_XYZ is the primay key name)

Alexander Hartmaier
  • 2,178
  • 12
  • 21
3

Try This Code Here I created a table for get primary key column in oracle which is called test and then query

create table test
(
id int,
name varchar2(20),
city varchar2(20),
phone int,
constraint pk_id_name_city primary key (id,name,city)
);

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 = 'TEST' AND cons.constraint_type = 'P' AND cons.constraint_name = cols.constraint_name AND cons.owner = cols.owner  ORDER BY cols.table_name, cols.position;
sainu
  • 2,686
  • 3
  • 22
  • 41
Kobir
  • 31
  • 3
0

Save the following script as something like findPK.sql.

set verify off
accept TABLE_NAME char prompt 'Table name>'

SELECT cols.column_name
FROM all_constraints cons NATURAL JOIN all_cons_columns cols
WHERE cons.constraint_type = 'P' AND table_name = UPPER('&TABLE_NAME');

It can then be called using

@findPK
Bugalugs Nash
  • 492
  • 1
  • 6
  • 21