Your SlideShare is downloading. ×
Oracle Database Sequence
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Oracle Database Sequence

866

Published on

Presentation about Sequence in Oracle database. …

Presentation about Sequence in Oracle database.
Advanced Database Lab - Informatics Engineering, Bakrie University

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
866
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
20
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. E R Y K B U D I P R A T A M AT I F L A B A S S I S T A N T – I N F O R M A T I C S E N G I N E E R I N GORACLE DATABASESEQUENCE
  • 2. DEFINITION• Just like tables, views, indexes, andsynonyms, a sequence is a type of databaseobject.• Sequences are used to generate unique,sequential integer values that are used asprimary key values in database tables.• The sequence of numbers can begenerated in either ascending ordescending order.
  • 3. SEQUENCE SYNTAXCREATE SEQUENCE <sequence name>[INCREMENT BY <number>][START WITH <start value number>][MAXVALUE <MAXIMUM VLAUE NUMBER>][NOMAXVALUE][MINVALUE <minimum value number>][CYCLE][NOCYCLE][CACHE <number of sequence value to cache>][NOCACHE][ORDER][NOORDER];
  • 4. EXAMPLECREATE SEQUENCEorder_number_sequenceINCREMENT BY 1START WITH 1MAXVALUE 100000000MINVALUE 1CYCLECACHE 10;Sequence created.
  • 5. ACCESSING SEQUENCE VALUES• Sequence values are generated through the use oftwo pseudocolumns named currval and nextval.• A pseudocolumn behaves like a table column, butpsuedocolumns are not actually stored in a table.• We can select values from pseudocolumns butcannot perform manipulations on their values.• The first time you select the nextval pseudocolumn,the initial value in the sequence is returned.• Subsequent selections of the nextval pseudocolumnwill cause the sequence to increment as specifiedby the INCREMENT BY clause and will return thenewly generated sequence value.
  • 6. • The currval pseudocolumn returns thecurrent value of the sequence, which is thevalue returned by the last reference tonextval.ExampleCREATE TABLE sales_order (order_number NUMBER(9)CONSTRAINT pk_sales_order PRIMARY KEY,order_amount NUMBER(9,2));
  • 7. • The INSERT commands shown below insert threerows into the sales_order table. The INSERTcommands reference theorder_number_sequence.nextval pseudocolumn.INSERT INTO sales_orderVALUES(order_number_sequence.nextval,155.59 );INSERT INTO sales_orderVALUES(order_number_sequence.nextval,450.00 );INSERT INTO sales_orderVALUES(order_number_sequence.nextval,16.95);
  • 8. SELECT * FROM sales_order;ORDER_NUMBER ORDER_AMOUNT------------ ------------1 155.592 4503 16.95
  • 9. • Use of currval.CREATE TABLE order_details (order_number NUMBER(9),order_row NUMBER(3),product_desc VARCHAR2(15),quantity_ordered NUMBER(3),product_price NUMBER(9,2),CONSTRAINT pk_order_detailsPRIMARY KEY (order_number, order_row),CONSTRAINT fk_order_number FOREIGN KEY(order_number) REFERENCES sales_order);
  • 10. • The order_details table has a FOREIGNKEY reference to the sales_order tablethrough the order_number column.DELETE FROM sales_order;INSERT INTO sales_orderVALUES ( order_number_sequence.nextval, 200.00 );INSERT INTO order_detailsVALUES ( order_number_sequence.currval, 1, EndTable,1, 100.00);INSERT INTO order_detailsVALUES ( order_number_sequence.currval, 2, TableLamp,2, 50.00);
  • 11. SELECT * FROM sales_order;ORDER_NUMBER ORDER_AMOUNT------------ ------------5 200SELECT *FROM order_details;ORDER_NUMBER ORDER_ROW PRODUCT_DESC QUANTITY_ORDEREDPRODUCT_PRICE--------- -------- ---------- ------------- ----------5 1 End Table 1 1005 2 Table Lamp 2 50
  • 12. ALTERING A SEQUENCE• A sequence is usually altered when it is desirable toset or eliminate the values of the MINVALUE orMAXVALUE parameters, or to change theINCREMENT BY value, or to change the number ofcached sequence numbers.• The ALTER SEQUENCE command shown herechanges the MAXVALUE of theorder_number_sequence to 200,000,000.ALTER SEQUENCE order_number_sequence MAXVALUE200000000;
  • 13. ALTERING A SEQUENCE• When specifying a MINVALUE clause, thespecified value should be less than theMAXVALUE where a sequence generatesascending numbers.• In the case of a descending sequence, theMAXVALUE should be less than theMINVALUE.
  • 14. VIEWING SEQUENCE PROPERTIES• You may need to review the names andproperties of your sequences.• You can do this by querying theUSER_SEQUENCES system view with a SELECTcommand.This view is part of the databasesdata dictionary.SELECT * FROM USER_SEQUENCES;SEQUENCE_NAME MIN_VAL MAX_VALUE INCRE C OCACHE_SIZE Last_N---------------- ------ ---------- ----- -- -- -------------ORDER_NUMBER_SEQUENCE 1 200000000 1 Y N 10 6
  • 15. DROPPING A SEQUENCE• DROP SEQUENCE command is used to dropsequences that need to be recreated or areno longer needed.• The general format is shown here along withan example that drops theorder_number_sequence object.DROP SEQUENCE <sequence name>;DROP SEQUENCE order_number_sequence;Sequence dropped.
  • 16. Q&A

×