美文网首页
第15课 聊聊存储过程

第15课 聊聊存储过程

作者: 猫哥的技术博客 | 来源:发表于2019-04-08 20:11 被阅读0次

存储过程

了解几个容易混淆的概念

  1. 存储过程
  2. 视图
  3. 事务
  4. 函数

视图(view):

可以理解成临时表, 如果你每次都需要连表查询, 而且sql老长老长了, 那就试试视图吧, 你会感谢我的.

事务(transaction):

一组sql语句, 要么全部执行成功, 要么都不执行

函数(function):

执行特定功能的代码段, 和存储过程(procedure)很像
区别在于, 函数有且只有个返回值, 存储过程对返回值的个数没有要求, 没有返回值也是可以的

了解存储过程

存储过程的概念

  • 存储过程和函数(执行特定功能的代码段)很像, 语法略有不同,
  • 存储过程主要用来增删改, 视图主要用来

存储过程的优缺点

优点:

  1. 提高效率
  2. 代码复用
  3. 减少开发人员的工作量
  4. 对存储过程设置权限, 安全性较高

缺点:

  • 用起来方便, 改起来费劲...

存储过程参数介绍

  • 函数有参数, 存储过程也有参数
  • 定义参数时, 需要有参数名, 参数类型, 参数方向
  • 存储过程的参数方向有三个(in, out, inout)
    • in: 只把值传给存储过程
    • out: 只从存储过程中返回值
    • inout: 参数在存储过程中进行运算, 然后返回

如果没有写方向, 默认是in

设计存储过程

创建存储过程

使用存储过程, 为教师表添加数据, 只需要姓名职称

创建教师表
drop table if exists teacher;

CREATE TABLE `teacher` (
  `id` int(11) NOT NULL auto_increment primary key,
  `salary` int(11) NOT NULL,
  `title` char(20) NOT NULL,
  `name` char(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `teacher`
    (`id`, `salary`, `title`, `name`) 
    VALUES 
    (1, 1500, '初级讲师', '教师1');
INSERT INTO `teacher`
    (`id`, `salary`, `title`, `name`) 
    VALUES 
    (2, 5500, '高级讲师', '教师2');
INSERT INTO `teacher`
    (`id`, `salary`, `title`, `name`) 
    VALUES 
    (3, 6500, '金牌讲师', '教师3');
INSERT INTO `teacher`
    (`id`, `salary`, `title`, `name`) 
    VALUES 
    (4, 7500, '教授', '教师4');
INSERT INTO `teacher`
    (`id`, `salary`, `title`, `name`) 
    VALUES 
    (5, 8500, '金牌教授', '教师5');
创建存储过程
drop procedure if exists add_teacher;
create procedure add_teacher(
    in teacher_name varchar(20),
    in teacher_title varchar(20)
) begin
INSERT INTO `teacher`
    (`salary`, `title`, `name`) 
VALUES 
    (3500, teacher_title, teacher_name);
end;
调用存储过程
call add_teacher('张三','特高级教师');
call add_teacher('李四','特高级教师他爹');

以上是创建并用存储过程的实例.

其中我们使用了in类型的参数, 接下来试试out

再看个例子, 添加数据后, 返回该表一共有多少条记录, 也就是说
添加教师后, 可以查看已经有多少老师了

创建存储过程
drop procedure if exists add_teacher;
create procedure add_teacher(
    in teacher_name varchar(20),
    in teacher_title varchar(20),
    out teacher_count int) 
begin
  INSERT INTO `teacher`
    (`salary`, `title`, `name`) 
    VALUES 
    (3500, teacher_title, teacher_name);
  select count(*) into teacher_count from teacher;
end;
调用存储过程
call add_teacher('张三','特高级教师', @count);
select @count;
call add_teacher('李四','特高级教师他爹', @count);
select @count;

着重讲一下 inout

表示该参数, 既用于输入, 有用于输出;
比如发年终奖(14薪),最后一个月, 发3个月的工资
我们的需求, 输入一个数字(每年的薪水, 14薪, 还是15薪), 返回最后一个月需要发多少钱

drop procedure if exists count_salary;
create procedure count_salary(
    in month int, 
    inout salary int
) begin 
  select salary*(month - 11) into salary;
end;

set @salary = 2000;
call count_salary(14,@salary);
select @salary;

为了方便理解, 我的变量使用中文...

drop procedure if exists 计算工资;
create procedure 计算工资(
    in 月份 int, 
    inout 月工资 int
) begin 
  select 月工资*(月份 - 11) into 月工资;
end;

set @月工资 = 2000;
call 计算工资(15,@月工资);
select @月工资;

in,outinout的区别何在呢?

假设我们想计算一个数字的平方, 我们分别用inoutin,out来试一下

drop procedure if exists count_square;
create procedure count_square(inout a int) BEGIN 
select a*a into a;
end;

set @a = 3;
call count_square(@a);
select @a;

同样提供中文版

drop procedure if exists 计算平方;
create procedure 计算平方(inout 数字 int) BEGIN 
select 数字*数字 into 数字;
end;

set @数字 = 3;
call 计算平方(@数字);
select @数字;
drop procedure if exists count_square2;
create procedure count_square2(in a int, out b int) begin 
select a*a into b;
end;

call count_square2(3,@b);
select @b;
drop procedure if exists 再计算平方;
create procedure 再计算平方(in 第一个数字 int, out 第二个数字 int) begin 
select 第一个数字*第一个数字 into 第二个数字;
end;

call 再计算平方(3,@第二个数字);
select @第二个数字;

查看存储过程信息

show procedure status like 'salary';
show create procedure add_teacher;

删除存储过程

drop procedure salary;

变量(声明, 赋值, 使用, 作用域)

  • 系统变量
    • 全局变量(global) 只要不重启, 都适用
    • 会话变量(session) 只能是当前会话
  • 自定义变量
    • 全局/用户变量(当前会话)
    • 局部/局部变量(当前函数)

查看变量(全部)

-- 第一条和第三条效果一样
show variables;
show global variables;
show session variables;

查看变量(搜索)

-- 第一条和第三条效果一样
show variables like 'auto%';
show global variables like 'auto%';
show session variables like 'auto%';

查看变量(一个)
select @@global|[session].系统变量;

-- 第一句和第三句一样
select @@autocommit;
select @@global.autocommit;
select @@session.autocommit;

全局变量, 重启失效, 除非改配置文件

修改变量/变量赋值

语法

set global|[session] 系统变量名 = 值;
set @@global|[session].系统变量名 = 值;

set autocommit = 0;
-- 相当于
-- set session autocommit = 0
-- 或者
-- set @@session.autocommit = 0;
-- 全局
-- set @@global.autocommit = 0;
-- set global autocommit = 0;

select @@autocommit;
select @@session.autocommit;
select @@global.autocommit;

用户自定义变量

  • 全局变量(当前会话)

set @variableName = value;
set @variableName := value;
select @variableName := value;

set @hello_world = 'hello world !!!!';
select @hello_world;

set @hello_world := 'hello world !!!!';
select @hello_world;

select @hello_world := 'hello world !!!!';
select @hello_world;

查询赋值

set @count = 0;
select count(*) into @count from teacher;
select @count;

局部变量(begin end)

drop PROCEDURE if EXISTS hello_world;

create PROCEDURE hello_world() begin 
declare result int;
select count(*) into result from teacher;
select result;
end;

call hello_world;

不能在begin end外使用

drop PROCEDURE if EXISTS hello_world;

create PROCEDURE hello_world() begin 
declare result int;
select count(*) into result from teacher;
select result;
end;

call hello_world;
select result;

注意, 必须是begin后的第一行

drop PROCEDURE if EXISTS hello_world;

create PROCEDURE hello_world() begin 
select 1+1;
declare result int;
select count(*) into result from teacher;
select result;
end;

call hello_world;

@变量名可以在begin end中使用

drop PROCEDURE if EXISTS hello_world;
set @result = 0;
create PROCEDURE hello_world() begin 
select count(*) into @result from teacher;
select @result;
end;

call hello_world;
select @result;

相关文章

  • 第15课 聊聊存储过程

    存储过程 了解几个容易混淆的概念 存储过程 视图 事务 函数 视图(view): 可以理解成临时表, 如果你每次都...

  • 《mysql必知必会》读书实战笔记23-存储过程

    第23章 使用存储过程 23.1 存储过程 存储过程,是为了完成一个复杂任务,而编写的多条sql语句的集合,语句执...

  • Mysql存储过程

    阅读目录:MySQL存储过程_创建-调用-参数 存储过程:SQL中的“脚本” 创建存储过程 调用存储过程 存储过程...

  • MySQL批量添加数据

    创建存储过程 执行存储过程 删除存储过程

  • 17 存储过程

    过程: 函数: 过程是没有返回值的函数 存储过程: 存储过程语法 存储过程1--创建简单的存储过程 存储过程2--...

  • 存储过程

    详见存储过程详解 创建存储过程 使用存储过程 存储过程简介 什么是存储过程:存储过程可以说是一个记录集吧,它是由一...

  • 存储过程与函数

    存储过程与函数存储过程的定义存储过程的创建存储过程的操作自定义函数 存储过程与函数 存储过程的定义 运行效率高 降...

  • 存储过程

    ps 不能修改过程体,要修改过程体必须先删除存储过程,再重建 mysql变量的术语分类: 1.用户变量:以"@"开...

  • 存储过程

    存储过程 过程,可以简单理解为是一个函数。 函数和过程的区别:函数总是向调用者返回数据,而过程没有数据返回。 Or...

  • 存储过程

    存储过程(Stored Procedure)是一个可编程的函数,是为了完成特定功能的SQL语句集;创建的存储过程保...

网友评论

      本文标题:第15课 聊聊存储过程

      本文链接:https://www.haomeiwen.com/subject/notmiqtx.html