Kirill A.
Kirill A.

Reputation: 1815

How to get primary key column in Oracle?

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

In the input, I only have the table name.

Upvotes: 156

Views: 418577

Answers (6)

noobprogrammer1987
noobprogrammer1987

Reputation: 93

I prefer two versions of what @Richie said.

This is useful if you created a primary key with two values. It is difficult to read without this extra info.

SELECT cols.constraint_name, cons.index_name, cols.table_name, cols.column_name, cols.position, cons.status, cons.owner
FROM all_constraints cons, all_cons_columns cols
WHERE cols.table_name = UPPER('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;

If you want to read everything this is useful.

SELECT *
FROM all_constraints cons, all_cons_columns cols
WHERE cols.table_name = UPPER('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;

Upvotes: 1

Vipin Tiwari
Vipin Tiwari

Reputation: 31

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)

Upvotes: 3

Kobir
Kobir

Reputation: 41

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;

Upvotes: 4

Bugalugs Nash
Bugalugs Nash

Reputation: 492

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

Upvotes: 0

My-Name-Is
My-Name-Is

Reputation: 4942

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'
    );
    

Upvotes: 31

Richie
Richie

Reputation: 9266

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.

Upvotes: 271

Related Questions