Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 8

User Defined Functions in MYSQL

- by
Hemanth Kumar Reddy
T
Batch -8
1. What are UDFs ?
2. How to Create UDFs ?

3. Example
4. Need of UDFS
Definition
• User Defined Functions(UDFs) are functions
composed by users, which in take some
parameters and perform previously defined
operations on it to return result of the action as a
value
• UDFs allow users to create custom queries ,
therefore easing it to users to solve complex
queries.
Creating User Defined Function
• The syntax for creating UDFs in SQL follows a specific structure and
format.

CREATE FUNCTION function_name


([parameter1 data_type1, parameter2 data_type2, ...])
RETURNS return_data_type
BEGIN
-- Function body
END;
Let’s understand
•function_name: The name of the UDF.
•parameter1, parameter2, ...: Optional
parameters that the UDF can accept. Each
parameter consists of a name and a data
type.
•return_data_type: The data type of the
value that the UDF returns.
•BEGIN and END: The start and end of the
function body. The function body contains
the logic and calculations performed by the
UDF.
Example
• UDF that calculates the square of a number :

CREATE FUNCTION square(number INT)


RETURNS INT
BEGIN
RETURN number * number;
END;
Need
OF
UDFs 1. Code Reusability
2. Modularity
3. Custom Logic
4. Performance Optimization
5. Encapsulation of security logic
6. Data Validation & Integrity
7. Abstraction of complex queries
….
and many more

You might also like