Presentation is loading. Please wait.

Presentation is loading. Please wait.

Oracle Sequences Sequences are an independent object in the database (not a data type) Sequences have a name and can be used anywhere a value is expected.

Similar presentations


Presentation on theme: "Oracle Sequences Sequences are an independent object in the database (not a data type) Sequences have a name and can be used anywhere a value is expected."— Presentation transcript:

1 Oracle Sequences Sequences are an independent object in the database (not a data type) Sequences have a name and can be used anywhere a value is expected. Sequences are not tied to a table / column. Sequences generate a numeric value that can be assigned to any column in any table. The table attribute to which you assigned a value based on a sequence can be edited and modified. Sequences can be created and deleted at any time

2 Sequence Syntax CREATE SEQUENCE name[START WITH n] [INCREMENT BY n][CACHE|NOCHACHE] n = can be positive or negative Start with : initial value. The default is 1 Increment by: determines the value by which the sequence incremented. Default value =1 can be + / - to ascend / descend CREATE * FROM USER_SEQUENCES;

3 To check all sequences created : SELECT * FROM USER_SEQENCES; To use sequences during data entry: You must use two special pseudo columns: NEXT VAL, CURRVAL

4 INSERT INTO CUSTOMER VALUES(CUS_CODE_SEQ.NEXTVAL, ’Connery’, ’sean’, NULL, ‘615’,’898-2008’, 0.00); CUS_CODE_SEQ.NEXTVAL: retrieves the next available value from the sequence. Each time you use NEXTVAL, the sequence is incremented. Once a sequence value is used it cannot be used again. If your statement rolls back the sequence value will not. The next time you use NEXTVAL it will look as if it skipped a number. You can issue an INSERT statement without using the sequence.

5 CURRVAL retrieves the current value of a sequence That is the last sequence number used. You cannot use CURRVAL unless a NEXTVAL was issued previously in the same session. The main use for CURRVAL is to enter rows in dependent tables e.g: INVOICE and LINE table are related in 1: M relationship through INV_NUMBER.

6 You use the INV_NUMBER_SEQ sequence to automatically generate invoice numbers. Then, using CURRVAL, you can get the latest INV_NUMBER USED and assign it to the related INV_NUMBER foreign key attribute in the LINE table. Example: INSERT INTO INVOICE VALUES(INV_NUMBER_SEQ.NEXTVAL, 20010,SYSDATE); INSERT INTO LINE VALUES(INV_NUMBER_SEQ.CURRVAL,1, ‘13-Q2/P2’, 1):

7 To Drop a sequence DROP SEQUENCE CUS_CODE_SEQ; REMEMBER The use of sequences is optional. You can enter the values manually A sequence is not associated with a table.


Download ppt "Oracle Sequences Sequences are an independent object in the database (not a data type) Sequences have a name and can be used anywhere a value is expected."

Similar presentations


Ads by Google