How to correctly make a public synonym

2020-07-06 04:07发布

问题:

This is a pretty silly one, but I need help.

I have a table owned by mydbowner. It is named mydbowner.mytable. I tried to make a public synonym by issuing the command:

CREATE OR REPLACE PUBLIC SYNONYM mytable FOR mydbowner.mytable;

When I do this, and I query the table I get:

ORA-01775: looping chain of synonyms

How do I make this synonym without having the problem.

回答1:

I think Justin is on the right track. What I think it actually means is that mydbowner.mytable doesn't exist.

Here's an example:

SQL> conn mbobak
Enter password: 
Connected.
SQL> drop table mytable;
drop table mytable
           *
ERROR at line 1:
ORA-00942: table or view does not exist


SQL> create public synonym mytable for mbobak.mytable;

Synonym created.

SQL> select * from mytable;
select * from mytable
              *
ERROR at line 1:
ORA-01775: looping chain of synonyms

I think what's happening is that Oracle tries to resolve mytable, there is no mytable in mbobak schema, so it looks for it in PUBLIC, it finds it, and sees that it points to mbobak.mytable. But, mbobak.mytable doesn't exist, so, it looks for mytable in PUBLIC, and there's the loop.

And in fact, if you create mytable, the error goes away:

SQL> create table mytable as select * from dual;

Table created.

SQL> select * from mytable;

D
-
X

1 row selected.

SQL> drop table mytable;

Table dropped.

SQL> select * from mytable;
select * from mytable
              *
ERROR at line 1:
ORA-01775: looping chain of synonyms

Yes, I realize that doesn't really entirely make sense, as, once the public synonym resolved to mbobak.mytable, and that's not found, it seems to me, it should return an error ORA-942 "table or view does not exist", which makes far more sense to me.

But, this does seem to be how it works.

QED

Hope that helps.



回答2:

The error you're getting implies that mydbowner.mytable is not, in fact a table. What does

SELECT object_type
  FROM all_objects
 WHERE owner = 'MYDBOWNER'
   AND object_name = 'MYTABLE'

return?