Sunteți pe pagina 1din 24

13

Other Database Objects


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 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
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
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}];

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> SELECT
SELECT sequence_name,
sequence_name, min_value,
min_value, max_value,
max_value,
22 increment_by,
increment_by, last_number
last_number
33 FROM
FROM 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
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 it
it 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
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 dept(deptno,
dept(deptno, dname,
dname, loc)
loc)
22 VALUES
VALUES (dept_deptno.NEXTVAL,
(dept_deptno.NEXTVAL,
33 'MARKETING',
'MARKETING', 'SAN
'SAN DIEGO');
DIEGO');
11 row
row created.
created.

•• 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;

13-10
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
Guidelines for Modifying
a Sequence
•• You
You must
must be
be the
the owner
owner oror 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
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, 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.

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 toto 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
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 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.

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 emp_ename_idx
emp_ename_idx
22 ON
ON emp(ename);
emp(ename);
Index
Index created.
created.

13-17
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 areare 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 2–4%
2–4% of of the
the
rows.
rows.
13-18
When Not to Create an Index

•• 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.

13-19
Confirming Indexes
•• The
The USER_INDEXES
USER_INDEXES datadata 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> SELECT ic.index_name, ic.column_name,
2 ic.column_position col_pos,ix.uniqueness
3 FROM user_indexes ix, user_ind_columns ic
4 WHERE ic.index_name = ix.index_name
5 AND ic.table_name = 'EMP';

13-20
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 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 byby another
another user.
user.
•• Shorten
Shorten lengthy
lengthy object
object names.
names.
CREATE
CREATE [PUBLIC]
[PUBLIC] SYNONYM
SYNONYM synonym
synonym
FOR
FOR object;
object;

13-22
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 d_sum
d_sum
22 FOR
FOR dept_sum_vu;
dept_sum_vu;
Synonym
Synonym Created.
Created.

•• 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 byby 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 toto 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 sequences
• Creating sequences
• Using sequences
• Using sequences
• Creating nonunique indexes
• Creating nonunique indexes
• Displaying data dictionary information about sequences and indexes
• Displaying data dictionary information about sequences and indexes
• Dropping indexes
• Dropping indexes

13-25

S-ar putea să vă placă și