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

Transposable elements in prokaryotes.ppt
Transposable elements in prokaryotes.pptTransposable elements in prokaryotes.ppt
Transposable elements in prokaryotes.pptArshadWarsi13
 
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.aasikanpl
 
‏‏VIRUS - 123455555555555555555555555555555555555555
‏‏VIRUS -  123455555555555555555555555555555555555555‏‏VIRUS -  123455555555555555555555555555555555555555
‏‏VIRUS - 123455555555555555555555555555555555555555kikilily0909
 
Harmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms PresentationHarmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms Presentationtahreemzahra82
 
Forest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are importantForest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are importantadityabhardwaj282
 
Analytical Profile of Coleus Forskohlii | Forskolin .pdf
Analytical Profile of Coleus Forskohlii | Forskolin .pdfAnalytical Profile of Coleus Forskohlii | Forskolin .pdf
Analytical Profile of Coleus Forskohlii | Forskolin .pdfSwapnil Therkar
 
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxLIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxmalonesandreagweneth
 
Solution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsSolution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsHajira Mahmood
 
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...lizamodels9
 
Pests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdfPests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdfPirithiRaju
 
Pests of castor_Binomics_Identification_Dr.UPR.pdf
Pests of castor_Binomics_Identification_Dr.UPR.pdfPests of castor_Binomics_Identification_Dr.UPR.pdf
Pests of castor_Binomics_Identification_Dr.UPR.pdfPirithiRaju
 
Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024AyushiRastogi48
 
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
 
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptxTHE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptxNandakishor Bhaurao Deshmukh
 
FREE NURSING BUNDLE FOR NURSES.PDF by na
FREE NURSING BUNDLE FOR NURSES.PDF by naFREE NURSING BUNDLE FOR NURSES.PDF by na
FREE NURSING BUNDLE FOR NURSES.PDF by naJASISJULIANOELYNV
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxkessiyaTpeter
 
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
 
Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)DHURKADEVIBASKAR
 

Recently uploaded (20)

Transposable elements in prokaryotes.ppt
Transposable elements in prokaryotes.pptTransposable elements in prokaryotes.ppt
Transposable elements in prokaryotes.ppt
 
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
 
‏‏VIRUS - 123455555555555555555555555555555555555555
‏‏VIRUS -  123455555555555555555555555555555555555555‏‏VIRUS -  123455555555555555555555555555555555555555
‏‏VIRUS - 123455555555555555555555555555555555555555
 
Harmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms PresentationHarmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms Presentation
 
Forest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are importantForest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are important
 
Analytical Profile of Coleus Forskohlii | Forskolin .pdf
Analytical Profile of Coleus Forskohlii | Forskolin .pdfAnalytical Profile of Coleus Forskohlii | Forskolin .pdf
Analytical Profile of Coleus Forskohlii | Forskolin .pdf
 
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxLIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
 
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
 
Solution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsSolution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutions
 
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
 
Pests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdfPests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdf
 
Pests of castor_Binomics_Identification_Dr.UPR.pdf
Pests of castor_Binomics_Identification_Dr.UPR.pdfPests of castor_Binomics_Identification_Dr.UPR.pdf
Pests of castor_Binomics_Identification_Dr.UPR.pdf
 
Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024
 
Volatile Oils Pharmacognosy And Phytochemistry -I
Volatile Oils Pharmacognosy And Phytochemistry -IVolatile Oils Pharmacognosy And Phytochemistry -I
Volatile Oils Pharmacognosy And Phytochemistry -I
 
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
 
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptxTHE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
 
FREE NURSING BUNDLE FOR NURSES.PDF by na
FREE NURSING BUNDLE FOR NURSES.PDF by naFREE NURSING BUNDLE FOR NURSES.PDF by na
FREE NURSING BUNDLE FOR NURSES.PDF by na
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
 
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 ...
 
Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)
 

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