You are on page 1of 26

13

Other Database Objects

Copyright Oracle Corporation, 1998. All rights reserved.

Objectives
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

13-2

Copyright Oracle Corporation, 1998. All rights reserved.

Database Objects
Object

Description

Table

Basic unit of storage; composed of rows


and columns

View

Logically represents subsets of data from


one or more tables

Sequence

Generates primary key values

Index

Improves the performance of some queries

Synonym

Alternative name for an object

13-3

Copyright Oracle Corporation, 1998. All rights reserved.

What Is a Sequence?
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 BY
BY 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}];

13-5

Copyright Oracle Corporation, 1998. All rights reserved.

Creating a Sequence
Create
Create aa sequence
sequence named
named DEPT_DEPTNO
DEPT_DEPTNO
to
to be
be used
used for
for the
the primary
primary key
key of
of the
the
DEPT
DEPT table.
table.
Do
Do not
not use
use the
the CYCLE
CYCLE option.
option.
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.

13-6

Copyright Oracle Corporation, 1998. All rights reserved.

Confirming Sequences
Verify
Verify your
your sequence
sequence values
values in
in the
the
USER_SEQUENCES
USER_SEQUENCES data
data dictionary
dictionary
table.
table.
SQL>
SQL>
22
33

SELECT
SELECT
FROM
FROM

sequence_name,
sequence_name, min_value,
min_value, max_value,
max_value,
increment_by,
increment_by, last_number
last_number
user_sequences;
user_sequences;

The
The LAST_NUMBER
LAST_NUMBER column
column displays
displays
the
the next
next available
available sequence
sequence number.
number.

13-7

Copyright Oracle Corporation, 1998. All rights reserved.

NEXTVAL and CURRVAL


Pseudocolumns
NEXTVAL
NEXTVAL returns
returns the
the next
next available
available
sequence
sequence value.
value.
It
It returns
returns aa unique
unique value
value every
every time
time itit is
is
referenced,
referenced, even
even for
for different
different users.
users.
CURRVAL
CURRVAL obtains
obtains the
the current
current sequence
sequence
value.
value.
NEXTVAL
NEXTVAL must
must be
be issued
issued for
for that
that
sequence
sequence before
before CURRVAL
CURRVAL contains
contains aa
value.
value.
13-8

Copyright Oracle Corporation, 1998. All rights reserved.

NEXTVAL and CURRVAL


Pseudocolumns
NEXTVAL
NEXTVAL returns
returns the
the next
next available
available
sequence
sequence value.
value.
It
It returns
returns aa unique
unique value
value every
every time
time itit is
is
referenced,
referenced, even
even for
for different
different users.
users.
CURRVAL
CURRVAL obtains
obtains the
the current
current sequence
sequence
value.
value.
NEXTVAL
NEXTVAL must
must be
be issued
issued for
for that
that
sequence
sequence before
before CURRVAL
CURRVAL contains
contains aa
value.
value.
13-9

Copyright Oracle Corporation, 1998. All rights reserved.

Using a Sequence
Insert
Insert aa new
new department
department named
named
MARKETING
MARKETING in
in San
San Diego.
Diego.
SQL>
SQL> INSERT
INSERT INTO
INTO
22 VALUES
VALUES
33
11 row
row created.
created.

dept(deptno,
dept(deptno, dname,
dname, loc)
loc)
(dept_deptno.NEXTVAL,
(dept_deptno.NEXTVAL,
'MARKETING',
'MARKETING', 'SAN
'SAN DIEGO');
DIEGO');

View
View the
the current
current value
value for
for the
the
DEPT_DEPTNO
DEPT_DEPTNO sequence.
sequence.
SQL>
SQL>
22

13-10

SELECT
SELECT
FROM
FROM

dept_deptno.CURRVAL
dept_deptno.CURRVAL
dual;
dual;

Copyright Oracle Corporation, 1998. All rights reserved.

Using a Sequence
Caching
Caching sequence
sequence values
values in
in memory
memory
allows
allows faster
faster access
access to
to those
those values.
values.
Gaps
Gaps in
in sequence
sequence values
values can
can occur
occur when:
when:
A
A rollback
rollback occurs
occurs
The
The system
system crashes
crashes
A
A sequence
sequence is
is used
used in
in another
another table
table

View
View the
the next
next available
available sequence,
sequence, if
if it
it was
was
created
created with
with NOCACHE,
NOCACHE, by
by querying
querying the
the
USER_SEQUENCES
USER_SEQUENCES table.
table.
13-11

Copyright Oracle Corporation, 1998. All rights reserved.

Modifying a Sequence
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.

13-12

Copyright Oracle Corporation, 1998. All rights reserved.

Guidelines for Modifying


a Sequence
You
You must
must be
be the
the owner
owner or
or have
have the
the
ALTER
ALTER privilege
privilege for
for the
the sequence.
sequence.
Only
Only future
future sequence
sequence numbers
numbers are
are
affected.
affected.
The
The sequence
sequence must
must be
be dropped
dropped and
and
re-created
re-created to
to restart
restart the
the sequence
sequence at
at aa
different
different number.
number.
Some
Some validation
validation is
is performed.
performed.
13-13

Copyright Oracle Corporation, 1998. All rights reserved.

Removing a Sequence
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, the
the 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.

13-14

Copyright Oracle Corporation, 1998. All rights reserved.

What Is an Index?
Is
Is aa schema
schema object
object
Is
Is used
used by
by the
the Oracle
Oracle Server
Server to
to speed
speed
up
up the
the retrieval
retrieval of
of rows
rows by
by using
using aa
pointer
pointer
Can
Can reduce
reduce disk
disk I/O
I/O by
by using
using rapid
rapid path
path
access
access method
method to
to locate
locate the
the data
data
quickly
quickly
Is
Is independent
independent of
of the
the table
table it
it indexes
indexes
Is
Is used
used and
and maintained
maintained automatically
automatically by
by
the
the Oracle
Oracle Server
Server
13-15

Copyright Oracle Corporation, 1998. All rights reserved.

How Are Indexes Created?


Automatically:
Automatically: A
A unique
unique index
index is
is
created
created automatically
automatically when
when you
you define
define aa
PRIMARY
PRIMARY KEY
KEY or
or 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 to
to speed
speed up
up access
access
time
time to
to the
the rows.
rows.

13-16

Copyright Oracle Corporation, 1998. All rights reserved.

Creating an Index
Create
Create an
an index
index on
on one
one or
or more
more columns.
columns.
CREATE
CREATE INDEX
INDEX index
index
ON
ON table
table (column[,
(column[, column]...);
column]...);

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
22 ON
ON
Index
Index created.
created.

13-17

emp_ename_idx
emp_ename_idx
emp(ename);
emp(ename);

Copyright Oracle Corporation, 1998. All rights reserved.

When to Create an Index


The
The column
column is
is used
used frequently
frequently in
in the
the WHERE
WHERE
clause
clause or
or in
in aa join
join condition.
condition.
The
The column
column contains
contains aa wide
wide range
range of
of values.
values.
The
The column
column contains
contains aa large
large number
number of
of null
null
values.
values.
Two
Two or
or more
more columns
columns are
are frequently
frequently used
used
together
together in
in aa WHERE
WHERE clause
clause or
or aa join
join
condition.
condition.
The
The table
table is
is large
large and
and most
most queries
queries are
are
expected
expected to
to retrieve
retrieve less
less than
than 24%
24% of
of the
the
rows.
rows.
13-18

Copyright Oracle Corporation, 1998. All rights reserved.

When Not to Create an Index


The
The table
table is
is small.
small.
The
The columns
columns are
are 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 to
to retrieve
retrieve
more
more than
than 24%
24% of
of the
the rows.
rows.
The
The table
table is
is updated
updated frequently.
frequently.

13-19

Copyright Oracle Corporation, 1998. All rights reserved.

Confirming Indexes
The
The USER_INDEXES
USER_INDEXES data
data dictionary
dictionary view
view
contains
contains the
the name
name of
of the
the index
index and
and its
its
uniqueness.
uniqueness.
The
The USER_IND_COLUMNS
USER_IND_COLUMNS view
view contains
contains
the
the index
index name,
name, the
the table
table name,
name, and
and the
the
column
column name.
name.
SQL>
2
3
4
5

13-20

SELECT
FROM
WHERE
AND

ic.index_name, ic.column_name,
ic.column_position col_pos,ix.uniqueness
user_indexes ix, user_ind_columns ic
ic.index_name = ix.index_name
ic.table_name = 'EMP';

Copyright Oracle Corporation, 1998. All rights reserved.

Removing an Index
Remove
Remove an
an index
index from
from the
the data
data dictionary.
dictionary.
SQL>
SQL> DROP
DROP INDEX
INDEX index;
index;

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 to
to 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
FOR
FOR

13-22

[PUBLIC]
[PUBLIC] SYNONYM
SYNONYM synonym
synonym
object;
object;

Copyright Oracle Corporation, 1998. All rights reserved.

Creating and Removing Synonyms


Create
Create aa shortened
shortened name
name for
for the
the
DEPT_SUM_VU
DEPT_SUM_VU view.
view.
SQL>
SQL> CREATE
CREATE SYNONYM
SYNONYM
22 FOR
FOR
Synonym
Synonym Created.
Created.

d_sum
d_sum
dept_sum_vu;
dept_sum_vu;

Drop
Drop aa synonym.
synonym.
SQL>
SQL> DROP
DROP SYNONYM
SYNONYM d_sum;
d_sum;
Synonym
Synonym dropped.
dropped.

13-23

Copyright Oracle Corporation, 1998. All rights reserved.

Summary
Automatically
Automatically generate
generate sequence
sequence
numbers
numbers by
by using
using aa sequence
sequence generator.
generator.
View
View sequence
sequence information
information in
in the
the
USER_SEQUENCES
USER_SEQUENCES data
data dictionary
dictionary table.
table.
Create
Create indexes
indexes to
to improve
improve query
query retrieval
retrieval
speed.
speed.
View
View index
index information
information in
in the
the
USER_INDEXES
USER_INDEXES dictionary
dictionary table.
table.
Use
Use synonyms
synonyms to
to provide
provide alternative
alternative
names
names for
for objects.
objects.
13-24

Copyright Oracle Corporation, 1998. All rights reserved.

Practice Overview
Creating
Creating sequences
sequences
Using
Using sequences
sequences
Creating
Creating nonunique
nonunique indexes
indexes
Displaying
Displaying data
data dictionary
dictionary information
information
about
about sequences
sequences and
and indexes
indexes
Dropping
Dropping indexes
indexes

13-25

Copyright Oracle Corporation, 1998. All rights reserved.

13-26

Copyright Oracle Corporation, 1998. All rights reserved.

You might also like