SQL oracle 11g 如何查询主键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5353522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to query for a primary key in oracle 11g
提问by web_novice118
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.
有人知道如何在 Oracle 11g 中查询表的主键吗?我看到了 SQL Server 的类似问题,但我在该线程上尝试答案时没有运气。
Thanks!
谢谢!
回答by Jacob Schoen
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 thissite for more details.
您可以查看此站点以获取更多详细信息。
回答by Mandeep Singh Gill
SELECT cols.table_name
||' - '
||cols.column_name primary_key
FROM all_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
AND cols.table_name = ut.table_name
ORDER BY cols.table_name;