SQL query to find Primary Key of a table?

前端 未结 8 688
耶瑟儿~
耶瑟儿~ 2021-02-04 01:30

How can I find which column is the primary key of a table by using a query?

8条回答
  •  轮回少年
    2021-02-04 01:53

    For Oracle, you can look it up in the ALL_CONSTRAINTS table:

    SELECT a.COLUMN_NAME
    FROM all_cons_columns a INNER JOIN all_constraints c 
         ON a.constraint_name = c.constraint_name 
    WHERE c.table_name = 'TBL'
      AND c.constraint_type = 'P';
    

    DEMO.

    For SQL Server, it was already answered here, and for MySQL check @ajon's answer.

提交回复
热议问题