Tài liệu Creating sequences docx

26 423 0
Tài liệu Creating sequences docx

Đ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 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. [...].. .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. .. 7.3, “Pseudocolumns” section and “CREATE SEQUENCE.” Class Management Note: Be sure to point out the rules listed on this page 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... 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... 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 Class Management Note: Duration: 25 minutes For additional exercises, see Practice 17, Exercises 3,4,5,6,7,8 Creating Sequences 13Ć23 13Ć24 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Practice 13 1 Create a sequence... sequence at a different number For more information, see Oracle7 Server SQL Reference, Release 7.3, “ALTER SEQUENCE.” Class Management Note: PowerPoint: The bottom slide contains the build feature 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... crashes 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... next sequence value Trust the system to provide a unique value each time a sequence is used in an INSERT statement The next available value of a sequence is not useful information in its own right 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... 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 . primary key value. When naming your sequences, consider using the table_column naming convention. Creating Sequences 13Ć9 Creating a Sequence continued Example. after all the numbers in the cache are used. Creating Sequences 13Ć11 Creating a Sequence continued Confirming Sequences Once you have created your sequence,

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

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