如何在 Oracle 中检查所有无效的同义词

aal*_*vic 2 oracle synonym

我有一个表 user_synonyms,我可以在其中看到名称、表和表所有者。有没有办法查看这个同义词是否仍然有效,即 如果不手动尝试引用表仍然存在?

Ben*_*Ben 5

您可以通过加入 ALL_TABLES 来检查表是否存在(同义词可能不在同一架构中的表上)。

select *
  from all_synonyms s
  left outer join all_tables t
    on s.table_owner = t.owner
   and s.table_name = t.table_name
 where s.owner = user
Run Code Online (Sandbox Code Playgroud)

and t.table_name is null如果您想要那些表不存在的同义词,请添加条件。

如果要检查同义词是否为 VALID 查询ALL_OBJECTS

select *
  from all_synonyms s
  join all_objects o
    on s.owner = o.owner
   and s.synonym_name = o.object_name
 where o.object_type = 'SYNONYM'
   and s.owner = user
   and o.status <> 'VALID'
Run Code Online (Sandbox Code Playgroud)

正如 a_horse_with_no_name 在评论中指出的那样,表上不需要同义词,视图、序列甚至包都是有效的。

因此,您可能希望更改第一个查询以查找这些:

select *
  from all_synonyms s
  join all_objects o
    on s.table_owner = o.owner
   and s.table_name = o.object_name
 where s.owner = user
Run Code Online (Sandbox Code Playgroud)