How to get primary key column in Oracle?

后端 未结 5 1760
悲哀的现实
悲哀的现实 2020-11-29 17:12

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

In the input, I only have the table name.

5条回答
  •  星月不相逢
    2020-11-29 17:31

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

提交回复
热议问题