简体   繁体   English

Oracle表的主键

[英]Primary keys for an Oracle table

I am trying to retrieve the primary keys for a table name EMPLOYEE as follows 我正在尝试检索表名称EMPLOYEE的主键,如下所示

    SELECT  cols.column_name
    FROM all_constraints cons, all_cons_columns cols
    WHERE cols.table_name ='EMPLOYEE'
    AND cons.constraint_type = 'P'
    AND cons.constraint_name = cols.constraint_name
    AND cons.owner = cols.owner
    ORDER BY cols.table_name, cols.position

The table EMPLOYEE exists for another user which also has a primary key called ID 表EMPLOYEE为另一个用户存在,该表也有一个称为ID的主键

So the result of the above query gives (ID, ID) instead of (ID) from the current user. 因此,上述查询的结果将提供(ID,ID)而不是当前用户的(ID)。 How do I get only the primary keys of the tables belonging to the user's schema 如何仅获取属于用户架构的表的主键

  1. Switch to USER_* views instead of ALL_* . 切换到USER_*视图而不是ALL_*
  2. Eliminate conditions on the OWNER columns in your query. 消除查询中“ OWNER列上的条件。

  • ALL_* views return everything the querying schema has access to including the objects the schema owns, in contrast to ALL_*视图相比, ALL_*视图返回查询模式有权访问的所有内容,包括该模式拥有的对象
  • USER_* views returning only the objects the schema is owner of USER_*视图仅返回架构所拥有的对象
  • DBA_* views return all objects in database DBA_*视图返回数据库中的所有对象

Therefore, you can query USER_* views or you can query DBA_* views specifying the condition on the OWNER column. 因此,您可以查询USER_*视图,也可以查询在OWNER列上指定条件的DBA_*视图。

In your case your query could look like this. 在您的情况下,您的查询可能如下所示。

select
  cons.table_name
, cons.constraint_name
, cols.column_name
, cols.position
from user_constraints cons, user_cons_columns cols
where cons.constraint_name = cols.constraint_name
and constraint_type = 'P'
order by cons.table_name;

For more details about data dictionary and views' and their scope, consult the Data Dictionary and Dynamic Performance Views section in Concepts. 有关数据字典和视图及其范围的更多详细信息,请参阅概念中的“ 数据字典和动态性能视图”部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM