Get primary key column of a table in oracle

12,017
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;

NOTE replace TABLE_NAME in upper case your table

Share:
12,017

Related videos on Youtube

Adarsh Ravi
Author by

Adarsh Ravi

Senior Software Developer & Product Specialist with great expertise in C# Dot NET Development and RFID Technology.

Updated on June 04, 2022

Comments

  • Adarsh Ravi
    Adarsh Ravi almost 2 years

    I am totally new to using Oracle so I might not be using the right terminologies. Please excuse me for that.

    I have the following query which helps me get the Primary key of a table in SQL Server

    SELECT Col.Column_Name, Tab.Table_Name from 
    INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab, 
    INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col 
    WHERE Col.Constraint_Name = Tab.Constraint_Name 
    AND Col.Table_Name = Tab.Table_Name 
    AND Constraint_Type = 'PRIMARY KEY'
    AND Col.Table_Name = 'EmployeeMaster'
    

    So I want something similar which can help me get the column name of the table which has the primary key. Thanks in advance

  • Adarsh Ravi
    Adarsh Ravi about 7 years
    Can you also help me with identity columns. How can I get the Identity columns of a table
  • sandeep rawat
    sandeep rawat about 7 years
    I think There is no such thing as "auto_increment" or "identity" columns in Oracle. However, you can model it easily with a sequence and a trigger:
  • Adarsh Ravi
    Adarsh Ravi about 7 years
    Ok. Thanks a lot
  • APC
    APC about 7 years
    Oracle introduced identity columns in 12c. Although as the question is tagged [oracle11g] this is not helpful here.