lab manual 20
TRANSCRIPT
![Page 1: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/1.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 1/47
![Page 2: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/2.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 2/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
INDEX
S.No Date Name of the ExperimentPage
NoRemarks
Introduction to SQL
1.Creation, altering and dropping of tables and
inserting rows into a table3
2. Queries (along with sub Queries) 20
3.Queries using Aggregate functions G!"# $%,
&A'G and Creation and dropping of 'iews.2*
+.
Queries using Conersion functions, string
functions, date functions 2-
PL/SQL Introduction 33
. /rite a #Q progra to find the total and
aerage of + sub4ects and displa5 the grade3+
*./rite a #Q progra to find the largest of
three nubers36
7./rite a # Q progra to generate 8ibonacci
series
+0
6. /rite a #Q #rogra to displa5 the nuberin eerse !rder
+3
-a #Q progra to a funtion for finding the
factorial of gien nuber.(8unctions)+
10
/rite a trigger on sailors table that runs for
eer5 insert stateent that should hae age
alue 916.
+7
EXP 1: Introduction to SQL
2
![Page 3: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/3.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 3/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
AIM: :o create alter and dropping of tables and inserting rows into a table(use constraints while creating tables) e;aples using <<C: coand.
DESCRIPTION:
!racle has an5 tools such as Q = #", !racle 8ors, !racle eport /riter,
!racle Graphics etc.
SQL PLUS> :he Q = #" tool is ade up of two distinct parts. :hese are
• Int!r"cti#! SQL: nteractie Q is designed for create, access and
anipulate data structures li?e tables and inde;es.
• PL/SQL: #Q can be used to deeloped progras for different
applications.
Or"c$! %or&': :his tool allows 5ou to create a data entr5 screen along with
the suitable enu ob4ects. :hus it is the oracle fors tool that handles data
gathering and data alidation in a coercial application.
R!(ort )rit!r: eport writer allows prograers to prepare innoatie reports
using data fro the oracle structures li?e tables, iews etc. t is the report writer
tool that handles the reporting section of coercial application.
Or"c$! Gr"(*ic': oe of the data can be better represented in the for of
pictures. :he oracle graphics tool allows prograers to prepare graphs using
data fro oracle structures li?e tables, iews etc.
SQL +Structur!d Qu!r, L"n-u"-!.:
tructured Quer5 anguage is a database coputer language designed for
anaging data in relational database anageent s5stes(@$), and originall5
based upon elational Algebra. ts scope includes data Buer5 and update,
schea creation and odification, and data access control. Q was one of the first
languages for <dgar 8. Codds relational odel in his influential 1-70 paper, DA
3
![Page 4: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/4.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 4/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
elational odel of @ata for arge hared @ata $an?sDE3F and becae the ost
widel5 used language for relational databases.
• $ deeloped Q in id of 1-70s.
•
!racle incorporated in the 5ear 1-7-.
• Q used b5 $@$2 and @ @atabase 5stes.
• Q adopted as standard language for @$ b5 A in 1-6-.
DATA TYPES:
1C0AR +Si!.: :his data t5pe is used to store character strings alues of fi;ed
length. :he siHe in brac?ets deterines the nuber of characters the cell can
hold. :he a;iu nuber of character is 2 characters.
2 3ARC0AR +Si!. / 3ERC0AR2 +Si!.> :his data t5pe is used to store ariable
length alphanueric data. :he a;iu character can hold is 2000 character.
4NUMBER +P5 S.: :he "$< data t5pe is used to store nuber (fi;ed or
floating point). uber of irtuall5 an5 agnitude a5 be stored up to 36 digits of
precision. uber as large as -.-- = 10 12+. :he precision (p) deterines the
nuber of places to the right of the decial. f scale is oitted then the default is
Hero. f precision is oitted, alues are stored with their original precision up to
the a;iu of 36 digits.
6 DATE: :his data t5pe is used to represent date and tie. :he standard forat is
@@II%% as in 17I<#I200-. :o enter dates other than the standard forat,
use the appropriate functions. @ate tie stores date in the 2+I&ours forat. $5
default the tie in a date field is 12>00>00 a, if no tie portion is specified. :he
default date for a date field is the first da5 the current onth.
7LONG: :his data t5pe is used to store ariable length character strings containing
up to 2G$. ong data can be used to store arra5s of binar5 data in AC forat.
4
![Page 5: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/5.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 5/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
!G alues cannot be inde;ed, and the noral character functions such as
"$: cannot be applied.
8 RA): :he A/ data t5pe is used to store binar5 data, such as digitiHed picture
or iage. @ata loaded into coluns of these data t5pes are stored without an5
further conersion. A/ data t5pe can hae a a;iu length of 2 b5tes.
!G A/ data t5pe can contain up to 2G$.
INTERACTI3E SQL:
Syntax : '<$(#araeterJ1,#araeterJ2,#araeterJ3,........#araeterJn)K
SQL $"n-u"-! i' 'u9di#id!d into '!#!r"$ $"n-u"-! !$!&!nt'5 inc$udin-:
Clauses, which are in soe cases optional, constituent coponents of
stateents and Bueries.
Expressions, which can produce either scalar alues or tables consisting
of coluns and rows of data.
Predicates which specif5 conditions that can be ealuated to Q threeIalued
logic (3') $oolean truth alues and which are used to liit the effects of
stateents and Bueries, or to change progra flow.
Queries which retriee data based on specific criteria.
Statements which a5 hae a persistent effect on scheas and data, or which
a5 control transactions, progra flow, connections, sessions, or diagnostics.
5
![Page 6: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/6.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 6/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
Q stateents also include the seicolon (DKD) stateent terinator. :hough
not reBuired on eer5 platfor, it is defined as a standard part of the Q
graar.
Insignificant white space is generall5 ignored in Q stateents and Bueries,
a?ing it easier to forat Q code for readabilit5.
:here are fie t5pes of Q stateents. :he5 are>
1. @A:A @<8:! AG"AG< (@@)
2. @A:A A#"A:! AG"AG< (@)
3. @A:A <:<'A AG"AG< (@)
+. :AA:!A C!:! AG"AG< (:C)
. @A:A C!:! AG"AG< (@C)
1 DATA DE%INITION LANGUAGE +DDL.: :he @ata @efinition anguage (@@) is
used to create and destro5 databases and database ob4ects. :hese coands will
priaril5 be used b5 database adinistrators during the setup and reoal phases of
a database pro4ect. ets ta?e a loo? at the structure and usage of four basic @@
coands>
1. C<A:< 2. A:< 3. @!# +. <A<
1 CREATE:
+".CREATE TABLE: :his is used to create a new relation and the corresponding
Syntax: C<A:< :A$< relationJnae
(fieldJ1 dataJt5pe(iHe),fieldJ2 dataJt5pe(iHe), .. . )K
Example:
QLC<A:< :A$< tudent (sno "$<(3),snae C&A(10),class C&A())K
6
![Page 7: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/7.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 7/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
+9.CREATE TABLEAS SELECT: :his is used to create the structure of a new
relation fro the structure of an e;isting relation.
Syntax: C<A:< :A$< (relationJnaeJ1, fieldJ1,fieldJ2,.....fieldJn) A
<<C: fieldJ1,fieldJ2,...........fieldJn 8! relationJnaeJ2K
Example: QLC<A:< :A$< std(rno,snae) A <<C: sno,snae 8!
studentK
2 ALTER:
+".ALTER TABLE ADD: :his is used to add soe e;tra fields into e;isting
relation.
Syntax: A:< :A$< relationJnae A@@(new fieldJ1 dataJt5pe(siHe), new fieldJ2
dataJt5pe(siHe),..)K
Example : QLA:< :A$< std A@@(Address C&A(10))K
+9.ALTER TABLEMODI%Y: :his is used to change the width as well as data t5pe
of fields of e;isting relations.
Syntax: A:< :A$< relationJnae !@8% (fieldJ1 newdataJt5pe(iHe), fieldJ2
newdataJt5pe(iHe),....fieldJnewdataJt5pe(iHe))K
Example:QLA:< :A$< student !@8%(snae 'AC&A(10),class
'AC&A())K
4 DROP TABLE: :his is used to delete the structure of a relation. t peranentl5
deletes the records in the table.
Syntax: @!# :A$< relationJnaeK
Example: QL@!# :A$< stdK
6 RENAME: t is used to odif5 the nae of the e;isting database ob4ect.Syntax: <A< :A$< oldJrelationJnae :! newJrelationJnaeK
Example: QL<A< :A$< std :! std1K
7 TRUNCATE: :his coand will reoe the data peranentl5. $ut structure will not
be reoed.
Syntax: :"CA:< :A$< 9:able naeL
7
![Page 8: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/8.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 8/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
Example :"CA:< :A$< studentK
Di;;!r!nc! 9!t<!!n Trunc"t! = D!$!t!:I
$5 using truncate coand data will be reoed peranentl5 M will not get
bac? where as b5 using delete coand data will be reoed teporall5 M get
bac? b5 using roll bac? coand.
$5 using delete coand data will be reoed based on the condition where
as b5 using truncate coand there is no condition.
:runcate is a @@ coand M delete is a @ coand.
2 DATA MANIPULATION LANGUAGE +DML.: :he @ata anipulation anguage
(@) is used to retriee, insert and odif5 database inforation. :hese coands
will be used b5 all database users during the routine operation of the database. ets
ta?e a brief loo? at the basic @ coands>
1 INSERT 2 UPDATE 4 DELETE
1 INSERT INTO: :his is used to add records into a relation. :hese are three t5pe of
<: :! Bueries which are as
". In'!rtin- " 'in-$! r!cord
Syntax: <: :! relationnae(fieldJ1,fieldJ2,.fieldJn)'A"<
(dataJ1,dataJ2,........dataJn)K
Example: QL<: :! student(sno,snae,class,address)'A"<
(1,ai,.:ech,#ala?ol)K
9. In'!rtin- "$$ r!cord' ;ro& "not*!r r!$"tion
Syntax: <: :! relationJnaeJ1 <<C: 8ieldJ1,fieldJ2,fieldJn
8! relationJnaeJ2 /&<< fieldJ;NdataK
Example: QL<: :! std <<C: sno,snae 8! student
/&<< nae N OauOK
c. In'!rtin- &u$ti($! r!cord'
Syntax: <: :! relationJnae fieldJ1,fieldJ2,.....fieldJn) 'A"<
(MdataJ1,MdataJ2,........MdataJn)K
8
![Page 9: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/9.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 9/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
Example: QL<: :! student(sno,snae,class,address)
'A"<(Msno,Msnae,Mclass,Maddress)K
<nter alue for sno> 101<nter alue for nae> ai
<nter alue for class> .:ech<nter alue for nae> #ala?ol
2 UPDATESET)0ERE: :his is used to update the content of a record in a relation.
Syntax: QL"#@A:< relation nae <: 8ieldJnae1Ndata,fieldJnae2Ndata,
/&<< fieldJnaeNdataK
Example: QL"#@A:< student <: snae N O?uar /&<< snoN1K
4 DELETE%ROM> :his is used to delete all the records of a relation but it will retain
the structure of that relation.
". DELETE%ROM> :his is used to delete all the records of relation.
Syntax: QL@<<:< 8! relationJnaeK
Example: QL@<<:< 8! stdK
9. DELETE %ROM)0ERE: :his is used to delete a selected record fro a relation.
Syntax: QL@<<:< 8! relationJnae /&<< conditionK
Example: QL@<<:< 8! student /&<< sno N 2K
4 DRL+DATA RETRIE3AL LANGUAGE.: etriees data fro one or ore tables.
1 SELECT %ROM: :o displa5 all fields for all records.
Syntax : <<C: = 8! relationJnaeK
Example : QL select = fro deptK
@<#:! @A< !C IIIIIIII IIIIIIIIIII IIIIIIIIII 10 ACC!":G </ %!P 20 <<AC& @AA 30 A< C&CAG! +0 !#<A:! $!:!
2 SELECT %ROM: :o displa5 a set of fields for all records of relation.
9
![Page 10: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/10.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 10/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
Syntax: <<C: a set of fields 8! relationJnaeK
Example: QL select deptno, dnae fro deptK
@<#:! @A<IIIIIII IIIIIIIIII
10 ACC!":G 20 <<AC& 30 A<4 SELECT %ROM )0ERE: :his Buer5 is used to displa5 a selected set of fields for
a selected set of records of a relation.
Syntax: <<C: a set of fields 8! relationJnae /&<< conditionK
Example: QL select = 8! dept /&<< deptno9N20K
@<#:! @A< !CIIIIII IIIIIIIIIII IIIIIIIIIIII
10 ACC!":G </ %!P 20 <<AC& @AA
6 SELECT %ROM GROUP BY: :his Buer5 is used to group to all the records in a
relation together for each and eer5 alue of a specific ?e5(s) and then displa5 the
for a selected set of fields the relation.
Syntax: <<C: a set of fields 8! relationJnae G!"# $% fieldJnaeK
Example: QL <<C: <#!, " (AA%) 8! <# G!"# $%
<#!K
<#! " (AA%)IIIIII IIIIIIIIII
1 3000 2 +000 3 000 + *000 + rows selected.
7 SELECT %ROM ORDER BY: :his Buer5 is used to displa5 a selected set of fields
fro a relation in an ordered anner base on soe field.
Syntax: <<C: a set of fields 8! relationJnae
!@< $% fieldJnaeK
Example: QL <<C: epno,enae,4ob 8! ep !@< $% 4obK
<#! <A< !$
10
![Page 11: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/11.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 11/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
IIIIII IIIIIIIII IIIIIIII + A' AAG< 2 araind anager 1 sagar cler? 3 a?i cler?
+rows selected.
8 >OIN u'in- SELECT %ROM ORDER BY: :his Buer5 is used to displa5 a set of
fields fro two relations b5 atching a coon field in the in an ordered anner
based on soe fields.
Syntax: <<C: a set of fields fro both relations 8! relationJ1, relationJ2
/&<< relationJ1.fieldJ; N relationJ2.fieldJ5 !@< $% fieldJHK
Example: QL<<C: epno,enae,4ob,dnae 8! ep,dept
/&<< ep.deptno N 20 !@< $% 4obK
<#! <A< !$ @A<IIIIII IIIIII IIIIIII IIIIIIIIII7766 C!:: AA%: ACC!":G7-02 8!@ AA%: ACC!":GIIIIII7** !< AAG< !#<A:!7** !< AAG< A< 20 rows selected.
? >OIN u'in- SELECT %ROM GROUP BY: :his Buer5 is used to displa5 a set of
fields fro two relations b5 atching a coon field in the and also group the
corresponding records for each and eer5 alue of a specified ?e5(s) while displa5ing.
Syntax: <<C: a set of fields fro both relations 8! relationJ1,relationJ2
/&<< relationJ1.fieldI;NrelationJ2.fieldI5 G!"# $% fieldIHK
Example: QL <<C: epno,"(AA%) 8! ep,dept
/&<< ep.deptno N20 G!"# $% epnoK
<#! " (AA%)IIIIIII IIIIIIII73*- 32007** 11-007766 12000767* ++00
@ UNION: :his Buer5 is used to displa5 the cobined rows of two different Bueries,
which are haing the sae structure, without duplicate rows.
11
![Page 12: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/12.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 12/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
Syntax: <<C: fieldJ1,fieldJ2,....... 8! relationJ1 /&<< (Condition)
"! <<C: fieldJ1,fieldJ2,....... 8! relationJ2 /&<< (Condition)K
Example:
QL <<C: = 8! :"@<:K
! A<IIIII IIIIIII
1 ?uar 2 rai 3 rau
QL <<C: = 8! :@K
! A< IIIII IIIIIII 3 rau lalitha - dei 1 ?uar
QL <<C: = 8! student "! <<C: = 8! stdK! A<IIII IIIIII
1 ?uar
2 rai 3 rau lalitha - dei
INTERSET: :his Buer5 is used to displa5 the coon rows of two different Bueries,
which are haing the sae structure, and to displa5 a selected set of fields out of
the.
Syntax: <<C: fieldJ1,fieldJ2,.. 8! relationJ1 /&<<
(Condition) :<<C: <<C: fieldJ1,fieldJ2,.. 8! relationJ2
/&<<(Condition)K
Example : QL <<C: = 8! student :<<C: <<C: = 8! stdK
! A<IIII IIIIIII
12
![Page 13: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/13.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 13/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
1 Puar
1 MINUS: :his Buer5 is used to displa5 all the rows in relationJ1,which are not
haing in the relationJ2.
Syntax: <<C: fieldJ1,fieldJ2,......8! relationJ1
/&<<(Condition) " <<C: fieldJ1,fieldJ2,.....
8! relationJ2 /&<<(Conditon)K
SQL <<C: = 8! student " <<C: = 8! stdK
! A<IIII IIIIIII
2 A'3 A"
4 TRANSATIONAL CONTROL LANGUAGE +TCL.:
A transaction is a logical unit of wor?. All changes ade to the database can be
referred to as a transaction. :ransaction changes can be ode peranent to the
database onl5 if the5 are coitted a transaction begins with an e;ecutable Q
stateent M ends e;plicitl5 with either role bac? or coit stateent.
1 COMMIT: :his coand is used to end a transaction onl5 with the help of the
coit coand transaction changes can be ade peranent to the database.
Syntax: QLC!:K
Example: QLC!:K
2 SA3E POINT> ae points are li?e ar?s to diide a er5 length5 transaction to
saller once. :he5 are used to identif5 a point in a transaction to which we can latter
role bac?. :hus, sae point is used in con4unction with role bac?.
Syntax: QLA'< #!: @K
13
![Page 14: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/14.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 14/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
Example: QLA'< #!: ;5HK
4 ROLE BAC: A role bac? coand is used to undo the current transactions. /e
can role bac? the entire transaction so that all changes ade b5 Q stateents are
undo (or) role bac? a transaction to a sae point so that the Q stateents after the
sae point are role bac?.
Syntax: !< $ACP( current transaction can be role bac?)
!< $ACP to sae point @K
Example: QL!< $ACPK
QL!< $ACP :! A'< #!: ;5HK
6 DATA CONTROL LANGUAGE +DCL.>
@C proides uses with priilege coands the owner of database ob4ects
(tables), has the soul authorit5 ollas the. :he owner (data base adinistrators) can
allow other data base uses to access the ob4ects as per their reBuireent
1 GRANT: :he GA: coand allows granting arious priileges to other users
and allowing the to perfor operations with in their priileges
For Example, if a uses is granted as O<<C: priilege then heshe can onl5 iew
data but cannot perfor an5 other @ operations on the data base ob4ect
GA:<@ priileges can also be withdrawn b5 the @$A at an5 tie
Syntax: QLGA: #'<G< on ob4ectJnae :o userJnaeK
Example> QLGA: <<C:, "#@A:< on ep :o heanthK
14
![Page 15: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/15.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 15/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
2 RE3OE: :o with draw the priileges that has been GA:<@ to a uses, we use
the <'!P< coand
Syntax: QL<'!P< #'<G< ! ob4ectInae 8! userJnaeK
Example: QL<'!P< <<C:, "#@A:< ! ep 8! raiK
1 Cr!"tion5 "$t!rin- "nd dro((in- o; t"9$!' "nd in'!rtin- ro<' into "
t"9$! +u'! con'tr"int' <*i$! cr!"tin- t"9$!'. !"&($!' u'in-
SELECT co&&"nd
1 CREATE:
+".CREATE TABLE: :his is used to create a new relation
Syntax: C<A:< :A$< relationJnae
(fieldJ1 dataJt5pe(iHe),fieldJ2 dataJt5pe(iHe), .. . )K
Example:
QLC<A:< :A$< tudent (sno "$<(3) PRIMARY EY ,snae
C&A(10),class C&A())K
2 ALTER:
+".ALTER TABLE ADD: :his is used to add soe e;tra fields into e;isting
relation.
Syntax: A:< :A$< relationJnae A@@(new fieldJ1 dataJt5pe(siHe), new fieldJ2
dataJt5pe(siHe),..)K
Example : QLA:< :A$< std A@@(Address C&A(10))K
+9.ALTER TABLEMODI%Y: :his is used to change the width as well as data t5pe
of fields of e;isting relations.
Syntax: A:< :A$< relationJnae !@8% (fieldJ1 newdataJt5pe(iHe), fieldJ2
newdataJt5pe(iHe),....fieldJnewdataJt5pe(iHe))K
Example: QLA:< :A$< student !@8%(snae 'AC&A(10),class
'AC&A())K
4 DROP TABLE: :his is used to delete the structure of a relation. t peranentl5
deletes the records in the table.
Syntax: @!# :A$< relationJnaeK
15
![Page 16: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/16.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 16/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
Example: QL@!# :A$< stdK
6 INSERT:
Syntax: <: :! relationJnae fieldJ1,fieldJ2,.....fieldJn) 'A"<
(MdataJ1,MdataJ2,........MdataJn)K
Example: QL<: :! student(sno,snae,class,address)
'A"<(Msno,Msnae,Mclass,Maddress)K
<nter alue for sno> 101<nter alue for nae> &A<nter alue for class> C<<nter alue for address> #ala?ol
7 SELECT %ROM: :o displa5 all fields for all records.Syntax : <<C: = 8! relationJnaeK
Example : QL select = fro studentK
! A< CA A@@< IIII IIIIIIII IIIIII IIIIIII 101 &A C< #AAP! 102 @<'AP C< AA#" 103 P"A CA@ $&A'AA 10+ A' ' #AAP!
2 SELECT %ROM: :o displa5 a set of fields for all records of relation.
Syntax: <<C: a set of fields 8! relationJnaeK
Example: QL select sno, snae fro studentK
! A< IIII IIIIIIII
101 &A102 @<'AP103 P"A10+ A'
4 SELECT %ROM )0ERE: :his Buer5 is used to displa5 a selected set of fields for
a selected set of records of a relation.
Syntax: <<C: a set of fields 8! relationJnae /&<< conditionK
Example: QL select = 8! student /&<< classNC<K
! A< CA A@@<
16
![Page 17: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/17.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 17/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
IIII IIIIIIII IIIIII IIIIIII 101 &A C< #AAP! 102 @<'AP C< AA#"
T*!r! "r! 7 con'tr"int' "#"i$"9$! in ORACLE:
1 NOT NULL: /hen a colun is defined as !:", then that colun becoes a
andator5 colun. t iplies that a alue ust be entered into the colun if the
record is to be accepted for storage in the table.
Syntax:
CREATE TABLE :ableJae(colunJnae dataJt5pe(size) NOT NULL5 )KExample:
CREATE TABLE student (sno NUMBER+4.NOT NULL5 nae C0AR(1))K
2 UNIQUE: :he purpose of a uniBue ?e5 is to ensure that inforation in the
colun(s) is uniBue i.e. a alue entered in colun(s) defined in the uniBue constraint
ust not be repeated across the colun(s). A table a5 hae an5 uniBue ?e5s.
Syntax:
CREATE TABLE :ableJae(colunJnae dataJt5pe(size) UNIQUE5 F)K
Example:
CREATE TABLE student (sno NUMBER+4. UNIQUE5 nae C0AR(1))K
4 C0EC: pecifies a condition that each row in the table ust satisf5. :o satisf5 the
constraint, each row in the table ust a?e the condition either :"< or un?nown
(due to a null).
Syntax:
CREATE TABLE :ableJae(colunJnae dataJt5pe(size) C0EC+logical
expression.5 F)K
Example: CREATE TABLE student (sno NUMBER +4.5 nae C0AR(1),class
C0AR+7.5C0EC(class IN(OC<,CA@,'))K
17
![Page 18: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/18.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 18/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
6 PRIMARY EY: A field which is used to identif5 a record uniBuel5. A colun or
cobination of coluns can be created as priar5 ?e5, which can be used as a
reference fro other tables. A table contains priar5 ?e5 is ?nown as aster :able.
t ust uniBuel5 identif5 each record in a table.
t ust contain uniBue alues.
t cannot be a null field.
t cannot be ulti port field.
t should contain a iniu no. of fields necessar5 to be called uniBue.
Syntax:
CREATE TABLE :ableJae(colunJnae dataJt5pe(size) PRIMARY EY5 F)K
Example:CREATE TABLE facult5 (fcode NUMBER+4. PRIMARY EY5 fnae
C0AR(1))K
7 %OREIGN EY: t is a table leel constraint. /e cannot add this at colun leel. :o
reference an5 priar5 ?e5 colun fro other table this constraint can be used. :he
table in which the foreign ?e5 is defined is called a d!t"i$ t"9$!. :he table that defines
the priar5 ?e5 and is referenced b5 the foreign ?e5 is called the &"'t!r t"9$!.Syntax: CREATE TABLE :ableJae(colunJnae dataJt5pe(size)
%OREIGN EY(colunJnae) RE%ERENCES tableJnae)K
Example:
CREATE TABLE sub4ect (scode NUMBER +4. PRIMARY EY5
subnae C0AR(1),fcode NUMBER+4.5
%OREIGN EY(fcode) RE%ERENCE facult5 )K
D!;inin- int!-rit, con'tr"int' in t*! "$t!r t"9$! co&&"nd:
Syntax: ALTER TABLE :ableJae ADD PRIMARY EY (colunJnae)K
Example: ALTER TABLE student ADD PRIMARY EY (sno)K
(!r)
18
![Page 19: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/19.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 19/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
Syntax: ALTER TABLE tableJnae ADD CONSTRAINT constraintJnae
PRIMARY EY(colnae)
Example: ALTER TABLE student ADD CONSTRAINT PRIMARY EY+!.
Dro((in- int!-rit, con'tr"int' in t*! "$t!r t"9$! co&&"nd:
Syntax: ALTER TABLE :ableJae DROP constraintJnaeK
Example: ALTER TABLE student DROP PRIMARY EYK
(or)
Syntax: ALTER TABLE student DROP CONSTRAINT constraintJnae
Example: ALTER TABLE student DROP CONSTRAINT
19
![Page 20: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/20.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 20/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
EXP NO: 2 Qu!ri!' +"$on- <it* 'u9 Qu!ri!'.
AIM: :o e;ecute Bueries along with sub Bueries b5 using A%, A, ,
<R:, !:<R:, "!, <: and Constraints.
Selecting data from sailors table
QL select = fro sailorsK
@ A< AG< A:GIIIIIIIII IIIIIIIIII IIIIIIIII IIIIIIIII 22 dustin 7 +
2- brutus 1 33 31 lubber 6 32 and5 6 2. 6 rust5 10 3 *+ horatio 7 3 71 Horba 10 +0 7+ horatio - +0 6 art 3 2. - bob 3 *3.
10 rows selected.
Selecting data from reserves table
QL select = fro reseresK
@ $@ @A%IIIIIIIII IIIIIIIII IIIIII
22 101 10I!C:I-6 22 102 10I!C:I-6 22 103 10IA"GI-6
22 10+ 10I"I-6 31 102 11I!C:I-6 31 103 11I"I-6 31 10+ 11I@<CI-6 *+ 101 0-IA%I-6 *+ 102 0-IA"GI-6 7+ 10+ 0-IA"GI-610 rows selected.
20
![Page 21: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/21.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 21/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
Selecting data from boat table
QL select = fro boatsK
$@ $A< C!!IIIIIIIII IIIIIIIIIIIIIIIIIIII IIIIIIIIII 101 interla?e blue 102 interla?e red 103 clipper green 10+ arine red
Q: find the names of sailors who have reserved boat 103.
QL select s.snae fro sailors s where s.sid in (select r.sid
fro reseres r where r.bidN103)K
A<IIIIIIIIIIIIIIdustinlubber
2 rows selected.
Q: find the names of sailors who have reserved a red boat.
QL select s.snae fro sailors s where s.sid in(select r.sid fro reseres r where r.bid in (select b.bid
fro boats b where b.colorNred))KA<IIIIIIIIIIIIIIIIIIII
dustinlubber horatiohoratio
+ rows selected.
Q: Find the name and age of the oldest sailors.
21
![Page 22: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/22.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 22/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
QL select AR(s.age)fro sailors sK
AR(.AG<)IIIIIIIIII 10
Q: o!nt the n!mber of sailors.
QL select C!":(s.age)fro sailors s
C!":(.AG<)IIIIIIIIIIII 10
Q: o!nt the n!mber of different sailors names.
QL select C!":(distinct s.snae)= fro sailors s
C!":(@:C:.A<)IIIIIIIIIIIIIIIIIIIIII -Q: find the names of sailors who have not reserved a red boat.
QL select s.snae 2 fro sailors s 3 where s.sid not in (select r.sid + fro reseres r where r.bid in (select b.bid * fro boats b 7 where b.colorNred))
A<IIIIIIIIbrutusand5
rust5Horbaartbob
* rows selected.
22
![Page 23: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/23.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 23/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
Q: find the names of sailors who have not reserved boat 103.
QL select s.snae fro sailors s where e;ists(select = froreseres r where r.bidN103 and r.sidNs.sid)K
A<IIIIIIIdustinlubber
2 rows selected.
Q: find the names of sailors who have reserved at least one boat.
QL select s.snae fro sailors s, reseres r wheres.sidNr.sidK
A<IIIIIIIIIIdustindustindustin
dustinlubber lubber lubber horatiohoratiohoratio
10 rows selected.Q: omp!te increments for the ratings of persons who have sailed two
different boats on the same day.
QL select s.snae,s.ratingS1 As rating fro sailors s,reseres r1, reseres r2 where s.sidNr1.sid A@ s.sidNr2.sid
A@ r1.da5Nr2.da5 A@ r1.bid9Lr2.bid
A< A:GIIIIIIIIIIIIIIIIIIII IIIIIIIII
23
![Page 24: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/24.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 24/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
dustin +*dustin +*
Q: Find the names of sailors who have reserved a red or a green boat.
QL select s.snae fro sailors s, reseres r,boats b where s.sidNr.sid [email protected] A@ (b.colorNred !
b.colorNgreen)
A<IIIIIIIIIIIIIIIIIIIIdustindustindustinlubber lubber lubber horatiohoratio
6 rows selected.
Q: find the all sids of sailors who have rating 10 or have reserved boat
10"..
QL select s.sid fro sailors s where s.ratingN10 union
select r.sid fro reseres r where r.bidN10+K
@IIIIII
22 31 7+
Q: Find the n!mber of reservations for each red boat.QL select b.bid,count(=)As sailorcount fro boats b,
reseres r where r.bidNb.bid A@ b.colorNred group b5 b.bidK$@ A!C!":IIIIIIIII IIIIIIIIIII 102 3 10+ 3
Q: Find the minim!m age of the sailor.
QL select in(s.age) fro sailors sK
24
![Page 25: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/25.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 25/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
(.AG<)IIIIIIIIII 1
Q: Find the s!m of the rating of sailors.
QL select su(s.rating)fro sailors sK
"(.A:G)IIIIIIIIIIIII 3-7.
Q: find the id and names of sailors who have reserved id#$$ or age%$&.
QL select sid,snae fro sailors where sidN22 or age92
@ A< II IIIIIIII 22 dustin
25
![Page 26: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/26.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 26/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
EXP NO: 4
AIM: :o e;ecute a Queries using Aggregate functions (C!":, ",
A'G, AR and ), G!"# $%, &A'G and Creation and dropping
of 'iews.
PROCEDURE:
A--r!-"ti#! o(!r"tor': n addition to sipl5 retrieing data, we often want to
perfor soe coputation or suariHation. Q allows the use of arithetic
e;pressions. /e now consider a powerful class of constructs for coputing aggregate
alues such as and ".
1 Count: C!": following b5 a colun nae returns the count of tuple in that
colun. f @:C: ?e5word is used then it will return onl5 the count of uniBue tuple
in the colun. !therwise, it will return count of all the tuples (including duplicates)
count (=) indicates all the tuples of the colun.
Syntax: C!": (Colun nae)
Example: <<C: C!": (al) 8! epK
2 SUM: " followed b5 a colun nae returns the su of all the alues in that
colun.
Syntax: " (Colun nae)
Example: <<C: " (al) 8ro epK
4 A3G: A'G followed b5 a colun nae returns the aerage alue of that colun
alues.
Syntax: A'G (n1,n2..)
Example: elect A'G(10, 1, 30) 8! @"AK
6 MAX: AR followed b5 a colun nae returns the a;iu alue of that colun.
Syntax: AR (Colun nae)
Example: <<C: AR (al) 8! epK
26
![Page 27: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/27.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 27/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
QL select deptno,a;(sal) fro ep group b5 deptnoK
@<#:! AR(A)IIIIII IIIIIIII10 00020 300030 260
QL select deptno,a;(sal) fro ep group b5 deptno hainga;(sal)93000K
@<#:! AR(A) IIIII IIIIIIII 30 260
7 MIN: followed b5 colun nae returns the iniu alue of that colun.
Syntax: (Colun nae)
Example: <<C: (al) 8! epK
QLselect deptno,in(sal) fro ep group b5 deptno haing in(sal)L1000K
@<#:! (A) IIIII IIIIIIII
10 1300
3IE): n Q, a iew is a irtual table based on the resultIset of an Q stateent.
A iew contains rows and coluns, 4ust li?e a real table. :he fields in a iew are
fields fro one or ore real tables in the database.
%ou can add Q functions, /&<<, and ! stateents to a iew and
present the data as if the data were coing fro one single table.
A iew is a irtual table, which consists of a set of coluns fro one or ore
tables. t is siilar to a table but it doest not store in the database. 'iew is a Buer5
stored as an ob4ect.
Syntax: C<A:< '</ iewJnae A <<C: set of fields 8!
relationJnae /&<< (Condition)
1. Example:
QLC<A:< '</ eplo5ee A <<C: epno,enae,4ob 8! <#
27
![Page 28: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/28.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 28/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
/&<< 4ob N Ocler?K
'iew created.
QL <<C: = 8! <#!%<<K
<#! <A< !$IIII IIIIII IIIIIII73*- :& C<P767* A@A C<P7-00 A< C<P7-3+ < C<P
$.Example:
C<A:< '</ ECurrent #roduct istF A<<C: #roduct@,#roductae
8! #roducts
/&<< @iscontinuedNo
DROP 3IE)> :his Buer5 is used to delete a iew , which has been alread5 created.
Syntax: @!# '</ 'iewJnaeK
Example : QL @!# '</ <#!%<<K
'iew dropped
28
![Page 29: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/29.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 29/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
EXP NO: 6
AIM: :o e;ecute Queries using Conersion functions (toJchar, toJnuber
and toJdate), string functions (Concatenation, lpad, rpad, ltri, rtri,
lower, upper, initcap, length, substr and instr), date functions (5sdate,
ne;tJda5, addJonths, lastJda5, onthsJbetween, least, greatest, trunc,
round, toJchar, toJdate)
PROCEDURE:
1 Con#!r'ion ;unction':
ToHc*"r: :!JC&A (nuber) conerts n to a alue of 'AC&A2 data t5pe, using
the optional nuber forat ft. :he alue n can be of
t5pe "$<, $A%J8!A:, or $A%J@!"$<.
QLselect toJchar(*,)fro dualK
R'
ToHnu&9!r : :!J"$< conerts e;pr to a alue of "$< data t5pe.
QL elect toJnuber(123+.*+) fro @ualK
123+.*+
ToHd"t!: :!J@A:< conerts char of C&A, 'AC&A2, C&A,
or 'AC&A2 data t5pe to a alue of @A:< data t5pe.
QL<<C: :!J@A:<(anuar5 1, 1-6-, 11>00 A..)8! @"AK
:!J@A:<(IIIIIIIII1IAI6-
2 Strin- ;unction':
Conc"t: C!CA: returns char1 concatenated with char2. $oth char1 and char2 can
be an5 of the datat5pes
QL<<C: C!CA:(O!AC<,C!#!A:!)8! @"AK
!AC<C!#!A:!
29
![Page 30: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/30.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 30/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
L("d: #A@ returns e;pr1, leftIpadded to length n characters with the seBuence of
characters in e;pr2.
QL<<C: #A@(O!AC<,1,=)8! @"AK
=========!AC<
R("d: #A@ returns e;pr1, rightIpadded to length n characters with e;pr2, replicated
as an5 ties as necessar5.
QL<<C: #A@ (O!AC<,1,=)8! @"AK!AC<=========
Ltri&: eturns a character e;pression after reoing leading blan?s.
QL<<C: :(O:&,)8! @"AK
:&
Rtri&: eturns a character string after truncating all trailing blan?s
QL<<C: :(O:&,)8! @"AK:&
Lo<!r: eturns a character e;pression after conerting uppercase character data to
lowercase.
QL<<C: !/<(O@$)8! @"AKdbs
U((!r: eturns a character e;pression with lowercase character data conerted to
uppercase
QL<<C: "##<(Odbs)8! @"AK@$
L!n-t*: eturns the nuber of characters, rather than the nuber of b5tes, of the
gien string e;pression, e;cluding trailing blan?s.
QL<<C: <G:&(O@A:A$A<)8! @"AK6
Su9'tr: eturns part of a character, binar5, te;t, or iage e;pression.
QL<<C: "$:(OA$C@<8G&3,+)8! @"AKC@<8
30
![Page 31: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/31.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 31/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
In'tr: :he : functions search string for substring. :he function returns an
integer indicating the position of the character in string that is the first character of this
occurrence.
QL<<C: :(C!#!A:< 8!!,!,3,2)8! @"AK1+
4 D"t! ;unction':
S,'d"t!:
QL<<C: %@A:< 8! @"AK
2-I@<CI06
n!tHd",:
QL<<C: <R:J@A%(%@A:<,/<@)8! @"AK
0IAI0-
"ddH&ont*':
QL<<C: A@@J!:&(%@A:<,2)8! @"AK
26I8<$I0-
$"'tHd",:
QL<<C: A:J@A%(%@A:<)8! @"AK
31I@<CI06
&ont*'H9!t<!!n:
QL<<C: !:&J$<:/<<(%@A:<,&<@A:<)8! <#K
+
L!"'t:
QL<<C: <A:(10IAI07,12I!C:I07)8! @"AK
10IAI07
Gr!"t!'t:
QL<<C: G<A:<:(10IAI07,12I!C:I07)8! @"AK
10IAI07
31
![Page 32: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/32.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 32/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
Trunc:
QL<<C: :"C(%@A:<,@A%)8! @"AK
26I@<CI06
Round:QL<<C: !"@(%@A:<,@A%)8! @"AK
26I@<CI06
toHc*"r:
QL select toJchar(s5sdate, DddTT55D) fro dualK2+IarI0.
toHd"t!:
QL select toJdate(s5sdate, DddTT55D) fro dualK
2+IarIo.
32
![Page 33: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/33.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 33/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
PL/SQL
33
![Page 34: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/34.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 34/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
EXP NO: 7 PL/SQL Introduction
AIM: (i):o write a #Q progra declaration selection, e;ecutable
selection and <;ception &andling.
ii) :o insert data into student table and use C!:, !$ACP
and A'<#!: using #Q bloc?.
DESCRIPTION:
#Q stands for #rocedural anguage e;tension of Q. #Q is a
cobination of Q along with the procedural features of prograing languages. t
was deeloped b5 !racle Corporation in the earl5 -0s to enhance the capabilities of
Q.
!racle uses a #Q engine to processes the #Q stateents. A #Q
code can be stored in the client s5ste (clientIside) or in the database (sererIside).
Ad#"nt"-!' o; PL/SQL:
• 'loc( Str!ct!res: # Q consists of bloc?s of code, which can be nested
within each other. <ach bloc? fors a unit of a tas? or a logical odule.
#Q $loc?s can be stored in the database and reused.
• )roced!ral *ang!age apability : # Q consists of procedural language
constructs such as conditional stateents (if else stateents) and loops li?e
(8! loops).
• 'etter )erformance: # Q engine processes ultiple Q stateents
siultaneousl5 as a single bloc?, thereb5 reducing networ? traffic.
• Error +andling : #Q handles errors or e;ceptions effectiel5 during the
e;ecution of a #Q progra. !nce an e;ception is caught, specific actions
can be ta?en depending upon the t5pe of the e;ception or it can be displa5ed to
the user with a essage.
34
![Page 35: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/35.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 35/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
S,nt" o; PL/SQL (ro-r"&:
@eclare
'ariable declarationK
$egin
<;ecutable stateentsK
endK
Condition"$ St"t!&!nt' in PL/SQL
As the nae iplies, #Q supports prograing language features li?e
conditional stateents, iteratie stateents.
:he prograing constructs are siilar to how 5ou use in prograing
languages li?e aa and CSS. n this section will proide 5ou s5nta; of how to use
conditional stateents in #Q prograing.
I% T0EN ELSE STATEMENT:
1) 8 condition :&<
stateent 1K
<<
stateent 2K
<@ 8K
2) 8 condition 1 :&<
stateent 1K
stateent 2K
<8 condtion2 :&<
stateent 3K
35
![Page 36: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/36.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 36/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
<<
stateent +K
<@ 8
PROCEDURE:
7 )rit! " PL/SQL (ro-r"& to ;ind t*! tot"$ "nd "#!r"-! o; 6 'u9!ct' "nd
di'($", t*! -r"d!
declare
4aa nuber(10)K
dbs nuber(10)K
co nuber(10)K
fcs nuber(10)K
total nuber(10)K
ags nuber(10)K
per nuber(10)K
begin
dbsJoutput.putJline(<:< :&< AP)K
4aa>NM4aaK
dbs>NMdbsK
co>NMcoK
fcs>NMfcslK
total>N(4aaSdbsScoSfcs)K
per>N(total*00)=100K
if 4aa9+0 or dbs9+0 or co9+0 or fcs9+0 then
dbsJoutput.putJline(8A)K
if perL7 then
dbsJoutput.putJline(GA@< A)K
elsif perL* and per97 then
36
![Page 37: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/37.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 37/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
dbsJoutput.putJline(GA@< $)K
elsif perL and per9* then
dbsJoutput.putJline(GA@< C)K
else
dbsJoutput.putJline('A@ #":)K
end ifK
dbsJoutput.putJline(#<C<:AG< UUper)K
endK
OUTPUT:
QL V GA@<.sBl
<nter alue for 4aa> 60
old 11> 4aa>NM4aaK
new 11> 4aa>N60K
<nter alue for dbs> 70
old 12> dbs>NMdbsK
new 12> dbs>N70K
<nter alue for co> 6-
old 13> co>NMcoK
new 13> co>N6-K
<nter alue for fcs> 71
old 1+> fcs>NMfcsK
new 1+> fcs>N71K
GA@< A
#<C<:AG< 77
#Q procedure successfull5 copleted.
37
![Page 38: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/38.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 38/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
EXP NO: 8 NESTED I%5 NULL I% STATEMENTS
AIM: :! /rite a #Q progra to find the largest of three nubers
PROCEDURE: declare
a nuberK
b nuberK
c nuberK
begin
a>NMaK
b>NMbKc>NMcK
if aNb and bNc and cNa then
dbsJoutput.putJline(A A< <Q"A)K
elsif aLb and aLc then
dbsJoutput.putJline(A G<A:<)K
elsif bLc then
dbsJoutput.putJline($ G<A:<)K
else
dbsJoutput.putJline(C G<A:<)K
end ifK
endK
OUTPUT:
QL V G<A:<:!83.sBl
<nter alue for a> 6old *> a>NMaKnew *> a>N6K<nter alue for b> -old 7> b>NMbKnew 7> b>N-K
38
![Page 39: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/39.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 39/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
<nter alue for c> 7old 6> c>NMcKnew 6> c>N7K
$ G<A:<
#Q procedure successfull5 copleted.
39
![Page 40: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/40.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 40/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
EXP NO: ? Loo(' in PL/SQL
AIM: /rite a #Q progra b5 using /&< !!# and 8!
!!#.
DESCRIPTION:
:here are three t5pes of loops in #Q>
1. iple oop
2. /hile oop
3. 8or oop
1 Si&($! Loo(: A iple oop is used when a set of stateents is to be e;ecuted at
least once before the loop terinates. An <R: condition ust be specified in the loop,
otherwise the loop will get into an infinite nuber of iterations. /hen the <R:
condition is satisfied the process e;its fro the loop.
Syntax:
!!#
stateentsK
<R:K
Wor <R: /&< condition KX
<@ !!#K
2 )*i$! Loo(: A /&< !!# is used when a set of stateents has to be e;ecuted
as long as a condition is true. :he condition is ealuated at the beginning of each
iteration. :he iteration continues until the condition becoes false.
Syntax:
WHILE condition!
L""P statements#E$% L""P#
4 %OR Loo(: A 8! !!# is used to e;ecute a set of stateents for a
predeterined nuber of ties. teration occurs between the start and end integer
40
![Page 41: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/41.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 41/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
alues gien. :he counter is alwa5s increented b5 1. :he loop e;its when the
counter reaches the alue of the end integer.
Syntax:
&"' counter I$ (al)**(al+
L""P statements#
E$% L""P#
PROGRAM:
? )rit! " PL/ SQL (ro-r"& to -!n!r"t! %i9on"cci '!ri!'
declare
a nuberK
b nuberKc nuberK
n nuberK
i nuberK
begin
n>NMnK
a>N0K
b>N1K
dbsJoutput.putJline(a)K
dbsJoutput.putJline(b)K
for i in 1..nI2
loop
c>NaSbK
dbsJoutput.putJline(c)K
a>NbK
b>NcK
end loopK
endK
41
![Page 42: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/42.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 42/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
OUTPUT:
QL V 8$!ACC.sBl
<nter alue for n> 3
old 6> n>NMnK
new 6> n>N3K
0
1
1
#Q procedure successfull5 copleted.
42
![Page 43: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/43.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 43/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
EXP NO: @ CREATING PROCEDURES
AIM: :o /rite a #Q #rogra to displa5 the nuber in eerse !rder.
PROGRAM:
declare
a nuberK
re nuberK
d nuberK
begin
a>NMaK
re>N0K
while aL0
loop
d>Nod(a,10)K
re>N(re=10)SdK
a>Ntrunc(a10)K
end loopK
dbsJoutput.putJline(no isUU re)K
endK
43
![Page 44: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/44.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 44/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
OUTPUT:
QL V <'<<2.sBl
<nter alue for a> 3*
old *> a>NMaK
new *> a>N3*K
no is *3
#Q procedure successfull5 copleted.
44
![Page 45: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/45.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 45/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
E( no: %UNCTIONS
AIM: :o write a #Q progra to a funtion for finding the factorial of gien nuber.
PROGRAM:
create a funtion fact (n in nuber)
return nuber is c nuber(3)K
$<G
if nN0 or nN1 then
c>N1K
else
c>Nn=fact(nI1)K
end ifK
return(c)
endK
!":#":>
funtion is created.
45
![Page 46: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/46.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 46/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
Ai&: write a #Q bloc? to appl5 string functions on a gien input string.
PROGRAM:
@<CA<
A archar2(20)K
nuber()K
a>NMaK
>Nlength(a)K
dbsJ<":.putJline(O"sing ower 8unction>UUlower(a))K
dbsJ<":.putJline(O"sing "##< 8unction>UUupper(a))K
<@K
OUTPUT:
QLV:
<nter alue for a> santosh redd5
old > aNMaK
new > a>Nsantosh redd5K
"sing ower 8unction>santosh redd5
"sing "pper 8unction>A:!&<@@%
#Q procedure successfull5 copleted
..
46
![Page 47: Lab Manual 20](https://reader031.vdocuments.site/reader031/viewer/2022021321/577cc3621a28aba71195e372/html5/thumbnails/47.jpg)
8/10/2019 Lab Manual 20
http://slidepdf.com/reader/full/lab-manual-20 47/47
(DBMS LAB MANUAL FOR II-IISEM CSE)
EXP NO:1 :GG<
AIM : /rite a trigger on sailors table that runs for eer5 insert stateent that should
hae age alue 916.
T0EORY:
A trigger is a procedure that is autoaticall5 ino?ed b5 the @$ isresponse to specified changes to the databaseand t5picall5 specified b5 @$A. A trigger description contain 3 parts.
• <ent
• Action
• Condition
E#!nt : A changes to the database that actiates the trigger.
Condition : A Buer5 or test that is run when the trigger is actiated.
Action : A procedure is actiated M its condition true.
S,nt" : create a trigger trigger nae before insert update on table nae.
create a trigger trigger nae after insert update on table nae.
PROGRAM :
create trigger :1 before insert on sailors for each rowwhen (new.age916)begin
raiseIapplicationIerror(I2006," cant insert the record)KendK
OUTPUT :trigger createdSQLselect = fro sailorsSQLinsert into sailors alues(eeani,26,argo)K