How can I find the OWNER of an object in Oracle?

后端 未结 5 1075
攒了一身酷
攒了一身酷 2021-01-31 19:59

I want to find the foreign keys of a table but there may be more than one user / schema with a table with the same name. How can I find the one that the currently logged user is

相关标签:
5条回答
  • 2021-01-31 20:08

    To find the name of the current user within an Oracle session, use the USER function.

    Note that the owner of the constraint, the owner of the table containing the foreign key, and the owner of the referenced table may all be different. It sounds like it’s the table owner you’re interested in, in which case this should be close to what you want:

    select Constraint_Name
    from All_Constraints
    where Table_Name = 'WHICHEVER_TABLE'
      and Constraint_Type = 'R' and Owner = User;
    
    0 讨论(0)
  • 2021-01-31 20:10

    I found this question as the top result while Googling how to find the owner of a table in Oracle, so I thought that I would contribute a table specific answer for others' convenience.

    To find the owner of a specific table in an Oracle DB, use the following query:

    select owner from ALL_TABLES where TABLE_NAME ='<MY-TABLE-NAME>';
    
    0 讨论(0)
  • 2021-01-31 20:19

    Interesting question - I don't think there's any Oracle function that does this (almost like a "which" command in Unix), but you can get the resolution order for the name by:

    select * from 
    (
     select  object_name objname, object_type, 'my object' details, 1 resolveOrder 
      from user_objects
      where object_type not like 'SYNONYM'
     union all
     select synonym_name obj , 'my synonym', table_owner||'.'||table_name, 2 resolveOrder
      from user_synonyms
     union all
     select  synonym_name obj , 'public synonym', table_owner||'.'||table_name, 3 resolveOrder
      from all_synonyms where owner = 'PUBLIC'
    )
    where objname like upper('&objOfInterest')
    
    0 讨论(0)
  • 2021-01-31 20:26

    You can query the ALL_OBJECTS view:

    select owner
         , object_name
         , object_type
      from ALL_OBJECTS
     where object_name = 'FOO'
    

    To find synonyms:

    select *
      from ALL_SYNONYMS
     where synonym_name = 'FOO'
    

    Just to clarify, if a user user's SQL statement references an object name with no schema qualification (e.g. 'FOO'), Oracle FIRST checks the user's schema for an object of that name (including synonyms in that user's schema). If Oracle can't resolve the reference from the user's schema, Oracle then checks for a public synonym.

    If you are looking specifically for constraints on a particular table_name:

    select c.*
      from all_constraints c 
     where c.table_name = 'FOO'
     union all
    select cs.*
      from all_constraints cs
      join all_synonyms s 
        on (s.table_name = cs.table_name
         and s.table_owner = cs.owner 
         and s.synonym_name = 'FOO'
           )
    

    HTH

    -- addendum:

    If your user is granted access to the DBA_ views (e.g. if your user has been granted SELECT_CATALOG_ROLE), you can substitute 'DBA_' in place of 'ALL_' in the preceding SQL examples. The ALL_x views only show objects which you have been granted privileges. The DBA_x views will show all database objects, whether you have privileges on them or not.

    0 讨论(0)
  • 2021-01-31 20:26

    Oracle views like ALL_TABLES and ALL_CONSTRAINTS have an owner column, which you can use to restrict your query. There are also variants of these tables beginning with USER instead of ALL, which only list objects which can be accessed by the current user.

    One of these views should help to solve your problem. They always worked fine for me for similar problems.

    0 讨论(0)
提交回复
热议问题