Lecture Notes Unit5 chapter18 Packages.pdf

Murugan146644 121 views 14 slides Aug 07, 2024
Slide 1
Slide 1 of 14
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

About This Presentation

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...


Slide Content

RDBMS -Unit V
Chapter 18
Packages
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

Package -Definition
➢Apackageisacollectionofdatabaseobjects,suchas
storedprocedures,functions,variables,constants,and
cursors.
➢Apackagemaycontainsubprogramsthatcanbecalled
fromatrigger,procedure,orfunction.
➢Apackagealsoimprovesmachineperformance,
becauseitsimultaneouslytransfersseveralobjectsto
thememory.

Package Structure
➢Apackagehastwosections:thespecificationsection
andthebodysection.
➢Theyneedtobecreatedseparately.

Specification Section
➢Thespecificationsectionisatypeofsummaryforthe
contentsofthepackagebody
➢ThespecificationsectioniscreatedwiththeCREATE
PACKAGEcommand,asshownbelow:
Syntax:
CREATE[ORREPLACE]PACKAGE [schema.]
package{IS|AS}pl/sql_package_spec
➢Arguments
ORREPLACE
Re-createsthepackagespecificationifitalreadyexists.

Specification Section
schema
➢Theschemathatwillcontainthepackage.Ifschemais
omitted,Oraclecreatesthepackageinitsownschema.
package
➢Thenameofthepackagetobecreated.
pl/sql_package_spec
➢Thepackagespecification.Itcandeclareprogram
objects.PackagespecificationsarewritteninPL/SQL.

Package Body
➢Thepackagebodycontainstheformaldefinitionofall
theobjectsreferencedinthedeclarationsection.
➢ThepackagebodyiscreatedwiththeCREATE
PACKAGEBODYcommand,asshownbelow:
➢Syntax:
CREATE[ORREPLACE]PACKAGEBODY[schema.]
package{IS|AS}pl/sql_package_body

Package Body
Arguments:
ORREPLACE
➢Re-createsthebodyofthepackageifitalreadyexists.
Schema
➢Theschemathatwillcontainthepackage.Ifschemais
omitted,Oraclecreatesitinitsownschema.
package
➢Thenameofthepackagetobecreated.
pl/sql_package_body
➢Thebodyofthepackage.Itcandeclareanddefine
programobjects.Packagebodiesarewrittenin
PL/SQL.

Steps to working with package
1.Createapackage(Declarationoffunctionand
procedure)
2.Compilethepackage
3.Createapackagebody(Definitionoffunctionand
procedure)
4.Compilethepackagebody
5.Executetheprocedurebyusingthefollowing
command.
Execpackagename.procedurename(argumentlist)
Ex:Execgenemp.increase2(1001,10)
6.Executethefunctionbyusingthefollowingcommand.
Selectpackagename.functionname(argument)fromdual.
Ex:Selectgenemp.countemp(1001)fromdual.

Package Creation
createPACKAGEGENEMP1AS
PROCEDURE increase2 (veno IN
emp2.eno%type,percentnumber);
functioncountemp(vdnoinemployee.dno%type)
RETURNnumber;
END;
/

Package Body Creation
createpackagebodygenemp1is
PROCEDURE increase2(venoINemp2.eno%type,percent
number)IS
BEGIN
updateemp2setsalary=salary*(1+percent/100)
whereeno=veno;
END;
functioncountemp(vdnoinempl.dno%type)returnnumberis
emptotnumber;
begin
selectcount(*)intoemptotfromemplwheredno=vdno;
returnemptot;
end;
end;
/

Referencing a Package Subprogram
To access the procedure specified inside a package, then
use the packagenameassociated with the procedure name.
SQL>Execgenemp.increase2(105,10)
PL/SQL procedure successfully completed.
Toaccessthefunctionspecifiedinsideapackage,then
usethepackagenameassociatedwiththefunctionname.
SQL>Selectgenemp.countemp(101)fromdual;
PL/SQLproceduresuccessfullycompleted.

Deleting a Package
➢To delete the package then use the following
command
SQL>Droppackagepackagename
Ex:Droppackagegenemp;
➢To delete the package body then use the following
command
SQL>Droppackagebodypackagename
Ex:Droppackagebodygenemp;

Oracle Packages
➢Oraclehasdozensofpackagescontainingprocedures
thatextenditsfunctionality.Mostofthemstartwith
theprefixDBMS_,UTL_,DEBUG_,orOUTLN_.
➢Followingisanexampleoftheuseofthe
DBMS_OUTPUT package,whichiscalledbya
triggerthatisenabledwhenanyDMLoperationis
performedintheEMP2table:

Oracle Packages
CREATEORREPLACETRIGGERsaldif
BEFOREDELETEORINSERTORUPDATEONemp2FOR
EACHROWWHEN(new.eno>0)
DECLARE
sal_diffnumber;
BEGIN
sal_diff:=:new.salary-:old.salary;
dbms_output.put('Old:'||:old.salary);
dbms_output.put('New:'||:new.salary);
dbms_output.put_line('Difference'||sal_diff);
END;
/
Output:
SQL> update emp2 set sal=sal*1.3 where eno=101;
Old: 1100 New: 1430 Difference 330