Oracle principle: public synonyms and private synonyms

A synonym is an alias for an existing object, similar to the typedef keyword in C++. Synonyms can simplify SQL statements, hide the name and owner of the object, and can also provide public access to the object. In Oracle, synonyms are divided into public synonyms and private synonyms. Public synonyms can be accessed by all database users. Private synonyms can only be accessed within its mode, and cannot have the same name as the object in the current mode.

CREATE [PUBLIC] SYNONYM [synonym name] FOR [object name];

If you don’t write PUBLIC, the created synonym is a private synonym, and PUBLIC is a public synonym.

Permission required to create synonyms

GRANT CREATE [PUBLIC] SYNONYM TO [user name]

Delete synonyms (deleting synonyms also requires permission)

DROP [PUBLIC] SYNONYM [synonym name];

Replace synonyms (requires permission):

CREATE OR REPLACE SYNONYM [new synonym name] FOR [object name];

Private synonyms, because each mode is private, private synonyms with the same name can be created in different modes without affecting each other;

 

-------创建表结构
create  table salary_tbl(
   employer_nm varchar(20),
   department varchar(20) not null,
   salary number not null,
   leader_nm varchar(20)
);
-----插入测试数据----
truncate table salary_tbl;
begin
 for i in  1..100
     loop
     insert into salary_tbl values('雇佣者'||i,'部门'||Mod(i,6),100*POWER(10000,i*0.01),'雇佣者'||Mod(i,6)); 
   end loop;
end; 
/
commit;
--------创建私有同义词---------
CREATE  SYNONYM  s_tbl FOR SALARY_TBL;
--------用私有同义词来查询-----------
select * from s_tbl s where s.employer_nm ='雇佣者1'; 
--------删除私有同义词----------
DROP   SYNONYM  s_tbl;

We know that the following SQL can be used to look up table information: select * from tab; So here is the table name?

I can view the object table to find object information

select * from all_objects where object_name='TAB';

It can be seen that the TAB object is a view in SYS, but is a synonym in public. The TAB here is not a table;

We can view synonyms information:

select * from all_synonyms  where SYNONYM_NAME='TAB';

We can see that no matter whether OWNER is public or SYSTEM, TAB synonyms correspond to the TAB table under SYS mode (user);

select * from tab; is to search based on synonyms, which is the same as the query table select * from sys.tab;

 

Guess you like

Origin blog.csdn.net/superSmart_Dong/article/details/104547555