1. Create a stored functionSyntax format: CREATE FUNCTION function name (parameter name parameter type, ...) RETURNS Return value type BEGIN Function body#The function body must contain a RETURN statement END illustrate: Parameter list: FUNCTION always The statement after The RETURNS clause can only be specified for FUNCTION and is The function body can also use If the function body contains only one statement, 2. Calling stored functions In MySQL, the usage of stored functions is the same as that of MySQL internal functions. In other words, user-defined stored functions are of the same nature as MySQL internal functions. The difference is that stored functions are SELECT function name (argument list) 3. Delete stored functionsGrammatical structure: DROP FUNCTION [IF EXISTS] stored function name 4. View stored procedures1. Use the SHOW CREATE statement to view the creation information of stored procedures and functions Grammatical structure: SHOW CREATE FUNCTION stored function name 2. Use the SHOW STATUS statement to view the status information of the stored function Grammatical structure: SHOW FUNCTION STATUS [LIKE 'pattern'] This statement returns the characteristics of the subroutine, such as database, name, type, creator, and creation and modification dates. Example: #Query the stored functions whose names begin with selectmysql> SHOW FUNCTION STATUS LIKE 'SELECT%'; *************************** 1. row *************************** Db: test_db Name: SelectAllData Type: FUNCTION Definer: root@localhost Modified: 2021-10-16 15:55:07 Created: 2021-10-16 15:55:07 Security_type: DEFINER Comment: character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci Database Collation: utf8mb4_general_ci 3. View the information of stored functions from the information_schema.Routines table The information of stored functions in MySQL is stored in the Routines table under the information_schema database. You can query the information of stored functions by querying the records in this table. Grammatical structure: SELECT * FROM information_schema.Routines WHERE ROUTINE_NAME = 'Stored function name' [AND ROUTINE_TYPE = 'FUNCTION ']; Note: If 5. Modify the storage functionModifying a storage function does not affect the function's functionality, but only modifies related features. This is done using the ALTER statement. ALTER FUNCTION stored function name; 6. Comparison between stored functions and stored procedures
In addition, stored functions can be used in query statements, but stored procedures cannot . On the contrary, stored procedures are more powerful, including the ability to perform table operations (such as creating tables, deleting tables, etc.) and transaction operations, which are not available in stored functions. 7. Strengthen the exercises#Prerequisites CREATE TABLE employees AS SELECT * FROM atguigudb.`employees`; CREATE TABLE departments AS SELECT * FROM atguigudb.`departments`; #1. Create a function get_count() to return the number of employees in the company. #with parameters and returns SET GLOBAL log_bin_trust_function_creators = 1; DELIMITER $ CREATE FUNCTION get_count() RETURNS INT BEGIN RETURN (SELECT COUNT(*) FROM employees); END $ DELIMITER ; Notice: When creating a stored #2. Create a function ename_salary() to return the salary of an employee according to his/her id. #Query the table structure to see the type of data to be returned. DESC employees; DELIMITER $ CREATE FUNCTION ename_salary(id INT) RETURNS DOUBLE(8,2) BEGIN RETURN (SELECT salary FROM employees WHERE employee_id = id); END $ DELIMITER ; #Query results SELECT ename_salary(100);
This is the end of this article about the detailed introduction of MySQL stored functions. For more relevant MySQL stored function content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
>>: What we have to say about CSS absolute and relative
Code first, then text Copy code The code is as fol...
Effect html <body> <div class="cont...
When laying out the page, in order to give users ...
The basic principle of all animations is to displ...
The MySQL version number is 5.7.28. Table A has 3...
Set the width of the body to the width of the wind...
Preface: When we want to clear a table, we often ...
Table of contents 1 Introduction to the new opera...
1. Add alternative text to your logo This has two...
Table of contents Variable type and storage space...
Compared with fdisk, parted is less used and is m...
Ellipses appear when multi-line text overflows Th...
Table of contents 1. Background 2. What is silent...
Optimize the fastcgi configuration file fcgiext.i...
MySQL installation is divided into installation v...