Home > Database > Mysql Tutorial > body text

mysql stored procedure usage

王林
Release: 2023-05-18 14:52:38
Original
965 people have browsed it

Usage of MySQL stored procedures

MySQL stored procedures are a set of predefined SQL statements designed to complete specific tasks. Stored procedures can help improve database performance, improve maintainability, and also help reduce network traffic and security.

This article will introduce the basic syntax of MySQL stored procedures, as well as several practical examples.

1. Basic syntax

1. Create a stored procedure

The syntax for creating a stored procedure is as follows:

CREATE PROCEDURE procedure_name(
    [IN] input_parameter_name data_type,
    [OUT] output_parameter_name data_type,
    [INOUT] input_output_parameter_name data_type
)
BEGIN
    -- SQL语句
END;
Copy after login

Among them, procedure_name represents the name of the stored procedure, input_parameter_name Indicates the name of the input parameter, output_parameter_name indicates the name of the output parameter, and input_output_parameter_name indicates the name of the input and output parameters.

2. Call a stored procedure

The syntax for calling a stored procedure is as follows:

CALL procedure_name([input_parameter_value], [output_parameter_value], [input_output_parameter_value]);
Copy after login

Among them, procedure_name represents the name of the stored procedure to be called, and input_parameter_value represents the parameter to be passed into the stored procedure. Input parameters, output_parameter_value represents the return value of the output parameter, and input_output_parameter_value represents the value of the input and output parameters.

2. Practical examples

1. Use stored procedures to query student scores

The following is a simple example of querying student scores through stored procedures:

CREATE PROCEDURE get_student_grade(IN student_name VARCHAR(255))
BEGIN
    SELECT * FROM student_grade WHERE name = student_name;
END;
Copy after login

This stored procedure receives a student's name as an input parameter and outputs the student's grade information.

2. Use a stored procedure to update the administrator password

The following is another example of updating the administrator password through a stored procedure:

CREATE PROCEDURE update_admin_password(IN admin_id INT, IN new_password VARCHAR(255))
BEGIN
    UPDATE admin SET password=new_password WHERE id=admin_id;
END;
Copy after login

This stored procedure receives the administrator's ID and new password as input parameters, and updates the administrator's password.

3. Use stored procedures to delete expired data

The following is an example of deleting expired data through stored procedures:

CREATE PROCEDURE delete_expired_data(IN table_name VARCHAR(255), IN expire_date DATE)
BEGIN
    DELETE FROM table_name WHERE date < expire_date;
END;
Copy after login

This stored procedure receives the table name and expiration date as input parameters , and delete expired data in the specified table.

3. Summary

This article mainly introduces the basic syntax and practical examples of MySQL stored procedures. Stored procedures can help us complete more work by improving the performance and maintainability of the database. However, when using stored procedures, we need to pay attention to security and avoid problems such as SQL injection attacks. It can also be seen from the above examples that stored procedures can be used to update data, query data, and delete data. It is a very practical function in the MySQL database.

The above is the detailed content of mysql stored procedure usage. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!