SlideShare a Scribd company logo
RDBMS - Unit IV
Chapter 11
SEQUENCES
Prepared By
Dr. S.Murugan, Associate Professor
Department of Computer Science,
Alagappa Government Arts College, Karaikudi.
(Affiliated by Alagappa University)
Mailid: muruganjit@gmail.com
Reference Book:
LEARN ORACLE 8i, JOSE A RAMALHO
Sequences
➢ A sequence is simply an automatic counter, which is
enabled whenever it is accessed.
➢
➢ This guarantees that there are no two rows with the
same code.
➢ A default sequence has the following characteristics:
• Always starts from the number 1.
• In ascending order.
• Increases by 1.
The CREATE SEQUENCE Command
➢ The SQL command used to create a sequence is the
CREATE SEQUENCE command.
The CREATE SEQUENCE Command
➢ START WITH: Indicates the initial value of the
sequence.
➢ INCREMENT BY: Indicates the value by which the
sequence will be incremented each time it is accessed.
➢ MAXVALUE: Indicates the maximum value of the
sequence. When omitted, the maximum value of the
sequence can be 1.00e +27.
➢ NOMAXVALUE: Indicates that the sequence doesn’t
have a predefined maximum value.
➢ CYCLE/NOCYCLE: CYCLE indicates that the
sequence should return to the initial value when the
maximum value is reached.
Pseudo-Columns
➢ Oracle has several pseudo-columns that behave as an
extra column when a new table is created.
➢ The currval and nextval pseudo-columns are directly
related to sequences. There are two additional pseudo-
columns: level and rownum
Currval: Returns the current value of the sequence.
Nextval: Increases the value of the sequence and returns
the next value.
Rowid: It is used to individualize each row of a table.
rowid offers a faster and more practical way to access a
row in the entire database.
Example for ROWID
Example for ROWNUM
➢ The rownum pseudo-column receives a number
that corresponds to the position of a row in the set
of rows returned by a query.
Creating a Sequence – Example 1
➢ The following example illustrates the creation of the
Newseq sequence using the default values:
➢ SQL> create sequence newseq;
Checking a User’s List of Sequences
To check the sequences that were created for a user and
the values of their parameters, you use a table called
USER_SEQUENCES.
Sequence – Example2
➢ create a sequence with an initial value, an increment
value, and the size of the cache specified by the
current user:
Sequence – Example3
➢ In this next example, the sequence is created with
minimum and maximum values:
SQL> create sequence newseq3
minvalue 1000
maxvalue 4000;
Sequence created:
Deleting a Sequence
➢ The DROP SEQUENCE command removes the
sequence from the schema in which it was created. In
this example, Newseq2 is removed:
SQL> drop sequence newseq2;
Changing a Sequence
➢ With the ALTER SEQUENCE command the user can
change some of the sequence’s parameters. However,
there are several restrictions.
➢ The initial value can not be altered
➢ The minimum value for the table cannot be greater
than the current value.
➢ The increment value can be changed without any
problems. In the next example, two successful changes
were made to the Newseq2 sequence:
SQL> alter sequence newseq2
minvalue 30
increment by 20;
Sequence altered.
Using a Sequence
➢ SQL>create newseq3 minval 1000 maxval 1005.
➢ The sequence is enabled the first time it is used, and
displays its initial value:
SQL> select newseq3.nextval from dual;
NEXTVAL
-------
1000
➢ The second time it is used, it displays the value
incremented by 1.
SQL> run
NEXTVAL
-------
1001
Using a Sequence
➢ When we access to currval, it does not change the
value of the sequence:
SQL> select newseq3.currval from dual;
CURRVAL
-------
1001
SQL> run
CURRVAL
-------
1001
Using a Sequence
➢ Inserting Sequence number to the database.
1. Create a table
2. create a sequence
3. Insert sequence into a table.
3. View the records from table.
1. Create table product(sno number(3), item_name
char(20), bill_date date);
2. Create sequence serial minvalue 1 maxvalue 100;
3. Insert into product values (serial.nextval,’RICE’,’14-
JUL-19’);
4. Repeat the step 3 as you want to insert.
5. Select * from product;

More Related Content

PPT
Sequences
PPT
PPTX
Oracle Database Sequence
PPTX
Part 3 drop sequence nextval,currval
PDF
Lecture Notes Unit5 chapter 15 PL/SQL Programming
PDF
Lecture Notes Unit5 chapter16 Trigger Creation
PPTX
Procedures and triggers in SQL
PPT
Lecture 3. MS SQL. Cursors.
Sequences
Oracle Database Sequence
Part 3 drop sequence nextval,currval
Lecture Notes Unit5 chapter 15 PL/SQL Programming
Lecture Notes Unit5 chapter16 Trigger Creation
Procedures and triggers in SQL
Lecture 3. MS SQL. Cursors.

Similar to SEQUNCES Lecture_Notes_Unit4_chapter11_sequence (20)

DOCX
Trig
PDF
Sequencereset
ODP
ODP
PPTX
NUMERICAL METHODS WITH MATLAB PROGRAMMING
PDF
sql10.pdfNGBHJHYGKIIIIYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
DOCX
two dimensional array
PPTX
Part 7 alter sequence modified
PDF
Power System Analysis and Design
PPTX
Presentation on shunt active filter
PDF
09_Control_Structures_and_Flowcharts.pdf
PDF
Taming velocity - a tale of four streams
PPTX
WWW 2008 Poster - Efficient mining of frequent sequence generators
PDF
PDF
JavaProgrammingManual
PPTX
Oracle: Cursors
PPTX
Oracle:Cursors
PPT
SQL WORKSHOP::Lecture 13
PDF
NuMachine and NuAlgebra
PPT
05-stack_basicqueuexample to clear concept.ppt
Trig
Sequencereset
NUMERICAL METHODS WITH MATLAB PROGRAMMING
sql10.pdfNGBHJHYGKIIIIYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
two dimensional array
Part 7 alter sequence modified
Power System Analysis and Design
Presentation on shunt active filter
09_Control_Structures_and_Flowcharts.pdf
Taming velocity - a tale of four streams
WWW 2008 Poster - Efficient mining of frequent sequence generators
JavaProgrammingManual
Oracle: Cursors
Oracle:Cursors
SQL WORKSHOP::Lecture 13
NuMachine and NuAlgebra
05-stack_basicqueuexample to clear concept.ppt
Ad

Recently uploaded (20)

PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
PDF
Hazard Identification & Risk Assessment .pdf
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PPTX
Unit 4 Skeletal System.ppt.pptxopresentatiom
PDF
advance database management system book.pdf
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
PDF
Trump Administration's workforce development strategy
PDF
Classroom Observation Tools for Teachers
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PPTX
Lesson notes of climatology university.
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Introduction to Building Materials
PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
medical_surgical_nursing_10th_edition_ignatavicius_TEST_BANK_pdf.pdf
Hazard Identification & Risk Assessment .pdf
UNIT III MENTAL HEALTH NURSING ASSESSMENT
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
Unit 4 Skeletal System.ppt.pptxopresentatiom
advance database management system book.pdf
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Chinmaya Tiranga Azadi Quiz (Class 7-8 )
Trump Administration's workforce development strategy
Classroom Observation Tools for Teachers
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
LDMMIA Reiki Yoga Finals Review Spring Summer
Lesson notes of climatology university.
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Introduction to Building Materials
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
Final Presentation General Medicine 03-08-2024.pptx
Ad

SEQUNCES Lecture_Notes_Unit4_chapter11_sequence

  • 1. RDBMS - Unit IV Chapter 11 SEQUENCES Prepared By Dr. S.Murugan, Associate Professor Department of Computer Science, Alagappa Government Arts College, Karaikudi. (Affiliated by Alagappa University) Mailid: muruganjit@gmail.com Reference Book: LEARN ORACLE 8i, JOSE A RAMALHO
  • 2. Sequences ➢ A sequence is simply an automatic counter, which is enabled whenever it is accessed. ➢ ➢ This guarantees that there are no two rows with the same code. ➢ A default sequence has the following characteristics: • Always starts from the number 1. • In ascending order. • Increases by 1.
  • 3. The CREATE SEQUENCE Command ➢ The SQL command used to create a sequence is the CREATE SEQUENCE command.
  • 4. The CREATE SEQUENCE Command ➢ START WITH: Indicates the initial value of the sequence. ➢ INCREMENT BY: Indicates the value by which the sequence will be incremented each time it is accessed. ➢ MAXVALUE: Indicates the maximum value of the sequence. When omitted, the maximum value of the sequence can be 1.00e +27. ➢ NOMAXVALUE: Indicates that the sequence doesn’t have a predefined maximum value. ➢ CYCLE/NOCYCLE: CYCLE indicates that the sequence should return to the initial value when the maximum value is reached.
  • 5. Pseudo-Columns ➢ Oracle has several pseudo-columns that behave as an extra column when a new table is created. ➢ The currval and nextval pseudo-columns are directly related to sequences. There are two additional pseudo- columns: level and rownum Currval: Returns the current value of the sequence. Nextval: Increases the value of the sequence and returns the next value. Rowid: It is used to individualize each row of a table. rowid offers a faster and more practical way to access a row in the entire database.
  • 7. Example for ROWNUM ➢ The rownum pseudo-column receives a number that corresponds to the position of a row in the set of rows returned by a query.
  • 8. Creating a Sequence – Example 1 ➢ The following example illustrates the creation of the Newseq sequence using the default values: ➢ SQL> create sequence newseq;
  • 9. Checking a User’s List of Sequences To check the sequences that were created for a user and the values of their parameters, you use a table called USER_SEQUENCES.
  • 10. Sequence – Example2 ➢ create a sequence with an initial value, an increment value, and the size of the cache specified by the current user:
  • 11. Sequence – Example3 ➢ In this next example, the sequence is created with minimum and maximum values: SQL> create sequence newseq3 minvalue 1000 maxvalue 4000; Sequence created:
  • 12. Deleting a Sequence ➢ The DROP SEQUENCE command removes the sequence from the schema in which it was created. In this example, Newseq2 is removed: SQL> drop sequence newseq2;
  • 13. Changing a Sequence ➢ With the ALTER SEQUENCE command the user can change some of the sequence’s parameters. However, there are several restrictions. ➢ The initial value can not be altered ➢ The minimum value for the table cannot be greater than the current value. ➢ The increment value can be changed without any problems. In the next example, two successful changes were made to the Newseq2 sequence: SQL> alter sequence newseq2 minvalue 30 increment by 20; Sequence altered.
  • 14. Using a Sequence ➢ SQL>create newseq3 minval 1000 maxval 1005. ➢ The sequence is enabled the first time it is used, and displays its initial value: SQL> select newseq3.nextval from dual; NEXTVAL ------- 1000 ➢ The second time it is used, it displays the value incremented by 1. SQL> run NEXTVAL ------- 1001
  • 15. Using a Sequence ➢ When we access to currval, it does not change the value of the sequence: SQL> select newseq3.currval from dual; CURRVAL ------- 1001 SQL> run CURRVAL ------- 1001
  • 16. Using a Sequence ➢ Inserting Sequence number to the database. 1. Create a table 2. create a sequence 3. Insert sequence into a table. 3. View the records from table. 1. Create table product(sno number(3), item_name char(20), bill_date date); 2. Create sequence serial minvalue 1 maxvalue 100; 3. Insert into product values (serial.nextval,’RICE’,’14- JUL-19’); 4. Repeat the step 3 as you want to insert. 5. Select * from product;