BK Birla Institue of Engineering & Technolgy (Bkbiet) Dbms Lab Sheet Vi
BK Birla Institue of Engineering & Technolgy (Bkbiet) Dbms Lab Sheet Vi
PROBLEM STATEMENT
Functions in MySQL
In MySQL, a function is a stored program that you can pass parameters into and then return a value.
Create Function
Just as you can create functions in other languages, you can create your own functions in MySQL. Let's
take a closer look.
Syntax
BEGIN
declaration_section
executable_section
END;
function_name
parameter
One or more parameters passed into the function. When creating a function, all parameters are
considered to be IN parameters (not OUT or INOUT parameters) where the parameters can be
referenced by the function but can not be overwritten by the function.
return_datatype
declaration_section
executable_section
The place in the function where you enter the code for the function.
Drop Function
Once you have created your function in MySQL, you might find that you need to remove it from the
database.
Syntax
function_name
PRACTICAL EXERCISE
Let's look at an example that shows how to create a function in MySQL:
DELIMITER //
BEGIN
RETURN income;
END; //
DELIMITER ;
A very basic CREATE FUNCTION example which will produced the famed 'Hello World' output:
DELIMITER $$
CREATE FUNCTION hello_world()
RETURNS TEXT
LANGUAGE SQL
BEGIN
RETURN 'Hello World';
END;
$$
DELIMITER ;
PRACTICAL PROBLEM
SOLUTION
Query to create table “Customer”
CREATE TABLE CUSTOMER
( Cid CHAR(4) PRIMARY KEY,
Name VARCHAR(30) NOT NULL,
Creditlimit INTEGER );
DELIMITER $$
RETURN (level);
END ;
$$
DELIMITER ;