我在此通过一个具体的来给大家展示一种常见的导致“ORA-00980”错误发生的情况,然后给出一些其他可能导致这个错误发生的原因,最后给出一个可以自动化获得删除所有无效同名的。
一、先来模拟一下这个“ORA-00980: translation is no longer valid”错误。
1.在用户中创建一张表t
secooler@ora10g> create table t (x number);
Table created.
secooler@ora10g> insert into t values (1);
1 row created.
secooler@ora10g> commit;
Commit complete.
2.切换到sec用户,创建同名syn_t引用secooler用户下的t表
secooler@ora10g> conn sec/sec
Connected.
sec@ora10g> create synonym syn_t for secooler.t;
Synonym created.
sec@ora10g> select * from syn_t;
X
----------
1
3.回到secooler用户中将表t删除掉
sec@ora10g> conn secooler/secooler
Connected.
secooler@ora10g> drop table t purge;
Table dropped.
4.此时,我们再次使用同名syn_t查看表t时就会见到这个“ORA-00980”错误了
secooler@ora10g> conn sec/sec
Connected.
sec@ora10g> select * From syn_t;
select * From syn_t
*
ERROR at line 1:
ORA-00980: synonym translation is no longer valid
二、导致“ORA-00980”错误的原因小结
1.删除了对象,但是忘记删除同名;
2.删除一个用户,但是忘记删除与此用户中相关的同名;
3.创建同名时,同名对应的数据库对象已经被删除掉了(这种情况很有意思)。
针对第三点,进一步演示一下,前面我们已经删除了secooler用户下t表的情况。此时我们尝试使用secooler用户下被删除的t表再创建两个同名,有意思的是虽然表已经不存在了,但同名还是可以创建成功的。需要清醒的认识到此时被创建出来的同名是无效的,从后面的脚本执行结果中可以更清晰的得出这个结论。
sec@ora10g> create synonym syn_public_t for secooler.t;
Synonym created.
sec@ora10g> create synonym syn_tt for secooler.t;
Synonym created.
三、隆重推出自动化生成删除无效同名的脚本
1.脚本如下
select 'drop '
|| decode (s.owner,
'PUBLIC', 'public synonym ',
'synonym ' || s.owner || '.')
|| s.synonym_name
|| ';' as "Dropping invalid synonyms:"
from dba_synonyms s
where table_owner not in ('SYSTEM', 'SYS') and db_link is null
and not exists
(select null
from dba_objects o
where s.table_owner = o.owner
and s.table_name = o.object_name)
/
2.针对上面的场景,真实演示一下
sys@ora10g> select 'drop '
2 || decode (s.owner,
3 'PUBLIC', 'public synonym ',
4 'synonym ' || s.owner || '.')
5 || s.synonym_name
6 || ';' as "Dropping invalid synonyms:"
7 from dba_synonyms s
8 where table_owner not in ('SYSTEM', 'SYS') and db_link is null
9 and not exists
10 (select null
11 from dba_objects o
12 where s.table_owner = o.owner
13 and s.table_name = o.object_name)
14 /
Dropping invalid synonyms:
------------------------------------------------------------------------
drop synonym SEC.SYN_TT;
drop synonym SEC.SYN_PUBLIC_T;
drop synonym SEC.SYN_T;
3.使用脚本生成的SQL脚本即可轻松完成无效同名的清理任务。
sys@ora10g> drop synonym SEC.SYN_TT;
Synonym dropped.
sys@ora10g> drop synonym SEC.SYN_PUBLIC_T;
Synonym dropped.
sys@ora10g> drop synonym SEC.SYN_T;
Synonym dropped.
四、小结
在DBA的日常维护中需要不断的思考一个问题:如何才能更加高效的应对故障的发生?
带着这个问题,它就会不断的指引我们向自动化的道路迈进。
编写一些顺手易用的脚本是一名优秀DBA必须具有的基本技能之一。
希望这个小文儿能给大家带来一点点思考。
Good luck.
-- The End --
如有侵权,请告知我,马上删除。
阅读(3046) | 评论(2) | 转发(0) |