1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Creating sequences docx

26 424 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Creating sequences
Chuyên ngành Oracle Database
Thể loại Lecture
Định dạng
Số trang 26
Dung lượng 170,98 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 13Ć2Schedule: Timing Topic 20 minutes Lecture 25 minutes Practice 45 minutes Total Class Management Note: Files required fo

Trang 1

Creating Sequences

13

Trang 2

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 13Ć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

Trang 3

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

Trang 4

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 13Ć4

Trang 5

A sequence generator can be used to automatically generate sequence numbers forrows in tables A sequence is a database object created by a user and can be shared bymultiple users

A typical usage for sequences is to create a primary key value, which must be uniquefor each row The sequence is generated and incremented (or decremented) by aninternal Oracle7 routine This can be a time saving object because it can reduce theamount of application code needed to write a sequence generating routine

Sequence numbers are stored and generated independently of tables Therefore, thesame sequence can be used for multiple tables

Trang 6

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 13Ć6

Technical Note:

If the INCREMENT BY value is negative, the sequence will descend.Additionally, NOMAXVALUE then specifies a maximum value of -1 andNOMINVALUE sets a minimum value of -(1026)

Also, ORDER | NOORDER options are available The ORDER optionguarantees that sequence values are generated in order It is not important ifyou use the sequence to generate primary key values This option is onlyrelevant with the Parallel Server option

If sequence values are cached, they will be lost if there is a system failure

Trang 7

Creating a Sequence

Define a sequence to generate sequential numbers automatically by using the

CREATE SEQUENCE command

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 sequencewill start with 1

MAXVALUE n specifies the maximum value the sequence can

generate

NOMAXVALUE specifies a maximum value of 1027 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 orminimum value or does not generate additionalvalues NOCYCLE is the default option

CACHE n | NOCACHE specifies how many values the Oracle7 Server

will preallocate and keep in memory Bydefault, the Server will cache 20 values

For more information, see

Oracle7 Server SQL Reference, Release 7.3, “CREATE SEQUENCE.”

Trang 8

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 13Ć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 aprimary key value

When naming your sequences, consider using the table_column naming

convention

Trang 9

Creating a Sequence continued

Example

Create a sequence named S_DEPT_ID to be used for the DEPT_ID column of theS_DEPT table Start the sequence at 51 Do not allow caching and do not allow thesequence to cycle

SQL> CREATE SEQUENCE s_dept_id

Trang 10

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 13Ć10

Class Management Note:

DEMO: l13dd.sql

PURPOSE: Display the USER_SEQUENCES data dictionary structure andcontents Explain that the LAST_NUMBER column is the last numbergenerated 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 valueafter all the numbers in the cache are used

Trang 11

Creating a Sequence continuedConfirming Sequences

Once you have created your sequence, it is documented in the data dictionary Since asequence is a database object, you can identify it in the USER_OBJECTS data

dictionary table

You can also confirm the settings of the sequence by selecting from the data

dictionary’s USER_SEQUENCES table

Example

Display information about all the sequences that you own

SQL> SELECT sequence_name, min_value, max_value,

Trang 12

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 13Ć12

Trang 13

Using a Sequence

Once you create your sequence, you can use the sequence to generate sequentialnumbers for use in your tables Reference the sequence values by using the

NEXTVAL and CURRVAL pseudocolumns

NEXTVAL and CURRVAL Pseudocolumns

The NEXTVAL pseudocolumn is used to extract successive sequence numbers from aspecified sequence You must qualify NEXTVAL with the sequence name When you

reference sequence.NEXTVAL, a new sequence number is generated and the current

sequence number is placed in CURRVAL

The CURRVAL pseudocolumn is used to refer to a sequence number that the currentuser has just generated NEXTVAL must be used to generate a sequence number inthe current user’s session before CURRVAL can be referenced You must qualify

CURRVAL with the sequence name When sequence.CURRVAL is referenced, the

last value returned to that user’s process is displayed

Rules for Using NEXTVAL and CURRVAL

You can use NEXTVAL and CURRVAL in

D The SELECT list of a SELECT statement that is not part of a subquery

D The SELECT list of a subquery in an INSERT statement

D The VALUES clause of an INSERT statement

D The SET clause of an UPDATE statement

You cannot use NEXTVAL and CURRVAL in

D A SELECT list of a view

D A SELECT statement with the DISTINCT keyword

D A SELECT statement with the GROUP BY, HAVING, or ORDER BY clauses

D A subquery in a SELECT, DELETE, or UPDATE statement

D A DEFAULT expression in a CREATE TABLE or ALTER TABLE command.For more information, see

Oracle7 Server SQL Reference, Release 7.3, “Pseudocolumns” section and “CREATE

SEQUENCE.”

Class Management Note:

Be sure to point out the rules listed on this page

Trang 14

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 13Ć14

Trang 15

Using a Sequence continuedCaching Sequence Values

Cache sequences in the memory to allow faster access to those sequence values Thecache is populated at the first reference to the sequence Each request for the nextsequence value is retrieved from the cached sequence After the last sequence is used,the next request for the sequence pulls another cache of sequences into memory

Beware of Gaps in Your Sequence

Although sequence generators issue sequential numbers without gaps, this actionoccurs independent of a commit or rollback Therefore, if you roll back a commandcontaining a sequence, the number is lost

Another event that can cause gaps in the sequence is a system crash If the sequencecaches values in the memory, then those values are lost if the system crashes

Because sequences are not tied directly to tables, the same sequence can be used formultiple tables If this occurs, each table can contain gaps in the sequential numbers

Viewing the Next Available Sequence Value Without Incrementing It

It is possible to view the next available sequence value without incrementing it, only

if the sequence was created with NOCACHE, by querying the USER_SEQUENCEStable

Technical Note:

Frequently used sequences should be created with caching to improveefficiency For cached sequence, there is no way to find out what the nextavailable sequence value will be without actually obtaining, and using up,that value

It is recommended that users resist finding the next sequence value Trustthe system to provide a unique value each time a sequence is used in anINSERT statement The next available value of a sequence is not usefulinformation in its own right

Trang 16

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 13Ć16

Trang 17

Altering a Sequence

If you reach the MAXVALUE limit for your sequence, no additional values from thesequence will be allocated and you will receive an error indicating the sequenceexceeds the MAXVALUE To continue to use the sequence, you can modify it byusing the ALTER SEQUENCE command

For more information, see

Oracle7 Server SQL Reference, Release 7.3, “ALTER SEQUENCE.”

Class Management Note:

PowerPoint: The bottom slide contains the build feature

Trang 18

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 13Ć18

Trang 19

Removing a Sequence

To remove a sequence from the data dictionary, use the DROP SEQUENCE

command You must be the owner of the sequence or have the DROP ANY

SEQUENCE privilege to remove it

Syntax

DROP SEQUENCE sequence;

where: sequence is the name of the sequence generator

For more information, see

Oracle7 Server SQL Reference, Release 7.3, “DROP SEQUENCE.”

Trang 20

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 13Ć20

Trang 21

The sequence generator can be used to automatically generate sequence numbers forrows in tables This can be time saving, and can reduce the amount of applicationcode needed

A sequence is a database object that can be shared with other users Informationabout the sequence can be found in the USER_SEQUENCES table of the data

dictionary

To use a sequence, reference it with either the NEXTVAL or the CURRVAL

pseudocolumns

D Retrieve the next number in the sequence by referencing sequence.NEXTVAL.

D Return the current available number by referencing sequence.CURRVAL.

Trang 22

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 13Ć22

Trang 23

Practice Overview

In this practice, you will create a sequence to be used when populating your

DEPARTMENT and WORKER tables

Trang 24

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 13Ć24

Trang 25

Practice 13

1. Create a sequence to be used with the DEPARTMENT table’s primary key

column The sequence should start at 76 and have a maximum value of 80 Besure that it increments by one number Name the sequence DEPT_ID_SEQ

2. Create another sequence This sequence will be used with the WORKER table’sprimary key column Start this sequence at 204, and set the maximum value to be

9999999 Be sure that it increments by one number Allow the sequence to cache

5 numbers Name the sequence WORKER_ID_SEQ

3. Write a script to display the following information about your sequences:

sequence name, cache size, maximum value, increment size, and last number

generated Name the script p13q3.sql.

4. Write an interactive script to insert a row into the DEPARTMENT table Name

your script p13q4.sql Be sure to use the sequence you created for the ID column.

Create a customized prompt to enter the department name Execute your script.Add two departments named Education and Administration Confirm your

additions

5. Display information about your sequences by executing the p13q3.sql script.

Notice that the WORKER_ID_SEQ last number does not match the highestprimary key value in Exercise 6 Why?

If you completed Practice 12, Exercise 8, you can do the following two exercises

6. Write a script to insert two rows into the WORKER table Name your script

p13q5.sql Use the sequence you create for the ID column Execute your script.

Add Tomas Lira as the President in the last department you just added to thetable The other employee is Anna Seigher who is the Vice President in theFinance department

7. Confirm your additions to the DEPARTMENT table and WORKER table Notethe highest primary key values for each table

Trang 26

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 13Ć26

If you have time, complete the following exercises:

8. Execute p13q4.sql to insert four additional departments named Accounting,

Warehouse, Operations, and Research What happened and why?

9. Modify your department sequence to allow no maximum value Verify the change

to the sequence by executing the p13q3.sql script.

10. Add the Research department by using your script named p13q4.sql Make this

addition permanent

11. Display the contents of the DEPARTMENT table and WORKER table

Ngày đăng: 10/12/2013, 16:16

TỪ KHÓA LIÊN QUAN

w