Professional Documents
Culture Documents
Synonyms'Synonyms in Oracle
Synonyms'Synonyms in Oracle
Author JP Vijaykumar
Date 08-10-2007
Understanding the characteristics of Oracle’s Synonyms and their object access priorities will help resolve
related issues.
I created a synonym of 31 characters long. Oracle does not impose the length restriction on synonym
names.
Even though, I could create a synonym name with 31 characters, I can not use the lengthy synonym name
to desc or select from the base table.
desc "/852936c6_AAAAAAAAAAAAAAAAAAAA"
Name Null? Type
----------------------------------------- -------- ----------------------------
COL1 NUMBER
COL2 VARCHAR2(20)
select * from "/852936c6_AAAAAAAAAAAAAAAAAAAA";
COL1 COL2
---------- --------------------
1 rama
But I can describe or select from the base table, using the Oracle defined synonym for my 31 character long
synonym_name.
In development, you will be creating private synonyms and public synonyms, left and right. If care is not
taken, you land in trouble with access priorities. Let us explore the access priorities, Oracle chooses, while
processing queries on the base tables via synonyms:
create table temp_jp(col1 number,col2 varchar2(20));
insert into temp_jp values(1,'rama');
create table temp_jp1(col1 number, col2 varchar2(20));
insert into temp_jp1 values(1,'sita');
commit;
create user jp identified by jp account unlock;
grant create session to jp;
grant select on temp_jp to jp;
grant select on temp_jp1 to jp;
grant create or replace synonym to jp;
create or replace synonym jp.temp_jp for temp_jp1;
create or replace public synonym temp_jp for temp_jp;
From the above executed sql statements, it is evident that, user JP can select from SCOTT.TEMP_JP table
via the public synonym TEMP_JP and from SCOTT.TEMP_JP1 table via the private synonym
JP.TEMP_JP in user JP’s schema.
The same is verified by querying the views all_synonyms, all_tab_privs.
Notice, the private synonym in JP’s schema and the public synonym are created with the same
nameTEMP_JP
If user JP executes a query to select from TEMP_JP, how Oracle decides, which base table to access?
Let us see:
connect jp/jp@prod
select * from temp_jp;
COL1 COL2
---------- --------------------
1 sita
The private synonym is taking precedence over the public synonym and user JP will be accessing
SCOTT.TEMP_JP1 table via JP’s private synonym TEMP_JP.
If we drop the private synonym, from JP’s schema, then user JP can access the data from scott.TEMP_JP
table via the public synonym.
In development, tables are created and dropped regularly. If you do not drop synonyms created on these
base tables, the orphaned synonyms will pose a problem down the road.
The synonym remains even after the base table is dropped. If you select from a synonym, on a non existent
table, you get an error. Even you can create a synonym on a non existent table.
create or replace synonym temp_jp for temp_jp;
create or replace synonym temp_jp for temp_jp
*
ERROR at line 1:
ORA-01471: cannot create a synonym with same name as object
I can not create a private synonym on an object with an identical name. But I can create a public synonym
on an object with an identical name.
You can create a public synonym on a non existent table. A synonym exists, even when the base table is
dropped. But when you access the public synonym on the non existent table, you get error.
Observe the different error messages generated by Oracle, when selecting from a synonym on a non-
existent table with an identical name and with a different name.
References:
Metalink Note:392705.1