如何在oracle 11g中查询主键

时间:2011-03-18 14:38:50

标签: sql oracle11g

有人知道如何在Oracle 11g中查询表的主键吗?我在SQL Server上看到了类似的问题,但是我在尝试该线程的答案时没有运气。

谢谢!

2 个答案:

答案 0 :(得分:10)

作为拥有该表的用户,您可以这样做:

select constraint_name, status, deferrable, deferred, validated, generated 
from user_constraints 
where constraint_type = 'P' and table_name = 'Table Name'

更新:我认为这可以满足您的需求。

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

您可以查看this网站了解详情。

答案 1 :(得分:1)

select cols.table_name ||' - '||cols.column_name primary_key fromall_constraints cons, all_cons_columns cols,
user_tables ut
where  cons.constraint_type = 'P'
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner`enter code here`
and cols.table_name = ut.table_name
order by cols.table_name;