[MySQL光速入門]015 聊聊存儲過程

存儲過程

瞭解幾個容易混淆的概念sql

  1. 存儲過程
  2. 視圖
  3. 事務
  4. 函數

視圖(view):

能夠理解成臨時表, 若是你每次都須要連表查詢, 並且sql老長老長了, 那就試試視圖吧, 你會感謝個人.安全

事務(transaction):

一組sql語句, 要麼所有執行成功, 要麼都不執行session

函數(function):

執行特定功能的代碼段, 和存儲過程(procedure)很像 區別在於, 函數有且只有個返回值, 存儲過程對返回值的個數沒有要求, 沒有返回值也是能夠的函數

瞭解存儲過程

存儲過程的概念

  • 存儲過程和函數(執行特定功能的代碼段)很像, 語法略有不一樣,
  • 存儲過程主要用來增刪改, 視圖主要用來

存儲過程的優缺點

優勢:post

  1. 提升效率
  2. 代碼複用
  3. 減小開發人員的工做量
  4. 對存儲過程設置權限, 安全性較高

缺點:ui

  • 用起來方便, 改起來費勁...

存儲過程參數介紹

  • 函數有參數, 存儲過程也有參數
  • 定義參數時, 須要有參數名, 參數類型, 參數方向
  • 存儲過程的參數方向有三個(in, out, inout)
    • in: 只把值傳給存儲過程
    • out: 只從存儲過程當中返回值
    • inout: 參數在存儲過程當中進行運算, 而後返回

若是沒有寫方向, 默認是inspa

設計存儲過程

建立存儲過程

使用存儲過程, 爲教師表添加數據, 只須要姓名職稱設計

建立教師表
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('李四','特高級教師他爹');
複製代碼

以上是建立並用存儲過程的實例.code

其中咱們使用了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;
複製代碼

快速跳轉

相關文章
相關標籤/搜索