Home >Database >Mysql Tutorial >mysql c stored procedure
MySQL is a very commonly used relational database management system, which can be used to store, process and manage large amounts of data. Stored procedures are a very useful function provided by MySQL. They can encapsulate a series of SQL statements into a reusable code block, thereby improving the execution efficiency of SQL statements, reducing code complexity, improving security, etc. Therefore, mastering the use of MySQL stored procedures is of great significance to improving the efficiency of database development and management.
1. Introduction to stored procedures
Stored procedures are a collection of precompiled SQL statements defined in MySQL. They are compiled in the database server. When querying, you only need to call the stored procedures. . Compared with executing SQL statements alone, stored procedures can significantly reduce network overhead and improve the efficiency of data query and processing.
In addition, stored procedures have the following important features:
2. Syntax format of stored procedures
To define a stored procedure in MySQL, you need to use the CREATE PROCEDURE statement. The following is the basic syntax format of the CREATE PROCEDURE statement:
CREATE PROCEDURE 存储过程名称(参数列表) BEGIN -- sql语句 END;
Among them, the stored procedure name is required, and the parameter list can be omitted as needed. Between BEGIN and END, you can write a series of SQL statements to perform data query, writing, modification and other related operations.
The following is a simple example of a stored procedure. The stored procedure has only one parameter and simply returns it after modifying it:
CREATE PROCEDURE test_proc(IN num INT) BEGIN SET num = num + 1; SELECT num; END;
When executing the stored procedure, it can be called through the CALL statement :
CALL test_proc(10);
The execution result is 11.
3. Parameters in stored procedures
In stored procedures, you can use three types of parameters: IN, OUT and INOUT.
The following is an example of a stored procedure with parameters:
CREATE PROCEDURE get_user_by_id(IN userid INT, OUT username VARCHAR(50)) BEGIN SELECT username FROM user WHERE id=userid; SET username = CONCAT('Welcome, ', username); END;
When calling this stored procedure, you need to pass in the userid parameter and define a variable to receive the username output parameter:
DECLARE uname VARCHAR(50); CALL get_user_by_id(1, @uname); SELECT @uname;
4. Flow control and looping in stored procedures
In addition to directly executing SQL statements, stored procedures can also use flow control statements and looping statements to complete specific logical operations. MySQL supports the following flow control statements:
The following is an example of a WHILE DO loop. This stored procedure adds 1 year to the age in the user table:
CREATE PROCEDURE update_user_age() BEGIN DECLARE i INT DEFAULT 0; DECLARE n INT; SELECT COUNT(*) FROM user INTO n; WHILE i<=n DO UPDATE user SET age=age+1 WHERE id=i; SET i=i+1; END WHILE; END;
When executing this stored procedure, you only need to call:
CALL update_user_age();
5. Exception handling in stored procedures
When an error occurs in a stored procedure, exception handling can be used to avoid program crashes. In MySQL, exception handling is implemented using the DECLARE ... HANDLER syntax.
The following is an example of exception handling. When deleting a user table record, the stored procedure will throw an exception if the record has been referenced by other tables.
CREATE PROCEDURE delete_user(IN userid INT) BEGIN DECLARE exit_test CONDITION FOR SQLSTATE '23000'; START TRANSACTION; DELETE FROM user WHERE id=userid; IF ROW_COUNT() = 0 THEN SIGNAL exit_test; END IF; COMMIT; END;
When executing the stored procedure, if an exception occurs, you can write the following program code to handle it:
DECLARE EXIT HANDLER FOR SQLSTATE '23000' -- 处理异常 END;
6. Optimization of the stored procedure
Although the stored procedure It can improve the efficiency of SQL queries, but if the stored procedure design is unreasonable, it may also lead to a decrease in query efficiency. The following are some optimization suggestions for stored procedures:
In short, stored procedures are a very important function in MySQL. It can improve the execution efficiency of SQL statements, reduce code complexity, improve security, etc. When using stored procedures, you need to pay attention to parameters, process control, exception handling, etc. You also need to consider how to improve database query and update efficiency by optimizing stored procedures.
The above is the detailed content of mysql c stored procedure. For more information, please follow other related articles on the PHP Chinese website!