Home  >  Article  >  Database  >  Why use mysql stored procedures? Introduction to mysql stored procedures

Why use mysql stored procedures? Introduction to mysql stored procedures

巴扎黑
巴扎黑Original
2017-05-18 11:01:171535browse

Stored Procedures

Requires MySQL 5 MySQL 5 adds support for stored procedures, therefore, the content of this chapter applies to MySQL 5 and later versions.

Most SQL statements used so far are single statements targeting one or more tables. Not all operations are so simple, and often a complete operation requires multiple statements to complete. For example, consider the following situation.

1. In order to process the order, it needs to be checked to ensure that the corresponding items are in stock.

2. If there are items in stock, these items need to be reserved so that they are not resold to others, and the number of items available is reduced to reflect the correct inventory level.

3. Items not in stock need to be ordered, which requires some kind of interaction with the supplier.

4. The corresponding customers need to be informed about which items are in stock (and can be shipped immediately) and which items are unsubscribed.

This is obviously not a complete example, and it is even beyond the scope of the sample tables used in this book, but it is enough to help express our meaning. Performing this processing requires multiple MySQL statements against many tables. Furthermore, the specific statements that need to be executed and their order are not fixed, they can (and will) change depending on which items are in the inventory and which are not.

So, how to write this code? You can write each statement individually and conditionally execute additional statements based on the results. This must be done every time this processing is needed (and in every application that requires it).

Stored procedures can be created. Simply put, a stored procedure is a collection of one or more MySQL statements that are saved for future use. Think of them as batch files, although their role is not limited to batch processing.


Why use stored procedures

Now that we know what a stored procedure is, why should we What about using them? There are many reasons, some of the main ones are listed below.

1. Simplify complex operations by encapsulating processing in easy-to-use units.

2. This ensures data integrity since it does not require repeated establishment of a series of processing steps. If all developers and applications use the same (pilot and test) stored procedure, the code used is the same. An extension of this point is to prevent errors. The more steps you need to perform, the greater the chance of error. Preventing errors ensures data consistency.

3. Simplify the management of changes. If the table names, column names, or business logic (or anything else) changes, you only need to change the code of the stored procedure. People using it don't even need to be aware of the changes. An extension of this is security. Restricting access to underlying data through stored procedures reduces the chance of data corruption (unintentional or otherwise).

4. Improve performance. Because using stored procedures is faster than using separate SQL statements.

5. There are some MySQL elements and features that can only be used in a single request. Stored procedures can use them to write more powerful and flexible code. In other words, there are three main benefits of using stored procedures, namely simplicity, safety, and high performance. Obviously, they are all important. However, before converting SQL code into stored procedures, you must also be aware of some of its shortcomings.

6. Generally speaking, writing stored procedures is more complicated than basic SQL statements. Writing stored procedures requires higher skills and richer experience.

7. You may not have security access to create stored procedures. Many database administrators restrict stored procedure creation permissions, allowing users to use stored procedures but not allow them to create stored procedures. Despite these flaws, stored procedures are very useful and should be used whenever possible.

Can't write a stored procedure? You can still use MySQL to separate the security and access of writing stored procedures from the security and access of executing stored procedures. This is a good thing. Even if you can't (or don't want to) write your own stored procedures, you can still execute other stored procedures when appropriate.

The above is the detailed content of Why use mysql stored procedures? Introduction to mysql stored procedures. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn