Tải bản đầy đủ (.pdf) (26 trang)

Tài liệu Creating sequences docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (170.98 KB, 26 trang )

Creating Sequences
13
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder13Ć2
Schedule: Timing Topic
20 minutes Lecture
25 minutes Practice
45 minutes Total
Class Management Note:
Files required for this lesson are:
Demonstration: l13dd.sql
Practice: None
Creating Sequences 13Ć3
Objectives
Many applications require the use of unique numbers as primary key values.
You can either build code into the application to handle this requirement or use
a sequence to generate unique numbers. This lesson covers creating and using
sequences that create unique numbers.
At the end of this lesson, you should be able to
D
Explain the use of sequences.
D
Create a sequence.
D
Use a sequence.
D
Modify a sequence definition.
D
Remove a sequence.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder13Ć4
Creating Sequences 13Ć5
Overview


A sequence generator can be used to automatically generate sequence numbers for
rows in tables. A sequence is a database object created by a user and can be shared by
multiple users.
A typical usage for sequences is to create a primary key value, which must be unique
for each row. The sequence is generated and incremented (or decremented) by an
internal Oracle7 routine. This can be a time saving object because it can reduce the
amount of application code needed to write a sequence generating routine.
Sequence numbers are stored and generated independently of tables. Therefore, the
same sequence can be used for multiple tables.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder13Ć6
Technical Note:
If the INCREMENT BY value is negative, the sequence will descend.
Additionally, NOMAXVALUE then specifies a maximum value of -1 and
NOMINVALUE sets a minimum value of -(10
26
).
Also, ORDER | NOORDER options are available. The ORDER option
guarantees that sequence values are generated in order. It is not important if
you use the sequence to generate primary key values. This option is only
relevant with the Parallel Server option.
If sequence values are cached, they will be lost if there is a system failure.
Creating Sequences 13Ć7
Creating a Sequence
Define a sequence to generate sequential numbers automatically by using the
CREATE SEQUENCE command.
Abridged Syntax
CREATE SEQUENCE sequence
[INCREMENT BY n]
[START WITH n]
[{MAXVALUE n | NOMAXVALUE

}]
[{MINVALUE n | NOMINVALUE
}]
[{CYCLE | NOCYCLE
}]
[{CACHE n | NOCACHE}]
where: sequence is the name of the sequence generator.
INCREMENT BY n specifies the interval between sequence numbers
where n is an integer. If this clause is omitted,
the sequence will increment by 1.
START WITH n specifies the first sequence number to be
generated. If this clause is omitted, the sequence
will start with 1.
MAXVALUE n specifies the maximum value the sequence can
generate.
NOMAXVALUE specifies a maximum value of 10
27
. This is the
default option.
MINVALUE n specifies the minimum sequence value.
NOMINVALUE specifies a minimum value of 1.
CYCLE | NOCYCLE specifies that the sequence continues to generate
values after reaching either its maximum or
minimum value or does not generate additional
values. NOCYCLE is the default option.
CACHE n | NOCACHE specifies how many values the Oracle7 Server
will preallocate and keep in memory. By
default, the Server will cache 20 values.
For more information, see
Oracle7 Server SQL Reference, Release 7.3, “CREATE SEQUENCE.”

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder13Ć8
Class Management Note:
The MAXVALUE was created because the column precision is 7 digits.
Also, you do not want the sequence to cycle because it is used to generate a
primary key value.
When naming your sequences, consider using the table_column naming
convention.
Creating Sequences 13Ć9
Creating a Sequence
continued
Example
Create a sequence named S_DEPT_ID to be used for the DEPT_ID column of the
S_DEPT table. Start the sequence at 51. Do not allow caching and do not allow the
sequence to cycle.
SQL> CREATE SEQUENCE s_dept_id
2 INCREMENT BY 1
3 START WITH 51
4 MAXVALUE 9999999
5 NOCACHE
6 NOCYCLE;
Sequence created.
Do not use the CYCLE option if the sequence is used to generate primary key values.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder13Ć10
Class Management Note:
DEMO: l13dd.sql
PURPOSE: Display the USER_SEQUENCES data dictionary structure and
contents. Explain that the LAST_NUMBER column is the last number
generated by the sequence if you do not use the CACHE option, which
means that it is the value you acquire when you use a sequence.NEXTVAL
expression.

If you use the CACHE OPTION, LAST_NUMBER displays the next value
after all the numbers in the cache are used.

×