Home > Database > Mysql Tutorial > What is a stored procedure and how do we create a MySQL stored procedure?

What is a stored procedure and how do we create a MySQL stored procedure?

WBOY
Release: 2023-08-24 18:09:02
forward
1072 people have browsed it

What is a stored procedure and how do we create a MySQL stored procedure?

A stored procedure, in the context of a regular computing language, can be defined as a subroutine-like subroutine that is stored in a database. In the context of MySQL, it is a declarative SQL statement stored in the database directory. Before writing stored procedures in MySQL, we have to check the version because MySQL 5 introduced stored procedures. The following is the syntax to create a stored procedure:

Syntax

CREATE [DEFINER = { user | CURRENT_USER }]
PROCEDURE sp_name ([proc_parameter[,...]])
[characteristic ...] routine_body
proc_parameter: [ IN | OUT | INOUT ] param_name type
type:
Any valid MySQL data type
characteristic:
COMMENT 'string'
| LANGUAGE SQL
| [NOT] DETERMINISTIC
| { CONTAINS SQL | NO SQL | READS SQL DATA
| MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
routine_body:
Valid SQL routine statement
Copy after login

Example

The following is an example where we have created a simple procedure to get all from the table 'student_info' records, these records have the following data −

mysql> select * from student_info;
+-----+---------+------------+------------+
| id  | Name    | Address    | Subject    |
+-----+---------+------------+------------+
| 100 | Aarav   | Delhi      | Computers  |
| 101 | YashPal | Amritsar   | History    |
| 105 | Gaurav  | Jaipur     | Literature |
| 110 | Rahul   | Chandigarh | History    |
+------+--------+------------+------------+
4 rows in set (0.00 sec)
Copy after login

Now, with the following query, we have created a stored procedure named allrecords()

mysql> Delimiter //
mysql> Create Procedure allrecords()
    -> BEGIN
    -> Select * from Student_info;
    -> END//
Query OK, 0 rows affected (0.02 sec)
mysql> DELIMITER ;
Copy after login

The above is the detailed content of What is a stored procedure and how do we create a MySQL stored procedure?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
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