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 principles and advanced topics in RDBMS, offering a structured appr...
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 principles and advanced topics in RDBMS, offering a structured approach to understanding databases in the context of modern computing. PDF content is prepared from the text book Learn Oracle 8I by JOSE A RAMALHO.
Key Topics Covered:
Main Topic : PL/SQL
Sub-Topic :
Structure of PL/SQL Block, Declaration Section, Variable, Constant, Execution Section, Exception, How PL/SQL works, Control Structures, If then Command,
Loop Command, Loop with IF, Loop with When, For Loop Command, While Command, Integrating SQL in PL/SQL program.
Target Audience:
Final year B.Sc. Computer Science students at Alagappa University seeking a solid foundation in RDBMS principles for academic and practical applications.
About the Author:
Dr. S. Murugan is Associate Professor at Alagappa Government Arts College, Karaikudi. With 23 years of teaching experience in the field of Computer Science, Dr. S. Murugan has a passion for simplifying complex concepts in database management.
Disclaimer:
This document is intended for educational purposes only. The content presented here reflects the author’s understanding in the field of RDBMS as of 2024.
Size: 605.81 KB
Language: en
Added: Jul 21, 2024
Slides: 32 pages
Slide Content
RDBMS -Unit V
Chapter 15
PL/SQL
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
PL/SQL
➢PL/SQLisanextensionoftheSQLlanguage.
➢Itisaprocedurallanguage.
➢PL/SQL combines the SQL language’s ease of data
manipulation and the procedural language’s ease of
programming.
➢With the PL/SQL language, we can create schema
objects, including Stored procedures and functions,
Packages, Triggers, Cursors.
PL/SQL BLOCK -Declaration Section
Inthedeclarationsection,thedevelopercanperformthe
following:
➢Declare the name of an identifier -PI.
➢Declare the type of identifier (constantor variable).
➢Declare the data type of the identifier. -REAL
➢Assign (initialize) contents to the identifier. –3.14
Ex:PI CONSTANT REAL := 3.14
How PL/SQL Works
➢PL/SQLisanenginethatmakesuppartoftheOracle
server.
➢Itexecutestheproceduralcommandsandpassesthe
SQLcommandsfortheOracleservertoprocess.
➢PL/SQLblockscanbecreatedwithanyofthe
processingeditors.(Ex:WindowsNotepad)
➢ToexecuteaPL/SQLprogramorscript,youcanuse
SQL*Plus,whichallowscreating,storing,and
executingPL/SQLblocks.
How PL/SQL Works
Example Program
➢Step1:Typetheprogramineditorandexecuteusing
@symbol.
➢SQL>setserveroutputon;
Example Program for variable and constant
Control Structures
➢PL/SQLhassomecommandstocontroltheexecution
flowoftheprogram.Theyareresponsiblefordecision
makinginsidetheapplication.
➢controlstructuresinPL/SQL,whichcanbedivided
intoconditioncontrolstructures(selection),sequence
structures,andrepetitionoriterationstructures.
➢Differenttypesofstructureexplainedinthefigure
15.3.
Control Structures
The IF..THEN Command
➢TheIF..THENcommandevaluatesaconditionand
executesoneormorerowsofcommandsonlyifthe
analyzedconditionistrue.
➢Ithastwovariationsasfollows:
The IF..THEN Command
➢Insyntax1,thecommandsthatappearbetweenthe
THENandENDIFclauseswillbeexecutedonlyif
theconditionistrue.
Example:
declare
salnumber(7,2);
begin
select salary into salfrom emp2 where eno=101;
IF SAL>= 5000 THEN
UPDATE EMP2 SET salary = salary*1.2;
END IF;
dbms_output.put_line(sal);
end;
/
The IF..THEN Command
In syntax2, more than one condition can be analyzed
and, therefore, several actions can be executed:
1. create table emp2(enonumber(3), salary number(7,2))
2. insert into emp2 values (102, 1500)
3. Type the following program and execute:
declare
salnumber(7,2);
begin
select salary into salfrom emp2 where eno=101;
IF sal<2000 THEN
UPDATE EMP2 SET salary = salary * 1.2;
ELSIF sal<3000 THEN
UPDATE EMP2 SET salary = salary * 1.3;
ELSE
UPDATE EMP2 SET salary = salary * 1.4;
END IF;
dbms_output.put_line(sal);
end;
The LOOP Command
➢TheLOOPcommandinitializesagroupofcommands
indefinitely,oruntilaconditionforcesa“break”inthe
loopandtheexecutionoftheprogramtoanother
place.
➢TheEXITcommandisresponsibleforinterruptingthe
loopexecution:
The LOOP Command -Example
LOOPwith IF Loop with WHEN
declare
I number(3);
begin
I := 1;
Loop
I := I+1;
DBMS_OUTPUT.PUT_LINE(I);
IF I>= 30 THEN
EXIT;
END IF;
END LOOP;
END;
/
declare
I number(3);
begin
I := 1;
Loop
I := I+1;
DBMS_OUTPUT.PUT_LINE(I);
EXIT WHEN I>= 30;
END LOOP;
END;
/
➢Thefollowinglooprepeateduntilthecountervariable
Ibecome30.
The FOR..LOOP Command
➢TheFOR..LOOPcommandisavariationoftheLOOP
command.
➢Here,thecommandsareautomaticallyexecuteduntil
anevaluatedconditionreturnsfalse.
Syntax:
FOR <counter> IN [REVERSE] <initial_value> . . <final_value>
LOOP
<commands>
END LOOP;
The FOR LOOPCommand -Example
Example:
declare
j number(3);
begin
FOR j IN 1..10
LOOP
DBMS_OUTPUT.PUT_LINE(j);
END LOOP;
END;
/
➢TheFORcommandinitializesacontrolvariablecalledJ,which
hastheinitialvalueof1.
➢ThecommandswillbeexecuteduntilENDLOOPisfound.
➢Then,thecontrolreturnstotheFORcommand,which
incrementsthevariableandanalyzesthemastercondition,i.e.,
whetherthevalueofJislessthanthefinalvalue.
➢Whenthishappensthecycleisreinitialized.
The FOR LOOPCommand -Example
➢TheREVERSEclausemakesthecounterstartwiththe
highestvalueanddecreaseuntilitreachesthelowest
value.
declare
j number(3);
begin
FOR j IN reverse 1..10
LOOP
DBMS_OUTPUT.PUT_LINE(j);
END LOOP;
END;
/
The WHILE Command
➢TheWHILEcommandisanothercontrolstructure.
Thisstructureonlyexecutesthecommandsifthe
analyzedconditionistrue.
declare
x number(3);
begin
x:=1;
while x<=20 loop
DBMS_OUTPUT.PUT_LINE(x);
x:=x+1;
END LOOP;
END;
/
Integrating SQL in a PL/SQL Program
➢SQLcommandscanbeinsertedintheexecution
sectionofaPL/SQLblock.
➢Thedevelopercanuse(reference)avariableor
constantdeclaredinthedeclarationsection.
➢InsideaPL/SQLprogramtheSELECTcommanduses
anewclausecalledINTOthatallowstransferringthe
contentsofthefieldsinarowtothevariablesin
memory.
➢Command:SETSERVEROUTPUT ON,GET
(Retreivethefile),RUN(@Executetheprogram)
Integrating SQL in a PL/SQL Program
Declare
Var_namenumber(7,2);
Begin
SELECTsalaryINTOVar_nameFROMemp2whereeno
=101;
dbms_output.put_line('salary='||Var_name);
End;
/
➢Notethattoattributecontentstoafieldinavariable,
theSELECTcommandmustreturnonlyonerow;
➢otherwiseanerrorwilloccur,sinceseveralvaluesare
beingassignedtoonlyonevariable.