Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sequence Sen Zhang. The AutoNumber data type stores an integer that Access increments (adds to) automatically as you add new records. You can use the.

Similar presentations


Presentation on theme: "Sequence Sen Zhang. The AutoNumber data type stores an integer that Access increments (adds to) automatically as you add new records. You can use the."— Presentation transcript:

1 Sequence Sen Zhang

2 The AutoNumber data type stores an integer that Access increments (adds to) automatically as you add new records. You can use the AutoNumber data type as a unique record identification (usually primary key ) for tables having no other unique value or no other more proper primary key candidates.

3 Similar to that Access supports autonumber, Oracle allows you to create counters called sequences that increment each time they are used. In MSSQL, the similar concept is identity, much easier than Oracle.

4 Creating a sequence in usual syntax create sequence sequence_name; This simple command creates a sequence that starts at 1 and increments by 1 each time it is used. This is often all you will require from a sequence.

5 More completed syntax about create sequence See page 267 for more parameters

6 Using a sequence Sequence.nextval Sequence.currval

7 Create a sequence

8 How to use the created sequences Sequential lists of numbers to create unique surrogate key values. This can be useful when you need to create a unique number or to act as a primary key. To use a sequence: you typically use two pseudocolumns Currval and nextval –SELECT sequence_name.NEXTVAL FROM DUAL; –INSERT INTO location (LOC_ID) VALUES(loc_id_sequence.NEXTVAL); Read book 226-270

9 Drop sequence SQL> create sequence sq1; Sequence created. SQL> drop sequence sq1; Sequence dropped. SQL>

10 Alter sequence Once a sequence has been created, you can modify it in a number of ways. You can alter different parameter values. Alter sequence sq1 maxvalue 10;

11 Question: How do we set the LASTVALUE value in an Oracle Sequence? Answer: There is no single command doing that at this moment. You can change the LASTVALUE for an Oracle sequence, by executing an ALTER SEQUENCE command. For example, if the last value used by the Oracle sequence was 100 and you would like to reset the sequence to serve 225 as the next value. Solution 1: You would execute the following commands. –alter sequence test_seq increment by 124; –select test_seq.nextval from dual; –alter sequence test_seq increment by 1; –Now, the next value to be served by the sequence will be 225.

12 Alternatively you can drop that sequence, and combine alter command to adjust the last value Option 2: Drop sequence test_seq; create sequence test_seq; alter sequence test_seq increment by 224; select test_seq.nextval from dual; alter sequence test_seq increment by 1; select test_seq.nextval from dual; 225


Download ppt "Sequence Sen Zhang. The AutoNumber data type stores an integer that Access increments (adds to) automatically as you add new records. You can use the."

Similar presentations


Ads by Google