Summary: in this tutorial, you will learn what a stored procedure is, its advantages and disadvantages.
Definition of stored procedure
A stored procedure is a segment of declarative SQL code, which is stored in the database catalog. A stored procedure can be invoked by a program, a trigger or even another stored procedure.
A stored procedure which calls itself is known as recursive stored procedure. Almost RDMBS supports recursive stored procedure but MySQL does not support it very well. You should check your version of MySQL database before implementing recursive stored procedures.
Stored Procedure in MySQL
MySQL is known as the most popular open source RDBMS which is widely used by both community and enterprise. However during the first decade of its existence, it did not support stored procedure, trigger, event…etc. Since MySQL version 5.0, those features have been added to MySQL database engine to make it become flexible and powerful.
Before starting the tutorial series about stored procedure, you should have MySQL version 5.x+ installed in your computer or server.
Stored Procedures Advantages
- Stored procedures help increase the performance of application using them. Once created, stored procedures are compiled and stored in the database catalog. A stored procedure typically runs faster than uncompiled SQL statements that are sent from external applications.
- Stored procedures reduce the traffic between application and database server because instead of sending multiple uncompiled lengthy SQL statements, the application only has to send the stored procedure’s name with parameters.
- Stored procedures are reusable and transparent to any applications. Stored procedures expose the database interface to all applications so developers don’t have to develop functions that are already supported in stored procedure in those applications.
- Stored procedures are secured. Database administrator can grant appropriate permission to application that access stored procedures in database catalog without giving any permission on the underlying database tables.
Besides those advantages, stored procedures have their own disadvantages, which you should be aware of before deciding using store procedures.
Stored Procedures Disadvantages
- Stored procedures make the database server high load in both memory and processors. Instead of being focused on the storing and retrieving data, you could be asking the database server to perform a number of logical operations which is not well-designed for database server.
- Stored procedures consist of declarative SQL so it is very difficult to write a procedure with complexity of business logic like other languages in application layer such as Java, C#, C++…
- Stored procedures are difficult to debug. Only few database mangement system supports debugging stored procedures. There are some workarounds on this problem but it still not easy enough.
- Store procedures are not easy to develop and maintain. Developing and maintaining stored procedure are usually required specialized skill set that not all application developers possess. This may introduce problems in both application development and maintain phases.
Stored procedures have their own advantages and disadvantages. When you develop applications, you should decide whether to use stored procedure or not based on the requirements. In the following tutorials, we will show you how to leverage stored procedures in your database programming tasks with a couple of practical examples.
Getting Started with MySQL Stored Procedures
Summary: in this tutorial, you will learn how to write the first MySQL stored procedure using CREATE PROCEDURE statement. We will show you how to call a store procedure using CALL statement.
Writing the first MySQL stored procedure
We are going to develop a simple stored procedure named GetAllProducts() to help you get familiar with syntax. The function of this stored procedure is to retrieve all products from the products table.
First launch the mysql client tool and type the following commands:
|DELIMITER //CREATE PROCEDURE GetAllProducts()
SELECT * FROM products;
Let’s examine the stored procedure in a greater detail:
- The first command is DELIMITER //, which is not related to the stored procedure syntax. The DELIMITER statement changes the standard delimiter which is semicolon (;) to another. In this case, the delimiter is changed from the semicolon(;) to double-slashes //. Why do we have to change the delimiter? because we want to pass the stored procedure to the server as a whole instead of lettingmysql interprets each statement at a time when we type. Following the END keyword, we use delimiter // to indicate the end of the stored procedure. The last command (DELIMITER;) changes the delimiter back to the semicolon (;).
- The CREATE PROCEDURE statement is used to create a new stored procedure. You can specify the name of stored procedure after the CREATE PROCEDURE statement. In this case, the name of the stored procedure is GetAllProducts. Do not forget the parenthesis after the name of the store procedure or you will get an error message.
- Everything inside a pair of keyword BEGIN and END is called stored procedure’s body. You can put the declarative SQL code inside the stored procedure’s body to handle business logic. In the store procedure we used simple SQL SELECT statement to query data from the products table.
It’s kind of tedious to write the store procedure in mysql client tool especially when the stored procedure is complex. Most of the GUI tool for MySQL allows you to create new stored procedures using intuitive interface. For example, in MySQL Workbench, you can create a new stored procedure as follows:
Enter the stored procedure code and click the Apply button
You can review the code before MySQL stores it in the database. Click Apply button if everything is good.
MySQL compiles and puts the stored procedure in the database catalog; click the Finish button.
You can see a new stored procedure created under Routines of the classicmodelsdatabase
We have created a new stored procedure. Now it’s time to learn how to call or invoke it.
Calling stored procedures
In order to invoke a stored procedure, you use the following SQL command:
You use CALL statement to call a stored procedure e.g., to invoke the GetAllProducts stored procedure, you use the following command:
If you execute the command above, you will get all products in the products table.
In this tutorial, you have learned how to write a simple stored procedure using CREATE PROCEDURE statement and invoke it using the CALL statement.