SlideShare a Scribd company logo
1 of 15
RDBMS
Presented by
Prof. Dipali Y. Jadhav
Mamasaheb Mohol college,kothrud,pune.
Introduction to PLSQLBlock–
• PLSQL is oracle’s procedural language which provide extension to
SQL language.
• For accessing data in relational database PLSQL block is made up of
3 sections-
• 1) Declarative section (Optional)
• 2) Executable section(Required)
• 3) Exception handling(Optional)
• Only Begin and End keywords are required.
• Block structure of PLSQL
Declare (Optional)
We can declare variables, cursor,user defined
exceptions etc.
Begin(Required)
SQL statements or PLSQL statements.
Exception(Optional)
Action to be performed when error occurs.
End(Requird)
• The oracle engine can process entire PL/SQL block.
• Plsql blocks are sent to oracle engine, where procedural
statements executed in sql statements. which is sent to sql engine
in the oracle.
• [plsql engine reisde in oracle engine , this will improve speed of
operation.]
• PL/SQL runtime Enviroment:
PLSQL Execution envoroment
Pl/sql built in data types:
• 1) Numeric Datatypes:
• For this we used int datatype and numeric data type
• For ex: number(I,j);
• Where: i: Total no. of decimal digits.
j:Total no. of digits after digits.
ex: num(2,3)
• 2) Character String data type:
• For fixed length we used char(n): where blank spaces are
considered.
• Varchar(n): where no blank spaces are considered.
PL/SQL Code block
• A function can be used as a part of SQL expression i.e. we can
use them with select/update/merge commands.
• One most important characteristic of a function is that unlike
procedures, it must return a value.
• Syntax to create a function:
• Create[or replace] function function_name (arguments) returns
type as’
Declare
declarations;
[…….]
Begin
statements;
[…….]
End;
• Where,
• function-name specifies the name of the function.
• [OR REPLACE] option allows the modification of an existing
function.
• The optional parameter list contains name, mode and types of
the parameters. IN represents the value that will be passed
from outside and OUT represents the parameter that will be
used to return a value outside of the procedure.
• The function must contain a return statement.
• The RETURN clause specifies the data type you are going to
return from the function.
• function-body contains the executable part.
• The AS keyword is used instead of the IS keyword for creating
a standalone function.
• 1) Dclaring simple function to calculate the area of circle.
• Create or replace function circle_area() returns float as’
declare
pi float :=3.14;
r float default 5;
area float;
begin
area:=pi*r*r;
return area;
End;
‘language ‘plpgsql’;
• Now above is the sample program that you should follow
following steps to run on postgress are as follows-
• 1) Open the text editor
• 2) write above code , and save the file as filename.sql
• 3) now open the terminal and type the following commands-
• #] su – postgres
• #] createdb demo
• #] createlang plpgsql demo
• #]Psql demo
• #]i progname.sql
• #] select circle_area();
• Above given statements are for postgresql.
• While writing su – postgres it will connect to bash prompt
• After creating database by using createdb demo.
• createlang plpgsql demo will create language so that you are
connected to plpgsql language and then type psql demo , so it will
connects to the demo database.
• After that by using i progname .sql it will run that program and by
using select circle_area() it will display the output.
• Then the output will look like-
• Area
• --------
• 23
Attribute:%TYPE and%ROWTYPE
• To handle with database objects we use % type and
% rowtype attribute.
• %TYPE: It is used to declare variable with same type as the
database object structure.
• The most powerful advantage is that if in future the datatype
of database object changed then, the declared variable in
program will also changes automatically.
• Syntax: variable_name table_name.column_name%type
• For example:
• Create or replace function emp_dept(int) returns text as’
• Declare
• empid alias for $1;
• tname employee.ename%type;
• Begin
• select into tname ename from emp where eid=empid;
• return tname;
• End;
• ‘language’plpgsql’;
• %Rowtype: It is used to declare variable with the structure same
as row structure of table.
• Syntax: variable_name table_name.%rowtype
• For accessing the data of particular field of table –
• Syntax: variablename.fieldname
• Create function empdept(integer)returns text as’
• Declare
• empid alias for $1;
• emp employee %rowtype;
• Begin
• select into emp * from employee where eid=empid;
• return emp.empdpt;
• End;
• ‘language’plpgsql’;
Functions with Loop
• 3 types of loop in PLSQL
• 1) basic loop
• 2) while loop
• 3) for loop
• Programs for all above 3 loops are as follows-
• 1) Basic Loop:
• Create or replace function sum1() returns int as’
• Declare
• sum int default 0;
• num int default 1;
• Begin
• Loop
• sum:=sum+num;
• num:=num+1;
• Exit when num=5;
• End loop;
• Return sum;
• End;
• ‘language’plpgsql’;
• In above program , we are going to print 1 to 5 numbers
• And default keyword is used to set default value as 0 and 1 for sum and num
variable .
• 2) While Loop:
• Create or replace function sum1() returns int as’
• Declare
• sum int default 0;
• num int default 1;
• Begin
• While num<5 loop
• sum:=sum+num;
• num:=num+1;
• End loop;
• Return sum;
• End;
• ‘language’plpgsql’;
ForLoop:programtoprinttheaverageof firstfive
numbers
• Create or replace function sum1() returns int as’
• Declare
• i int default 0;
• avg int default 1;
• Begin
• For i in 1..5 loop
• avg:=avg+i;
• End loop;
• Avg:=avg/5;
• Return avg;
• End;
• ‘language’plpgsql’;
We will discuss Procedure in next lecture.
Thank You….

More Related Content

What's hot

Intro to JavaScript - Week 2: Function
Intro to JavaScript - Week 2: FunctionIntro to JavaScript - Week 2: Function
Intro to JavaScript - Week 2: FunctionJeongbae Oh
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT03062679929
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++Learn By Watch
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKamal Acharya
 
Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)Jay Patel
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
Command line arguments
Command line argumentsCommand line arguments
Command line argumentsAshok Raj
 
Anonymous and Inline Functions in MATLAB
Anonymous and Inline Functions in MATLABAnonymous and Inline Functions in MATLAB
Anonymous and Inline Functions in MATLABShameer Ahmed Koya
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 
Function in c++
Function in c++Function in c++
Function in c++Kumar
 
Lambda Expressions in Java
Lambda Expressions in JavaLambda Expressions in Java
Lambda Expressions in JavaErhan Bagdemir
 
predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functionsSwapnil Yadav
 

What's hot (20)

functions
functionsfunctions
functions
 
Intro to JavaScript - Week 2: Function
Intro to JavaScript - Week 2: FunctionIntro to JavaScript - Week 2: Function
Intro to JavaScript - Week 2: Function
 
Function C++
Function C++ Function C++
Function C++
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
functions of C++
functions of C++functions of C++
functions of C++
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
Built in function
Built in functionBuilt in function
Built in function
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Unit 3(rdbms)
Unit 3(rdbms)Unit 3(rdbms)
Unit 3(rdbms)
 
user defined function
user defined functionuser defined function
user defined function
 
Function & Recursion
Function & RecursionFunction & Recursion
Function & Recursion
 
Java8
Java8Java8
Java8
 
Inline function
Inline functionInline function
Inline function
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
 
Anonymous and Inline Functions in MATLAB
Anonymous and Inline Functions in MATLABAnonymous and Inline Functions in MATLAB
Anonymous and Inline Functions in MATLAB
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Function in c++
Function in c++Function in c++
Function in c++
 
Lambda Expressions in Java
Lambda Expressions in JavaLambda Expressions in Java
Lambda Expressions in Java
 
predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functions
 

Similar to Rdbms chapter 1 function

PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals INick Buytaert
 
ORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSmohdoracle
 
Introduction to c first week slides
Introduction to c first week slidesIntroduction to c first week slides
Introduction to c first week slidesluqman bawany
 
TEMPLATES IN JAVA
TEMPLATES IN JAVATEMPLATES IN JAVA
TEMPLATES IN JAVAMuskanSony
 
Programming in java basics
Programming in java  basicsProgramming in java  basics
Programming in java basicsLovelitJose
 
PLSQL-OO [SOUG 2022].pptx
PLSQL-OO [SOUG 2022].pptxPLSQL-OO [SOUG 2022].pptx
PLSQL-OO [SOUG 2022].pptxRichard Martens
 
Programming Language
Programming  LanguageProgramming  Language
Programming LanguageAdeel Hamid
 
Peeking into the Black Hole Called PL/PGSQL - the New PL Profiler / Jan Wieck...
Peeking into the Black Hole Called PL/PGSQL - the New PL Profiler / Jan Wieck...Peeking into the Black Hole Called PL/PGSQL - the New PL Profiler / Jan Wieck...
Peeking into the Black Hole Called PL/PGSQL - the New PL Profiler / Jan Wieck...Ontico
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2sumitbardhan
 
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdfProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdflailoesakhan
 
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdfManiMala75
 
C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorialSURBHI SAROHA
 

Similar to Rdbms chapter 1 function (20)

SQL / PL
SQL / PLSQL / PL
SQL / PL
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals I
 
Mis4200notes8 2
Mis4200notes8 2Mis4200notes8 2
Mis4200notes8 2
 
ORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERS
 
c++ UNIT II.pptx
c++ UNIT II.pptxc++ UNIT II.pptx
c++ UNIT II.pptx
 
Introduction to c first week slides
Introduction to c first week slidesIntroduction to c first week slides
Introduction to c first week slides
 
TEMPLATES IN JAVA
TEMPLATES IN JAVATEMPLATES IN JAVA
TEMPLATES IN JAVA
 
Programming in java basics
Programming in java  basicsProgramming in java  basics
Programming in java basics
 
PLSQL-OO [SOUG 2022].pptx
PLSQL-OO [SOUG 2022].pptxPLSQL-OO [SOUG 2022].pptx
PLSQL-OO [SOUG 2022].pptx
 
Plc part 3
Plc  part 3Plc  part 3
Plc part 3
 
Programming Language
Programming  LanguageProgramming  Language
Programming Language
 
Peeking into the Black Hole Called PL/PGSQL - the New PL Profiler / Jan Wieck...
Peeking into the Black Hole Called PL/PGSQL - the New PL Profiler / Jan Wieck...Peeking into the Black Hole Called PL/PGSQL - the New PL Profiler / Jan Wieck...
Peeking into the Black Hole Called PL/PGSQL - the New PL Profiler / Jan Wieck...
 
PL_SQL - II.pptx
PL_SQL - II.pptxPL_SQL - II.pptx
PL_SQL - II.pptx
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2
 
C
CC
C
 
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdfProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
 
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
662213141-Tuxdoc-com-Programming-in-c-Reema-Thareja.pdf
 
CPP06 - Functions
CPP06 - FunctionsCPP06 - Functions
CPP06 - Functions
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorial
 

Recently uploaded

NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdf
NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdfNAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdf
NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdfWadeK3
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Sérgio Sacani
 
Boyles law module in the grade 10 science
Boyles law module in the grade 10 scienceBoyles law module in the grade 10 science
Boyles law module in the grade 10 sciencefloriejanemacaya1
 
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Nistarini College, Purulia (W.B) India
 
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfBehavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfSELF-EXPLANATORY
 
Nanoparticles synthesis and characterization​ ​
Nanoparticles synthesis and characterization​  ​Nanoparticles synthesis and characterization​  ​
Nanoparticles synthesis and characterization​ ​kaibalyasahoo82800
 
GFP in rDNA Technology (Biotechnology).pptx
GFP in rDNA Technology (Biotechnology).pptxGFP in rDNA Technology (Biotechnology).pptx
GFP in rDNA Technology (Biotechnology).pptxAleenaTreesaSaji
 
A relative description on Sonoporation.pdf
A relative description on Sonoporation.pdfA relative description on Sonoporation.pdf
A relative description on Sonoporation.pdfnehabiju2046
 
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bNightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bSérgio Sacani
 
Biopesticide (2).pptx .This slides helps to know the different types of biop...
Biopesticide (2).pptx  .This slides helps to know the different types of biop...Biopesticide (2).pptx  .This slides helps to know the different types of biop...
Biopesticide (2).pptx .This slides helps to know the different types of biop...RohitNehra6
 
VIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C PVIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C PPRINCE C P
 
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...Sérgio Sacani
 
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCESTERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCEPRINCE C P
 
G9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.pptG9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.pptMAESTRELLAMesa2
 
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Lokesh Kothari
 
Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PPRINCE C P
 
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsSérgio Sacani
 
Scheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxScheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxyaramohamed343013
 

Recently uploaded (20)

NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdf
NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdfNAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdf
NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdf
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
 
Boyles law module in the grade 10 science
Boyles law module in the grade 10 scienceBoyles law module in the grade 10 science
Boyles law module in the grade 10 science
 
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
 
Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...
 
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfBehavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
 
Nanoparticles synthesis and characterization​ ​
Nanoparticles synthesis and characterization​  ​Nanoparticles synthesis and characterization​  ​
Nanoparticles synthesis and characterization​ ​
 
GFP in rDNA Technology (Biotechnology).pptx
GFP in rDNA Technology (Biotechnology).pptxGFP in rDNA Technology (Biotechnology).pptx
GFP in rDNA Technology (Biotechnology).pptx
 
A relative description on Sonoporation.pdf
A relative description on Sonoporation.pdfA relative description on Sonoporation.pdf
A relative description on Sonoporation.pdf
 
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bNightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
 
Biopesticide (2).pptx .This slides helps to know the different types of biop...
Biopesticide (2).pptx  .This slides helps to know the different types of biop...Biopesticide (2).pptx  .This slides helps to know the different types of biop...
Biopesticide (2).pptx .This slides helps to know the different types of biop...
 
VIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C PVIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C P
 
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
 
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCESTERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
 
G9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.pptG9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.ppt
 
Engler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomyEngler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomy
 
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
 
Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C P
 
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
 
Scheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxScheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docx
 

Rdbms chapter 1 function

  • 1. RDBMS Presented by Prof. Dipali Y. Jadhav Mamasaheb Mohol college,kothrud,pune.
  • 2. Introduction to PLSQLBlock– • PLSQL is oracle’s procedural language which provide extension to SQL language. • For accessing data in relational database PLSQL block is made up of 3 sections- • 1) Declarative section (Optional) • 2) Executable section(Required) • 3) Exception handling(Optional) • Only Begin and End keywords are required. • Block structure of PLSQL Declare (Optional) We can declare variables, cursor,user defined exceptions etc. Begin(Required) SQL statements or PLSQL statements. Exception(Optional) Action to be performed when error occurs. End(Requird)
  • 3. • The oracle engine can process entire PL/SQL block. • Plsql blocks are sent to oracle engine, where procedural statements executed in sql statements. which is sent to sql engine in the oracle. • [plsql engine reisde in oracle engine , this will improve speed of operation.] • PL/SQL runtime Enviroment: PLSQL Execution envoroment
  • 4. Pl/sql built in data types: • 1) Numeric Datatypes: • For this we used int datatype and numeric data type • For ex: number(I,j); • Where: i: Total no. of decimal digits. j:Total no. of digits after digits. ex: num(2,3) • 2) Character String data type: • For fixed length we used char(n): where blank spaces are considered. • Varchar(n): where no blank spaces are considered.
  • 5. PL/SQL Code block • A function can be used as a part of SQL expression i.e. we can use them with select/update/merge commands. • One most important characteristic of a function is that unlike procedures, it must return a value. • Syntax to create a function: • Create[or replace] function function_name (arguments) returns type as’ Declare declarations; […….] Begin statements; […….] End;
  • 6. • Where, • function-name specifies the name of the function. • [OR REPLACE] option allows the modification of an existing function. • The optional parameter list contains name, mode and types of the parameters. IN represents the value that will be passed from outside and OUT represents the parameter that will be used to return a value outside of the procedure. • The function must contain a return statement. • The RETURN clause specifies the data type you are going to return from the function. • function-body contains the executable part. • The AS keyword is used instead of the IS keyword for creating a standalone function.
  • 7. • 1) Dclaring simple function to calculate the area of circle. • Create or replace function circle_area() returns float as’ declare pi float :=3.14; r float default 5; area float; begin area:=pi*r*r; return area; End; ‘language ‘plpgsql’; • Now above is the sample program that you should follow following steps to run on postgress are as follows- • 1) Open the text editor • 2) write above code , and save the file as filename.sql • 3) now open the terminal and type the following commands-
  • 8. • #] su – postgres • #] createdb demo • #] createlang plpgsql demo • #]Psql demo • #]i progname.sql • #] select circle_area(); • Above given statements are for postgresql. • While writing su – postgres it will connect to bash prompt • After creating database by using createdb demo. • createlang plpgsql demo will create language so that you are connected to plpgsql language and then type psql demo , so it will connects to the demo database. • After that by using i progname .sql it will run that program and by using select circle_area() it will display the output. • Then the output will look like- • Area • -------- • 23
  • 9. Attribute:%TYPE and%ROWTYPE • To handle with database objects we use % type and % rowtype attribute. • %TYPE: It is used to declare variable with same type as the database object structure. • The most powerful advantage is that if in future the datatype of database object changed then, the declared variable in program will also changes automatically. • Syntax: variable_name table_name.column_name%type
  • 10. • For example: • Create or replace function emp_dept(int) returns text as’ • Declare • empid alias for $1; • tname employee.ename%type; • Begin • select into tname ename from emp where eid=empid; • return tname; • End; • ‘language’plpgsql’; • %Rowtype: It is used to declare variable with the structure same as row structure of table. • Syntax: variable_name table_name.%rowtype
  • 11. • For accessing the data of particular field of table – • Syntax: variablename.fieldname • Create function empdept(integer)returns text as’ • Declare • empid alias for $1; • emp employee %rowtype; • Begin • select into emp * from employee where eid=empid; • return emp.empdpt; • End; • ‘language’plpgsql’;
  • 12. Functions with Loop • 3 types of loop in PLSQL • 1) basic loop • 2) while loop • 3) for loop • Programs for all above 3 loops are as follows- • 1) Basic Loop: • Create or replace function sum1() returns int as’ • Declare • sum int default 0; • num int default 1; • Begin • Loop • sum:=sum+num;
  • 13. • num:=num+1; • Exit when num=5; • End loop; • Return sum; • End; • ‘language’plpgsql’; • In above program , we are going to print 1 to 5 numbers • And default keyword is used to set default value as 0 and 1 for sum and num variable . • 2) While Loop: • Create or replace function sum1() returns int as’ • Declare • sum int default 0; • num int default 1; • Begin • While num<5 loop • sum:=sum+num; • num:=num+1; • End loop; • Return sum; • End; • ‘language’plpgsql’;
  • 14. ForLoop:programtoprinttheaverageof firstfive numbers • Create or replace function sum1() returns int as’ • Declare • i int default 0; • avg int default 1; • Begin • For i in 1..5 loop • avg:=avg+i; • End loop; • Avg:=avg/5; • Return avg; • End; • ‘language’plpgsql’;
  • 15. We will discuss Procedure in next lecture. Thank You….