Administration of Other Database Objects
Oracle Tips by Burleson
Chapters 4, 5, and 6 covered the
administration of database tables and indexes. In this chapter, we
will discuss the administration of other database objects, which
include synonyms, sequences, views, and so on. More specifically, for
Oracle8 and Oracle8i, we add Java, DIRECTORY, DIMENSION, OUTLINE,
CONTEXT, OPERATOR, MATERIALIZED VIEWS, and LIBRARY objects; for
Oracle, we add the ability to create parameter (initialization) and
server parameter (dynamically reconfigurable initialization) files.
Administration of Synonyms
Synonyms are a very powerful feature of Oracle
and other SQL-compliant relational database systems. They are used as
a database shorthand. They make it possible to shorten the
specification of long or complex object names. This is especially
useful for shared tables or views. In addition, the use of DATABASE
LINKS in synonyms allows transparent access to other databases on
other nodes or even other entire systems halfway around the globe.
Creation of Synonyms
Synonyms are created using the CREATE SYNONYM
command. Synonyms can be either PRIVATE (the default) or PUBLIC.
Private synonyms can only be used by the user creating them. You must
have a specific grant or one of the special ROLES assigned to you
before you can create synonyms. Only users with appropriate privileges
(usually DBAs) can create PUBLIC synonyms. Since only the owner can
use them, PRIVATE synonyms are more secure.
The simplified format of the CREATE SYNONYM
[PUBLIC] SYNONYM [schema.]synonym_name
Synonym. An allowed name. (It cannot be
the name of an existing object for this user. For the purposes of
uniqueness, the schema name is considered to be a part of the name for
Schema.object. An existing table, view,
package, procedure, function, Java object, materialized view, other
synonym, or sequence name. The schema object cannot be contained in a
package. A synonym cannot be created for an object type.
Database link. An existing database
link (covered in section 7.3).
Synonyms provide both data independence and
location transparency. With proper use and assignment, they allow an
application to function regardless of table ownership, location, or
Synonyms can be used in SELECT, INSERT,
UPDATE, DELETE, EXPLAIN PLAN, LOCK TABLE, AUDIT, NOAUDIT, GRANT,
REVOKE, and COMMENT commands.
Alteration of Synonyms
Synonyms cannot be altered. To change a
synonym’s definition, it must be dropped and re-created.
Synonyms are dropped via the DROP command:
[PUBLIC] SYNONYM [schema.]synonym_name;
It is sometimes handy to have a script lying
around to rebuild your existing synonyms. The script in Source 7.1
will create a synonym rebuild script for you from your data
SOURCE 7.1 Script to generate synonym
FUNCTION: SCRIPT FOR CREATING SYNONYMS
This script must be run by a user with the DBA role.
REM This script is intended to run with Oracle7 or
REM Running this script will in turn create a script
REM all the synonyms in the database. The created
REM create_synonyms.sql, can be run by any user with
REM role or with the 'CREATE ANY SYNONYM' and
REM SYNONYM' system privileges.
REM NOTE: This script does not capture synonyms for tables owned
REM by the 'SYS' user.
REM Only preliminary testing of this script was
REM sure to test it completely before relying on it.
SET VERIFY OFF FEEDBACK OFF TERMOUT OFF ECHO OFF PAGESIZE 0
SET TERMOUT ON
SELECT ''Creating synonym build script...'' FROM dual;
SET TERMOUT OFF
COLUMN dbname NEW_VALUE db NOPRINT
SELECT name dbname FROM v$database;
SELECT ''CREATE ''|| DECODE(owner,''PUBLIC'',''PUBLIC '',NULL) ||
'SYNONYM ''|| DECODE(owner,''PUBLIC'',NULL, owner || ''.'') ||
LOWER(synonym_name) || '' FOR '' || LOWER(table_owner) ||
''.'' || LOWER(table_name) ||
DECODE(db_link,NULL,NULL,''@''||db_link) || '';''
WHERE table_owner != ''SYS''
ORDER BY owner
SET VERIFY ON FEEDBACK ON TERMOUT ON PAGESIZE 22
Administration of Sequences
Sequences are special database objects that
provide numbers in sequence for input to a table. They are useful for
providing generated primary key values and for input of number type
columns such as purchase order, employee number, sample number, and
sales order number, where the input must be unique and in some form of
Creation of Sequences
Sequences are created by use of the CREATE
SEQUENCE command. The command’s format follows:
[START WITH n]
[INCREMENT BY n]
sequence_name. The name you want the
sequence to have. This may include the user name if created from an
account with DBA privilege.
n. An integer, positive or negative.
INCREMENT BY. Tells the system how to
increment the sequence. If it is positive, the values are ascending;
if it is negative, the values are descending.
START WITH. Tells the system which
integer to start with.
MINVALUE. Tells the system how low the
sequence can go. For ascending sequences, it defaults to 1; for
descending sequences, the default value is 10e27-1.
MAXVALUE. Tells the system the highest
value that will be allowed. For descending sequences, the default is
1; for ascending sequences, the default is 10e27-1.
CYCLE. Causes the sequence to
automatically recycle to minvalue when maxvalue is reached for
ascending sequences; for descending sequences, it causes a recycle
from minvalue back to maxvalue.
CACHE. Caches the specified number of
sequence values into the buffers in the SGA. This speeds access, but
all cached numbers are lost when the database is shut down. The
default value is 20; maximum value is maxvalue-minvalue.
ORDER. Forces sequence numbers to be
output in order of request. In cases where they are used for
timestamping, this may be required. In most cases, the sequence
numbers will be in order anyway, so ORDER will not be required. ORDER
is necessary only to guarantee ordered generation if you are using
Oracle with the Oracle Real Application Clusters option in parallel
mode. If you are using exclusive mode, sequence numbers are always
generated in order.
Sequences prevent the performance problems
associated with sequencing numbers generated by application triggers
of the form:
LOCK TABLE PO_NUM IN EXCLUSIVE MODE NOWAIT;
SELECT MAX(PO_NUM)+1 INTO TEMP_NO FROM SALES ;
If the application requires numbers that are
exactly in sequence (e.g., 1, 2, 3 . . .) then the trigger shown above
may be your only recourse, since if a statement that references a
sequence is rolled back (canceled), that sequence number is lost.
Likewise, any cached sequence numbers are lost each time a database is
shut down. The closest a sequence can get to being fully sequential
START WITH 1
INCREMENT BY 1
If a sequence already exists in a test or
development instance, the script SEQ_RCT.SQL, available from the Wiley
Web site, can be used to build a script to transfer or document the
structure of the sequence.
Alteration of Sequences
There may be times when a sequence must be
altered, such as when a maximum or minimum value is reached. The ALTER
SEQUENCE command is used to accomplish this. The format of the command
[INCREMENT BY n]
future sequence numbers are affected by this statement. To alter the
START WITH clause, the sequence must be dropped and re-created. For
ascending sequences, the MAXVALUE cannot be less than the current
sequence value. For descending sequences, the MINVALUE cannot be
greater than the current sequence value.
Code Depot for Full Scripts
||This is an excerpt
from Mike Ault, bestselling author of "Oracle
10g Grid and Real Application Clusters".
You can buy it direct from the publisher for 30%-off and get
instant access to the code depot of Oracle tuning scripts.