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)

相關文章
相關標籤/搜索