SEQUNCES Lecture_Notes_Unit4_chapter11_sequence

muruganjit1 930 views 16 slides Jul 09, 2024
Slide 1
Slide 1 of 16
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16

About This Presentation

Title: Relational Database Management System Concepts(RDBMS)

Description:
Welcome to the comprehensive guide on Relational Database Management System (RDBMS) concepts, tailored for final year B.Sc. Computer Science students affiliated with Alagappa University. This document covers fundamental princ...


Slide Content

RDBMS -Unit IV
Chapter 11
SEQUENCES
Prepared By
Dr. S.Murugan, Associate Professor
Department of Computer Science,
AlagappaGovernment Arts College, Karaikudi.
(Affiliated by AlagappaUniversity)
Mailid: [email protected]
Reference Book:
LEARN ORACLE 8i, JOSE A RAMALHO

Sequences
➢Asequenceissimplyanautomaticcounter,whichis
enabledwheneveritisaccessed.

➢Thisguaranteesthattherearenotworowswiththe
samecode.
➢Adefaultsequencehasthefollowingcharacteristics:
•Always starts from the number 1.
•In ascending order.
•Increases by 1.

The CREATE SEQUENCE Command
➢TheSQLcommandusedtocreateasequenceisthe
CREATESEQUENCEcommand.

The CREATE SEQUENCE Command
➢STARTWITH:Indicatestheinitialvalueofthe
sequence.
➢INCREMENT BY:Indicatesthevaluebywhichthe
sequencewillbeincrementedeachtimeitisaccessed.
➢MAXVALUE:Indicatesthemaximumvalueofthe
sequence.Whenomitted,themaximumvalueofthe
sequencecanbe1.00e+27.
➢NOMAXVALUE :Indicatesthatthesequencedoesn’t
haveapredefinedmaximumvalue.
➢CYCLE/NOCYCLE :CYCLEindicatesthatthe
sequenceshouldreturntotheinitialvaluewhenthe
maximumvalueisreached.

Pseudo-Columns
➢Oraclehasseveralpseudo-columnsthatbehaveasan
extracolumnwhenanewtableiscreated.
➢Thecurrvalandnextvalpseudo-columnsaredirectly
relatedtosequences.Therearetwoadditionalpseudo-
columns:levelandrownum
Currval:Returnsthecurrentvalueofthesequence.
Nextval:Increasesthevalueofthesequenceandreturns
thenextvalue.
Rowid:Itisusedtoindividualizeeachrowofatable.
rowidoffersafasterandmorepracticalwaytoaccessa
rowintheentiredatabase.

Example for ROWID

Example for ROWNUM
➢Therownumpseudo-columnreceivesanumber
thatcorrespondstothepositionofarowintheset
ofrowsreturnedbyaquery.

Creating a Sequence –Example 1
➢Thefollowingexampleillustratesthecreationofthe
Newseqsequenceusingthedefaultvalues:
➢SQL>createsequencenewseq;

Checking a User’s List of Sequences
Tocheckthesequencesthatwerecreatedforauserand
thevaluesoftheirparameters,youuseatablecalled
USER_SEQUENCES.

Sequence –Example2
➢createasequencewithaninitialvalue,anincrement
value,andthesizeofthecachespecifiedbythe
currentuser:

Sequence –Example3
➢Inthisnextexample,thesequenceiscreatedwith
minimumandmaximumvalues:
SQL> create sequence newseq3
minvalue1000
maxvalue4000;
Sequence created:

Deleting a Sequence
➢TheDROPSEQUENCE commandremovesthe
sequencefromtheschemainwhichitwascreated.In
thisexample,Newseq2isremoved:
SQL> drop sequence newseq2;

Changing a Sequence
➢WiththeALTERSEQUENCEcommandtheusercan
changesomeofthesequence’sparameters.However,
thereareseveralrestrictions.
➢Theinitialvaluecannotbealtered
➢Theminimumvalueforthetablecannotbegreater
thanthecurrentvalue.
➢Theincrementvaluecanbechangedwithoutany
problems.Inthenextexample,twosuccessfulchanges
weremadetotheNewseq2sequence:
SQL>altersequencenewseq2
minvalue30
incrementby20;
Sequencealtered.

Using a Sequence
➢SQL>createnewseq3minval1000maxval1005.
➢Thesequenceisenabledthefirsttimeitisused,and
displaysitsinitialvalue:
SQL>selectnewseq3.nextvalfromdual;
NEXTVAL
-------
1000
➢Thesecondtimeitisused,itdisplaysthevalue
incrementedby1.
SQL>run
NEXTVAL
-------
1001

Using a Sequence
➢Whenweaccesstocurrval,itdoesnotchangethe
valueofthesequence:
SQL> select newseq3.currval from dual;
CURRVAL
-------
1001
SQL> run
CURRVAL
-------
1001

Using a Sequence
➢InsertingSequencenumbertothedatabase.
1.Createatable
2.createasequence
3.Insertsequenceintoatable.
3.Viewtherecordsfromtable.
1.Createtableproduct(snonumber(3),item_name
char(20),bill_datedate);
2.Createsequenceserialminvalue1maxvalue100;
3.Insertintoproductvalues(serial.nextval,’RICE’,’14-
JUL-19’);
4.Repeatthestep3asyouwanttoinsert.
5.Select*fromproduct;