Professional Documents
Culture Documents
Les 13
Les 13
After
After completing
completing this
this lesson,
lesson, you
you should
should
be
be able
able to
to do
do the
the following:
following:
•• Describe
Describe some
some database
database objects
objects and
and
their
their uses
uses
•• Create,
Create, maintain,
maintain, and
and use
use sequences
sequences
•• Create
Create and
and maintain
maintain indexes
indexes
•• Create
Create private
private and
and public
public synonyms
synonyms
•• Automatically
Automatically generates
generates unique
unique
numbers
numbers
•• Is
Is aa sharable
sharable object
object
•• Is
Is typically
typically used
used to
to create
create aa primary
primary key
key
value
value
•• Replaces
Replaces application
application code
code
•• Speeds
Speeds up
up the
the efficiency
efficiency of
of accessing
accessing
sequence
sequence values
values when
when cached
cached in
in
memory
memory
13-4 Copyright Oracle Corporation, 1998. All rights reserved.
The CREATE SEQUENCE
Statement
Define
Define aa sequence
sequence to
to generate
generate sequential
sequential
numbers
numbers automatically.
automatically.
CREATE
CREATE SEQUENCE
SEQUENCE sequence
sequence
[INCREMENT
[INCREMENT BYBY n]
n]
[START
[START WITH
WITH n]
n]
[{MAXVALUE
[{MAXVALUE nn || NOMAXVALUE}]
NOMAXVALUE}]
[{MINVALUE
[{MINVALUE nn || NOMINVALUE}]
NOMINVALUE}]
[{CYCLE
[{CYCLE || NOCYCLE}]
NOCYCLE}]
[{CACHE
[{CACHE nn || NOCACHE}];
NOCACHE}];
SQL>
SQL> CREATE
CREATE SEQUENCE
SEQUENCE dept_deptno
dept_deptno
22 INCREMENT
INCREMENT BY
BY 11
33 START
START WITH
WITH 91
91
44 MAXVALUE
MAXVALUE 100
100
55 NOCACHE
NOCACHE
66 NOCYCLE;
NOCYCLE;
Sequence
Sequence created.
created.
•• The
The LAST_NUMBER
LAST_NUMBER column
column displays
displays
the
the next
next available
available sequence
sequence number.
number.
•• View
View the
the current
current value
value for
for the
the
DEPT_DEPTNO
DEPT_DEPTNO sequence.
sequence.
SQL>
SQL> SELECT
SELECT dept_deptno.CURRVAL
dept_deptno.CURRVAL
22 FROM
FROM dual;
dual;
Change
Change the
the increment
increment value,
value, maximum
maximum
value,
value, minimum
minimum value,
value, cycle
cycle option,
option, or
or
cache
cache option.
option.
SQL>
SQL> ALTER
ALTER SEQUENCE
SEQUENCE dept_deptno
dept_deptno
22 INCREMENT
INCREMENT BY
BY 11
33 MAXVALUE
MAXVALUE 999999
999999
44 NOCACHE
NOCACHE
55 NOCYCLE;
NOCYCLE;
Sequence
Sequence altered.
altered.
•• Remove
Remove aa sequence
sequence from
from the
the data
data
dictionary
dictionary by
by using
using the
the DROP
DROP
SEQUENCE
SEQUENCE statement.
statement.
•• Once
Once removed,
removed, thethe sequence
sequence can
can no
no
longer
longer be
be referenced.
referenced.
SQL>
SQL> DROP
DROP SEQUENCE
SEQUENCE dept_deptno;
dept_deptno;
Sequence
Sequence dropped.
dropped.
•• Automatically:
Automatically: A A unique
unique index
index is
is created
created
automatically
automatically when
when you
you define
define aa
PRIMARY
PRIMARY KEYKEY oror UNIQUE
UNIQUE constraint
constraint in
in
aa table
table definition.
definition.
•• Manually:
Manually: Users
Users can
can create
create nonunique
nonunique
indexes
indexes on
on columns
columns toto speed
speed up
up access
access
time
time to
to the
the rows.
rows.
•• Improve
Improve the
the speed
speed of
of query
query access
access on
on
the
the ENAME
ENAME column
column in
in the
the EMP
EMP table.
table.
SQL>
SQL> CREATE
CREATE INDEX
INDEX emp_ename_idx
emp_ename_idx
22 ON
ON emp(ename);
emp(ename);
Index
Index created.
created.
•• The
The table
table is
is small.
small.
•• The
The columns
columns areare not
not often
often used
used as
as aa
condition
condition in
in the
the query.
query.
•• Most
Most queries
queries are
are expected
expected toto retrieve
retrieve
more
more than
than 2–4%
2–4% ofof the
the rows.
rows.
•• The
The table
table is
is updated
updated frequently.
frequently.
•• Remove
Remove the
the EMP_ENAME_IDX
EMP_ENAME_IDX index
index from
from
the
the data
data dictionary.
dictionary.
SQL>
SQL> DROP
DROP INDEX
INDEX emp_ename_idx;
emp_ename_idx;
Index
Index dropped.
dropped.
•• To
To drop
drop an
an index,
index, you
you must
must be
be the
the owner
owner
of
of the
the index
index or
or have
have the
the DROP
DROP ANY
ANY INDEX
INDEX
privilege.
privilege.
13-21 Copyright Oracle Corporation, 1998. All rights reserved.
Synonyms
Simplify
Simplify access
access toto objects
objects by
by creating
creating aa
synonym
synonym (another
(another name
name for
for an
an object).
object).
•• Refer
Refer to
to aa table
table owned
owned by
by another
another user.
user.
•• Shorten
Shorten lengthy
lengthy object
object names.
names.
CREATE
CREATE [PUBLIC]
[PUBLIC] SYNONYM
SYNONYM synonym
synonym
FOR
FOR object;
object;
•• Drop
Drop aa synonym.
synonym.
SQL>
SQL> DROP
DROP SYNONYM
SYNONYM d_sum;
d_sum;
Synonym
Synonym dropped.
dropped.