• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

MySql存储过程

武飞扬头像
技术老男孩
帮助1

一、概念:

  • 存储过程相当于mysql服务里的脚本
  • 将登录服务器后,要重复执行的命令写成存储过程
  • 存储过程分为:创建、查看、执行、删除
  • 存储过程会默认保存在mysql库下的proc表里面

二、语法结构:

创建存储过程

delimiter //
create procedure 库名.存储过程名()
begin
功能代码...
end
//
delimiter ;

查看存储过程

use 库名;
show procedure status \G

执行存储过程

call 存储过程名();
call 库名.存储过程名();

删除存储过程

drop procedure 库.存储过程名;

三、使用示例:

  • 创建存储过程 pria()
# 进入tarena库
mysql> use tarena;
Database changed
# 修改执行结束符为//
mysql> delimiter //
# 创建存储过程代码
mysql> create procedure pria()
    -> begin
    -> select count(*) from tarena.user;
    -> end
    -> //
Query OK, 0 rows affected (0.00 sec)
# 恢复执行结束符
mysql> delimiter ;
  • 查看存储过程 pria()
# 进入tarena库
mysql> use tarena;
# 查看关键词type是否是 PROCEDURE
mysql> show procedure status \G
*************************** 1. row ***************************
                  Db: tarena
                Name: pria
                Type: PROCEDURE
             Definer: root@localhost
            Modified: 2023-02-01 13:38:15
             Created: 2023-02-01 13:38:15
       Security_type: DEFINER
             Comment: 
character_set_client: utf8
collation_connection: utf8_general_ci
  Database Collation: latin1_swedish_ci
1 row in set (0.01 sec)

# 到mysql库中的proc表中查看信息
# 列出服务器上所有的存储过程
mysql> select db,name,type from mysql.proc;
 -------- ------ ----------- 
| db     | name | type      |
 -------- ------ ----------- 
| tarena | pria | PROCEDURE |
 -------- ------ ----------- 
1 row in set (0.00 sec)
#查看存储过程的功能代码
mysql> select body from mysql.proc where type="PROCEDURE";
 --------------------------------------------- 
| body                                        |
 --------------------------------------------- 
| begin
select count(*) from tarena.user;
end |
 --------------------------------------------- 
1 row in set (0.00 sec)
  • 执行存储过程 pria()
  • 存储创建时括号() 里没有参数 ,执行可以省略()
mysql> call pria;
Query OK, 0 rows affected (0.00 sec)
mysql> call pria();
Query OK, 0 rows affected (0.00 sec)
mysql> call tarena.pria;
Query OK, 0 rows affected (0.00 sec)
mysql> call tarena.pria();
Query OK, 0 rows affected (0.00 sec)
  • 删除存储过程 pria()
  • 删除不带括号
mysql> drop procedure tarena.pria;
Query OK, 0 rows affected (0.01 sec)

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgkfejh
系列文章
更多 icon
同类精品
更多 icon
继续加载