Tài liệu creating sequences pdf

26 213 0
Tài liệu creating sequences pdf

Đ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

Creating Sequences 13 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder13Ć2 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 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 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 [...].. .Creating a Sequence continued Confirming Sequences Once you have created your sequence, it is documented in the data dictionary Since a sequence 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. .. last_number user _sequences; SEQUENCE_NAME MIN_VALUE MAX_VALUE INCREMENT_BY LAST_NUMBER - - - S_CUSTOMER_ID 1 9999999 1 216 S_DEPT_ID 1 9999999 1 51 S_EMP_ID 1 9999999 1 26 S_IMAGE_ID 1 9999999 1 1981 S_LONGTEXT_ID 1 9999999 1 1369 S_ORD_ID 1 9999999 1 113 S_PRODUCT_ID 1 9999999 1 50537 S_REGION_ID 1 9999999 1 6 S_WAREHOUSE_ID 1 9999999 1 10502 9 rows selected Creating Sequences. .. TABLE command For more information, see Oracle7 Server SQL Reference, Release 7.3, “Pseudocolumns” section and “CREATE SEQUENCE.” Creating Sequences 13Ć13 13Ć14 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Using a Sequence continued Caching Sequence Values Cache sequences in the memory to allow faster access to those sequence values The cache is populated at the first reference to the... Because sequences are not tied directly to tables, the same sequence can be used for multiple 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 _SEQUENCES table Creating. .. users Information about 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 Creating Sequences 13Ć21 13Ć22 Introduction to Oracle: SQL and PL/SQL Using... Using Procedure Builder Practice Overview In this practice, you will create a sequence to be used when populating your DEPARTMENT and WORKER tables Practice Contents D Creating a sequence D Modifying a sequence D Using a sequence Creating Sequences 13Ć23 13Ć24 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Practice 13 1 Create a sequence to be used with the DEPARTMENT table’s primary key... ALTER SEQUENCE The sequence must be dropped and re-created in order to restart the sequence at a different number For more information, see Oracle7 Server SQL Reference, Release 7.3, “ALTER SEQUENCE.” Creating Sequences 13Ć17 13Ć18 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Removing a Sequence To remove a sequence from the data dictionary, use the DROP SEQUENCE command You must be the... 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.” Creating Sequences 13Ć19 13Ć20 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Summary The sequence generator can be used to automatically generate sequence numbers for rows in tables This can... other employee is Anna Seigher who is the Vice President in the Finance department 7 Confirm your additions to the DEPARTMENT table and WORKER table Note the highest primary key values for each table Creating Sequences 13Ć25 Practice 13 continued If you have time, complete the following exercises: 8 Execute p13q4.sql to insert four additional departments named Accounting, Warehouse, Operations, and Research... 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 _SEQUENCES table Creating Sequences 13Ć15 13Ć16 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Altering a Sequence If you reach the MAXVALUE limit for your sequence, no additional values from the sequence will be . Creating Sequences 13 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder13Ć2 Creating Sequences 13Ć3 Objectives Many. SQL and PL/SQL Using Procedure Builder13Ć10 Creating Sequences 13Ć11 Creating a Sequence continued Confirming Sequences Once you have created your sequence,

Ngày đăng: 24/01/2014, 10:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan