ORACLE重编译同义词(synonym)遇到ORA-01031总结

咱们在编译INVALID的同义词(synonym)的时候,可能在某些版本会遇到一些特殊权限问题,具体来讲是遇到ORA-01031错误。下面构造这样一个例子:app

 

 

以下所示,scott用户下面建立了一个公共同义词emp(下面测试环境为Oracle 10.2.0.5测试

 

SQL> show user;
USER is "SCOTT"
SQL> create public synonym emp for scott.emp;
 
Synonym created.
 
SQL> 

 

   假设咱们须要从新编译同义词,在system用户下去编译同义词时,就会遇到ORA-01031this

 

SQL> show user;
USER is "SYSTEM"
SQL> alter public synonym emp compile;
alter public synonym emp compile
*
ERROR at line 1:
ORA-01031: insufficient privileges

 

 另一种状况也会遇到这个错误,UserA 去编译UserB下的同义词的话,也会遇到这个错误。按照个人理解,若是用户拥有CREATE ANY SYNONYM和DROP ANY SYNONYM权限的话,应该是能够编译其它SCHEMA下的同义词。并且SYSTEM用户被授予了DBA角色,DBA角色拥有CREATE ANY SYNONYM 或DROP ANY SYNONYM的权限。spa

 

 
SQLSELECT * FROM DBA_SYS_PRIVS WHERE PRIVILEGE IN('CREATE ANY SYNONYM','DROP ANY SYNONYM') AND GRANTEE='DBA';
 
GRANTEE                        PRIVILEGE                                ADM
------------------------------ ---------------------------------------- ---
DBA                            DROP ANY SYNONYM                         YES
DBA                            CREATE ANY SYNONYM                       YES
 
 
SQL> SELECT * FROM DBA_ROLE_PRIVS WHERE GRANTEE='SYSTEM';
 
GRANTEE                        GRANTED_ROLE                   ADM DEF
------------------------------ ------------------------------ --- ---
SYSTEM                         AQ_ADMINISTRATOR_ROLE          YES YES
SYSTEM                         DBA                            YES YES
SYSTEM                         MGMT_USER                      NO  YES

 

可是metalink上的文档Doc ID 1299001.1提示:同义词的编译时,要么是sys用户或者同义词的OWNER才能编译,也就是说,你要编译同义词emp,必须是scott用户或sys用户。3d

 

 

SQL> show user
USER is "SYS"
SQL> alter public synonym emp compile;
 
Synonym altered

 

 

因此在Oracle 10g下要编译其它schema下的同义词,必须是sys用户。Ora-01031 When Compiling A Synonym (Doc ID 1299001.1)的介绍以下:code

 

 

CAUSEorm

This is the expected behaviour. The command "alter synonym <name> compile" is not documented and as such it is not supported. Although it works in some circumstances it is not meant to be used by the users directly.blog

SOLUTIONip

This is the expected behaviour. If all the synonyms have to compiled then run the recompilation script as SYS.ci

 

文档Ora-01031 When Executing Alter Synonym Compile For Other Schema (Doc ID 1435898.1)中也有相关简单阐述

clip_image001

 

  在Oracle 11.2.0.4.0中测试也是如此。可是后续官方文档中将这个现象视为一个Bug,官方文档4189542 - Only SYS can "alter synonym... compile" for another users's synonym (Doc ID 4189542.8)中有简单介绍,而且在后面的一些版本中修复了这个Bug,Doc ID 4189542.8的具体信息以下以下:

 

Only SYS can "alter synonym... compile" for another users's synonym.

Other users get ORA-1031 attempting to compile another users synonym.

With this fix a user with both CREATE ANY SYNONYM and DROP ANY SYNONYM

privilege is allowed to issue ALTER SYNONYM .. COMPILE for another users synonym.

 

clip_image002

 

 

 相同的现象在不一样文档中有不一样阐述,确实有点让人迷惑,若是获取的信息不全或比较片面的话,可能对这个问题和现象有不一样的理解。

 

 

参考资料:

 

Ora-01031 When Compiling A Synonym (Doc ID 1299001.1)

Ora-01031 When Executing Alter Synonym Compile For Other Schema (Doc ID 1435898.1)

Bug 4189542 - Only SYS can "alter synonym... compile" for another users's synonym (Doc ID 4189542.8)

相关文章
相关标签/搜索