MySQL/MariaDB数据库的存储过程
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.存储过程概述
1>.存储过程优势
存储过程把经常使用的SQL语句或业务逻辑封装起来,预编译保存在数据库中,当需要时从数据库中直接调用,省去了编译的过程
提高了运行速度
同时降低网络数据传输量
2>.存储过程与自定义函数的区别
存储过程实现的过程要复杂一些,而函数的针对性较强
存储过程可以有多个返回值,而自定义函数只有一个返回值
存储过程一般独立的来执行,而函数往往是作为其他SQL语句的一部分来使用
3>.存储过程存储位置
存储过程保存在mysql.proc表中。
MariaDB [yinzhengjie]> SELECT * FROM mysql.proc\G #不难发现存储过程和函数都保存在该表中。 *************************** 1. row *************************** db: mysql name: AddGeometryColumn type: PROCEDURE specific_name: AddGeometryColumn language: SQL sql_data_access: CONTAINS_SQL is_deterministic: NO security_type: DEFINER param_list: catalog varchar(64), t_schema varchar(64), t_name varchar(64), geometry_column varchar(64), t_srid int returns: body: begin set @qwe= concat('ALTER TABLE ', t_schema, '.', t_name, ' ADD ', geometry_column,' GEOMETRY REF_SYSTEM_ID=', t_srid); PREPARE ls from @qwe; exe cute ls; deallocate prepare ls; end definer: root@localhost created: 2019-10-26 22:17:15 modified: 2019-10-26 22:17:15 sql_mode: comment: character_set_client: utf8 collation_connection: utf8_general_ci db_collation: latin1_swedish_ci body_utf8: begin set @qwe= concat('ALTER TABLE ', t_schema, '.', t_name, ' ADD ', geometry_column,' GEOMETRY REF_SYSTEM_ID=', t_srid); PREPARE ls from @qwe; exe cute ls; deallocate prepare ls; end*************************** 2. row *************************** db: mysql name: DropGeometryColumn type: PROCEDURE specific_name: DropGeometryColumn language: SQL sql_data_access: CONTAINS_SQL is_deterministic: NO security_type: DEFINER param_list: catalog varchar(64), t_schema varchar(64), t_name varchar(64), geometry_column varchar(64) returns: body: begin set @qwe= concat('ALTER TABLE ', t_schema, '.', t_name, ' DROP ', geometry_column); PREPARE ls from @qwe; execute ls; deallocate prepare ls; en d definer: root@localhost created: 2019-10-26 22:17:15 modified: 2019-10-26 22:17:15 sql_mode: comment: character_set_client: utf8 collation_connection: utf8_general_ci db_collation: latin1_swedish_ci body_utf8: begin set @qwe= concat('ALTER TABLE ', t_schema, '.', t_name, ' DROP ', geometry_column); PREPARE ls from @qwe; execute ls; deallocate prepare ls; en d*************************** 3. row *************************** db: yinzhengjie name: students_numbers type: FUNCTION specific_name: students_numbers language: SQL sql_data_access: CONTAINS_SQL is_deterministic: NO security_type: DEFINER param_list: returns: smallint(6) body: BEGIN DECLARE x SMALLINT; SELECT COUNT(StuID) FROM students INTO x; RETURN x; END definer: root@localhost created: 2019-10-28 20:38:19 modified: 2019-10-28 20:38:19 sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION comment: character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci db_collation: utf8_general_ci body_utf8: BEGIN DECLARE x SMALLINT; SELECT COUNT(StuID) FROM students INTO x; RETURN x; END *************************** 4. row *************************** db: yinzhengjie name: deleteById type: FUNCTION specific_name: deleteById language: SQL sql_data_access: CONTAINS_SQL is_deterministic: NO security_type: DEFINER param_list: uid SMALLINT UNSIGNED returns: varchar(100) CHARSET utf8 body: BEGIN DELETE FROM students WHERE stuid = uid; RETURN (SELECT COUNT(stuid) FROM students); END definer: root@localhost created: 2019-10-28 20:06:27 modified: 2019-10-28 20:06:27 sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION comment: character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci db_collation: utf8_general_ci body_utf8: BEGIN DELETE FROM students WHERE stuid = uid; RETURN (SELECT COUNT(stuid) FROM students); END *************************** 5. row *************************** db: yinzhengjie name: addTwoNumber type: FUNCTION specific_name: addTwoNumber language: SQL sql_data_access: CONTAINS_SQL is_deterministic: NO security_type: DEFINER param_list: x SMALLINT UNSIGNED, Y SMALLINT UNSIGNED returns: smallint(6) body: BEGIN DECLARE a, b SMALLINT UNSIGNED; SET a = x, b = y; RETURN a+b; END definer: root@localhost created: 2019-10-28 20:13:59 modified: 2019-10-28 20:13:59 sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION comment: character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci db_collation: utf8_general_ci body_utf8: BEGIN DECLARE a, b SMALLINT UNSIGNED; SET a = x, b = y; RETURN a+b; END *************************** 6. row *************************** db: yinzhengjie name: showTime type: PROCEDURE specific_name: showTime language: SQL sql_data_access: CONTAINS_SQL is_deterministic: NO security_type: DEFINER param_list: returns: body: BEGIN SELECT now(); END definer: root@localhost created: 2019-10-28 20:49:09 modified: 2019-10-28 20:49:09 sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION comment: character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci db_collation: utf8_general_ci body_utf8: BEGIN SELECT now(); END *************************** 7. row *************************** db: yinzhengjie name: dorepeat type: PROCEDURE specific_name: dorepeat language: SQL sql_data_access: CONTAINS_SQL is_deterministic: NO security_type: DEFINER param_list: n INT returns: body: BEGIN SET @i = 0; SET @sum = 0; REPEAT SET @sum = @sum+@i; SET @i = @i + 1; UNTIL @i > n END REPEAT; END definer: root@localhost created: 2019-10-28 21:32:01 modified: 2019-10-28 21:32:01 sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION comment: character_set_client: utf8mb4 collation_connection: utf8mb4_general_ci db_collation: utf8_general_ci body_utf8: BEGIN SET @i = 0; SET @sum = 0; REPEAT SET @sum = @sum+@i; SET @i = @i + 1; UNTIL @i > n END REPEAT; END 7 rows in set (0.00 sec) MariaDB [yinzhengjie]> MariaDB [yinzhengjie]>