Different types of Procedures in MySQL

A procedure is a subroutine (like a subprogram) in a regular scripting language, stored in a database. In the case of MySQL, procedures are written in MySQL and stored in the MySQL database/server. A MySQL procedure has a name, a parameter list, and SQL statement(s).

There are four different types of MySQL procedures:

1. Procedure with no parameters:
A procedure without parameters does not take any input or casts an output indirectly. It is simply called with its procedure name followed by () (without any parameters). It is used for simple queries.

Example:
Consider two tables author and book:

create table author (author_id integer primary key, 
                            authorName varchar(30), 
                            email varchar (25), gender varchar (6));

create table book (BookId integer not null unique, 
                        ISBN integer primary key, 
                       book_name varchar (30) not null, 
                        author integer, ed_num integer, 
                      price integer, pages integer, 
         foreign key (author) references author (author_id) on delete cascade);

Inserting values into them:

insert into author values 
              (1, "Kraig Muller", "Wordnewton@gmail.com", "Male");
insert into author values
              (2, "Karrie Nicolette", "karrie23@gmail.com", "Female");
insert into book values
              (1, 001, "Glimpses of the past", 1, 1, 650, 396);
insert into book values
              (2, 002, "Beyond The Horizons of Venus", 1, 1, 650, 396);
insert into book values
              (3, 003, "Ultrasonic Aquaculture", 2, 1, 799, 500);
insert into book values
              (4, 004, "Cyrogenic Engines", 2, 1, 499, 330); 

Procedure (with no parameters) to display all the books:

delimiter //
create procedure display_book() 
                      -> begin 
                      -> select *from book; 
                      -> end //
call display_book(); //

Output:

+--------+------+------------------------------+--------+--------+-------+-------+
| BookId | ISBN | book_name                    | author | ed_num | price | pages |
+--------+------+------------------------------+--------+--------+-------+-------+
|      1 |    1 | Glimpses of the past         |      1 |      1 |   650 |   396 |
|      2 |    2 | Beyond The Horizons of Venus |      1 |      1 |   650 |   396 |
|      3 |    3 | Ultrasonic Aquaculture       |      2 |      1 |   799 |   500 |
|      4 |    4 | Cyrogenic Engines            |      2 |      1 |   499 |   330 |
+--------+------+------------------------------+--------+--------+-------+-------+
4 rows in set (0.0012 sec)

2. Procedure with IN parameter:
An IN parameter is used to take a parameter as input such as an attribute. When we define an IN parameter in a procedure, the calling program has to pass an argument to the stored procedure. In addition, the value of an IN parameter is protected. It means that even if the value of the IN parameter is changed inside the procedure, its original value is retained after the procedure ends (like pass by value). In other words, the procedure only works on the copy of the IN parameter.

Example:
Procedure to update price of a book taking ISBN of the book and its new price as input: (considering the tables above)

delimiter //
create procedure update_price (IN temp_ISBN varchar(10), IN new_price integer)
               -> begin
               -> update book set price=new_price where ISBN=temp_ISBN;
               -> end; //
call update_price(001, 600); //

We changed the price of book with ISBN ‘001’(Glimpses of the past) to 600 (from its default price 650).

Output:

delimiter ;
select *from book;
+--------+------+------------------------------+--------+--------+-------+-------+
| BookId | ISBN | book_name                    | author | ed_num | price | pages |
+--------+------+------------------------------+--------+--------+-------+-------+
|      1 |    1 | Glimpses of the past         |      1 |      1 |   600 |   396 |
|      2 |    2 | Beyond The Horizons of Venus |      1 |      1 |   650 |   396 |
|      3 |    3 | Ultrasonic Aquaculture       |      2 |      1 |   799 |   500 |
|      4 |    4 | Cyrogenic Engines            |      2 |      1 |   499 |   330 |
+--------+------+------------------------------+--------+--------+-------+-------+
4 rows in set (0.0013 sec)

3. Procedure with OUT parameter:
An OUT parameter is used to pass a parameter as output or display like the select operator, but implicitly (through a set value). The value of an OUT parameter can be changed inside the procedure and its new value is passed back to the calling program. A procedure cannot access the initial value of the OUT parameter when it starts.

Example:
Procedure to display the highest price among all the books with an output parameter:

delimiter //
create procedure disp_max(OUT highestprice integer)
                 -> begin
                 -> select max(price) into highestprice from book;
                 -> end; //
call disp_max(@M); //
select @M; 

Output:
The highest price from our book database is of the book with ISBN 003 (Ultrasonic Aquaculture) with a price of 799, which is displayed.

+-----+
| @M  |
+-----+
| 799 |
+-----+
1 row in set (0.0005 sec) 

4. Procedure with IN-OUT parameter:
An INOUT parameter is a combination of IN and OUT parameters. It means that the calling program may pass the argument, and the stored procedure can modify the INOUT parameter and pass the new value back to the calling program.

Example:
Procedure to take gender type input (‘Male’/’Female’ here) with an in-out parameter which reflects the number of authors falling in that gender category/type:

delimiter //
create procedure disp_gender(INOUT mfgender integer, IN emp_gender varchar(6))  
                     -> begin 
                     -> select COUNT(gender) 
                         INTO mfgender FROM author where gender = emp_gender;   
                     -> end; //
delimiter ;
call disp_gender(@M, "Male");
select @M;
call disp_gender(@F, "Female");
select @F; 

Output:
We have two authors, one being male and one being female as per insertions in the table author. Hence, output is 1 for one male author and 1 for one female author respectively.

+----+
| @M |
+----+
|  1 |
+----+
1 row in set (0.0004 sec)
+----+
| @F |
+----+
|  1 |
+----+
1 row in set (0.0005 sec)


My Personal Notes arrow_drop_up

Game Design | Cryptography | Web Development

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


4


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.