Unformatted text preview:

SequenceSlide 2Slide 3Creating a sequence in usual syntaxMore completed syntax about create sequenceUsing a sequenceCreate a sequenceHow to use the created sequencesDrop sequenceAlter sequenceQuestion: How do we set the LASTVALUE value in an Oracle Sequence?Alternatively you can drop that sequence, and combine alter command to adjust the last valueSequenceSen Zhang•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.•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.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.More completed syntax about create sequence•See page 267 for more parametersUsing a sequence•Sequence.nextval•Sequence.currvalCreate a sequenceHow 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-270Drop sequence•SQL> create sequence sq1;•Sequence created.•SQL> drop sequence sq1;•Sequence dropped.•SQL>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;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.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


View Full Document

Oneonta CSCI 242 - Lecture Notes

Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?