如果我有另一个Schema表的同义词,我可以插入同义词吗? 如果是这样,它还会影响原始表吗?
答案 0 :(得分:2)
同义词不是“真实”对象,而只是指向对象的指针;所以,无论你在同义词上做什么,你都在做推荐的对象。
您可以通过显式编写架构来访问不同架构中的表:
SQL> select count(1) from sys.sys_table;
COUNT(1)
----------
0
SQL> select count(1) from sys_table;
select count(1) from sys_table
*
ERROR at line 1:
ORA-00942: table or view does not exist
创建同义词
SQL> create synonym my_synonym for sys.sys_table;
Synonym created.
然后您可以使用同义词来访问该表:
SQL> select count(1) from my_synonym;
COUNT(1)
----------
0
但你仍然无法直接访问该表:
SQL> select count(1) from sys_table;
select count(1) from sys_table
*
ERROR at line 1:
ORA-00942: table or view does not exist
您可以使用同义词
插入表格SQL> insert into my_synonym values (1);
1 row created.
...或使用schema.table
:
SQL> insert into sys.sys_table values (2);
1 row created.
无论你怎么做,都会插入所有行:
SQL> select count(1) from sys.sys_table;
COUNT(1)
----------
2
SQL> select count(1) from my_synonym;
COUNT(1)
----------
2
如果删除同义词
SQL> drop synonym my_synonym;
Synonym dropped.
您需要使用schema.table,数据和表仍然保留:
SQL> select count(1) from sys.sys_table;
COUNT(1)
----------
2
但是,当然,您不能再使用同义词
SQL> select count(1) from my_synonym;
select count(1) from my_synonym
*
ERROR at line 1:
ORA-00942: table or view does not exist