|
|
Managing Oracle
Synonyms
Oracle Tips by Burleson Consulting |
Managing Oracle Synonyms
These are not your daddy?s synonyms, or your SAT?s
synonyms; no, these are Oracle synonyms. An Oracle synonym basically allows you to
create a pointer to an object that exists somewhere else. You need Oracle synonyms
because when you are logged into Oracle, it looks for all objects you are
querying in your schema (account). If they are not there, it will give you an error telling you that they do not exist. For example, assume from the
ROBERT schema that we issue a query like SELECT * FROM emp; and the EMP table is
not there, we get this error:
Notice that we added SCOTT to the beginning of the EMP
table reference. This indicates, of course, that we want to query the EMP table
in the SCOTT schema, and sure enough there is the table. A schema is another
word for a user. While a ?user? is an account you can log into, every user also
has a ?schema,? which is a virtual space for the user to create their own
objects. So what does that make the words ?Users? and ?Schemas?? Synonyms, of
course!
However, it would be a bit of a pain to have to always
prefix all SQL calls to the EMP table with SCOTT, there must be an easier way.
There is, the way is called synonyms. In the following sections we will discuss
the creation and removal of synonyms.
Creating Oracle Synonyms
A synonym is named, and points to a specific object. For
example, in the ROBERT schema we can create a private synonym for SCOTT.EMP
using the create synonym command:
SQL> CREATE SYNONYM emp FOR SCOTT.EMP;
Now, when we issue the query with just the EMP (removing
the SCOTT.) We will see the data from the SCOTT.EMP table because Oracle will
follow the synonym to the correct place as seen here:
Note that we said that this was a private synonym. That
means that only the ROBERT user can use the synonym. We can also create public
synonyms using the create public synonym command as seen here:
SQL> CREATE PUBLIC SYNONYM emp FOR SCOTT.EMP;
Generally good DBA?s try to avoid public synonyms. They
do make management of the database a bit easier, but they also have security and
performance issues associated with them. Hence, try not to use public synonyms
unless you have to.
You can have a public and private synonym of the same
name. In fact, you can have a public and private synonym called EMP in the SCOTT
schema and have a table called EMP in the same schema. In cases where you have
multiple synonyms and/or a table present, it can get confusing which object you
are using (this is another reason we hate public synonyms). There is an order of
precedence with regards to the use of synonyms and local objects. This is:
1. Local objects will always be accessed first.
2. If a local object does not exist, the object with a
private synonym will be accessed.
3. If a private synonym does not exist or the object
does not exist, then the public synonym will be used.
Note that a synonym can be created for an object that
does not exist, and an object with an associated synonym can be dropped without
removing the synonym. This can cause all sorts of interesting problems for
DBA?s, so be careful.
Removing Synonyms
The drop synonym command is used to drop public and
private synonyms. Here is an example of dropping a private synonym and a public
synonym with the drop synonym command:
SQL> --
Drop public synonym
SQL> DROP
PUBLIC SYNONYM emp;
SQL> --
Drop private synonym
SQL> DROP
SYNONYM emp;
Tip: You can use this command to avoid the
use of public synonyms. By setting the current_schema attribute to the
schema owner name it is not necessary to create public synonyms for production
table names.:
alter session set current_schema='MYSCHEMA';
Synonyms and Views
Changes to synonyms often result in invalidation of PL/SQL
program units, which causes time-consuming re-compilation of the program units.
Prior to Oracle Database 10g, when a synonym was
re-created, the status of any dependent
PL/SQL program unit was set to
INVALID. If you
did not recompile the
PL/SQL program units manually, they would recompile automatically the
next time they were invoked, causing runtime performance overhead.
Now, Oracle Database 10g introduces automatic
validation of PL/SQL program units, which determines whether program units will
have to be invalidated. This is only the case in the following circumstances.
?
The column order, column names, and column data
types of the tables need to be identical.
?
The privileges on the newly referenced table and
its columns are a superset of the set of privileges on the original table. These
privileges must not be derived through roles alone.
?
The names and types of partitions and
sub-partitions need to be identical.
?
The tables are of the same organization type.
?
Object type columns are of the exact same type.
In the case of views, just like PL/SQL program units, even
when the synonym definition is changed, the dependent views will not be
validated. However, this only happens when certain conditions are met. All the
conditions specified for PL/SQL units are relevant here also.
In addition, the following conditions must be met to keep the
VALID status of
the dependent views when you redefine a synonym:
?
Columns and order of columns defined for primary
key and unique indexes,
NOT NULL constraints, and primary key and unique constraints must be
identical.
?
The dependent view cannot have any referential
constraints.
Changes to synonyms often
result in invalidation of PL/SQL program units, which causes time-consuming
re-compilation of the program units.
Prior to Oracle Database
10g, when a synonym was re-created, the status of any dependent PL/SQL program
unit was set to INVALID. If you did not recompile the PL/SQL program units
manually, they would recompile automatically the next time they were invoked,
causing runtime performance overhead.
Now, Oracle Database 10g
introduces automatic validation of PL/SQL program units, which determines
whether program units will have to be invalidated. This is only the case in the
following circumstances.
-
The column order, column names, and column
data types of the tables need to be identical.
-
The privileges on the newly referenced table
and its columns are a superset of the set of privileges on the original
table. These privileges must not be derived through roles alone.
-
The names and types of partitions and
sub-partitions need to be identical.
-
The tables are of the same organization type.
-
Object type columns are of the exact same
type.
In the case of views, just
like PL/SQL program units, even when the synonym definition is changed, the
dependent views will not be validated. However, this only happens when certain
conditions are met. All the conditions specified for PL/SQL units are relevant
here also.
In addition, the following
conditions must be met to keep the VALID status of the dependent views when you
redefine a synonym:
-
Columns and order of columns defined for
primary key and unique indexes, NOT NULL constraints, and primary key and
unique constraints must be identical.
-
The dependent view cannot have any referential
constraints.
This is an excerpt from the bestselling "Easy
Oracle Jumpstart"
|