DEV Community

Cover image for How to create function in MySQL
The Dream Coding
The Dream Coding

Posted on

How to create function in MySQL

1. Syntax

drop function if exists function_name ;
delimiter $$
CREATE FUNCTION function_name [ (parameter datatype [, parameter datatype]) ]
RETURNS return_datatype
BEGIN
    -- statements
END
$$
-- for use the function 
select function_name(parameter);
Enter fullscreen mode Exit fullscreen mode

2. example

drop function if exists countscoll;
delimiter $$
CREATE FUNCTION countscoll( cit int ) 
returns int 
begin 
   declare tota int ;
   set tota = ( select count(*) from school 
   where city = cit );
   return tota ;
end
$$
delimiter ;
select countscoll("new york");
Enter fullscreen mode Exit fullscreen mode

3. remark

we use function when we return value or Variable

4. i explaine how to create function in MySQL on youtube

click this => vidéo

5. this is my channel on youtube

click this => The Dream Coding

Top comments (0)