How to query for a primary key in oracle 11g

女生的网名这么多〃 提交于 2019-12-22 06:47:06

问题


Does anybody know how to query for a primary key of a table in Oracle 11g? I saw a similar question for SQL Server, but I've had no luck from trying the answers on that thread.

Thanks!


回答1:


As the user that owns the table you can do:

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

Update: I think this gets you what you need.

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

You can check this site for more details.




回答2:


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;


来源:https://stackoverflow.com/questions/5353522/how-to-query-for-a-primary-key-in-oracle-11g

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!