Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
Database Objects
Object
Description
Table
View
Sequence
Index
Synonym
13-3
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
13-5
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
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
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;
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
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
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
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
13-16
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);
13-19
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';
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
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;
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
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
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
13-26