1、ストアド・プロシージャとストアド・ファンクションの概要
SQLステートメントのセットのためのストアド・プロシージャおよび関数は、事前にコンパイルされ、データベースに格納され、コールストアド・プロシージャおよび関数は、データ処理を向上させるために、データベースとアプリケーションサーバとの間のデータの送信を低減する業務アプリケーションの開発者の多くを簡素化することができ効率が良いです。
ストアド・プロシージャおよび関数との間の差は、プロセスが格納されていない間関数は、返すことがあることです。
機能:プロセスは戻り値であり、
プロセス:関数の値が返されません。
図2に示すように、ストアドプロシージャ
ストアドプロシージャを作成する2,1
语法:
CREATE PROCEDURE procedure_name ([proc_parameter[,...]])
begin
-- SQL语句
end ;
示例 :
delimiter $
create procedure pro_test1()
begin
select 'Hello Mysql' ;
end$
delimiter ;
delimiter说明:
该关键字用来声明SQL语句的分隔符, 告诉MySQL解释器,该段命令是否已经结束了,mysql是否可以执行了。
默认情况下,delimiter是分号;。在命令行客户端中,如果有一行命令以分号结束,那么回车后,mysql将会执行该
命令。
2,1,1変数定義が
ローカル変数DECLAREによって定義することができ、変数のスコープは、BEGIN ... ENDブロック。
示例
delimiter $
create procedure pro_test2()
begin
declare num int default 5;
select num+ 10;
end$
割り当ては2,1,2set
次のように特定の構文は、SETを使用して直接割り当てを、または賦形剤は、定数式を割り当てることができます。
SET VAR_NAME = exprの【、VAR_NAME =式] ...
示例
DELIMITER $
CREATE PROCEDURE pro_test3()
BEGIN
DECLARE NAME VARCHAR(20);
SET NAME = 'MYSQL';
SELECT NAME ;
END$
あなたはまた、選択することができます...割り当てへの道によって:
DELIMITER $
CREATE PROCEDURE pro_test5()
BEGIN
declare countnum int;
select count(*) into countnum from city;
select countnum;
END$
2,1,3、条件が決定された場合
示例
delimiter $
create procedure pro_test6()
begin
declare height int default 175;
declare description varchar(50);
if height >= 180 then
set description = '身材高挑';
elseif height >= 170 and height < 180 then
set description = '标准身材';
else
set description = '一般身材';
end if;
select description ;
end$
:呼び出しの結果、
2,1,4-渡すパラメータの
構文
create procedure procedure_name([in/out/inout] 参数名 参数类型)
...
IN : 该参数可以作为输入,也就是需要调用方传入值 , 默认
OUT: 该参数作为输出,也就是该参数可以作为返回值
INOUT: 既可以作为输入参数,也可以作为输出参数
示例
delimiter $
create procedure pro_test5(in height int , out description varchar(100))
begin
if height >= 180 then
set description='身材高挑';
elseif height >= 170 and height < 180 then
set description='标准身材';
else
set description='一般身材';
end if;
end$
调用:
call pro_test5(168, @description)$
select @description$
小知识
@description : 这种变量要在变量名称前面加上“@”符号,叫做用户会话变量,代表整个会话过程他都是有作用
的,这个类似于全局变量一样。
@@global.sort_buffer_size : 这种在变量前加上 "@@" 符号, 叫做系统变量
2,1,5case構造
文法構造
方式一 :
CASE case_value
WHEN when_value THEN statement_list
[WHEN when_value THEN statement_list] ...
[ELSE statement_list]
END CASE;
方式二 :
CASE
WHEN search_condition THEN statement_list
[WHEN search_condition THEN statement_list] ...
[ELSE statement_list]
END CASE;
例
delimiter $
create procedure pro_test9(month int)
begin
declare result varchar(20);
case when month >= 1 and month <=3 then
set result = '第一季度';
when month >= 4 and month <=6 then
set result = '第二季度';
when month >= 7 and month <=9 then
set result = '第三季度';
when month >= 10 and month <=12 then
set result = '第四季度';
end case;
select concat('您输入的月份为 :', month , ' , 该月份为 : ' , result) as content ;
end$
2,1,6whileサイクル
例
delimiter $
create procedure pro_test8(n int)
begin
declare total int default 0;
declare num int default 1;
while num<=n do
set total = total + num;
set num = num + 1;
end while;
select total;
end$
2,1,7リピート構造
条件ループ制御文、ループの状態が終了したとき。実行前に条件を満たしながら、その条件付きループ終了を繰り返します。
例:n = 0の場合、ループが終了するまで。
delimiter $
create procedure pro_test10(n int)
begin
declare total int default 0;
repeat
set total = total + n;
set n = n - 1;
until n=0
end repeat;
select total ;
end$
2,1,8ループ文
LOOP単純なループループ終了条件が定義されている他のステートメントを使用する必要がある、あなたは通常、特定の構文を達成するためにLEAVEステートメントを使用することができ、次のようです
如果不在 statement_list 中增加退出循环的语句,那么 LOOP 语句可以用来实现简单的死循环。
[begin_label:] LOOP
statement_list
END LOOP [end_label]
leave语句
用来从标注的流程构造中退出,通常和 BEGIN ... END 或者循环一起使用。下面是一个使用 LOOP 和 LEAVE 的简
单例子 , 退出循环:
delimiter $
CREATE PROCEDURE pro_test11(n int)
BEGIN
declare total int default 0;
ins: LOOP
IF n <= 0 then leave ins;//退出语句
END IF;
set total = total + n;
set n = n - 1;
END LOOP ins;
select total;
END$
2,1,9カーソル/カーソルの
カーソル・データ・タイプは、クエリ結果セットを格納するために使用され、カーソルは、記憶及び循環機能中に結果セットを処理するために使用することができます。
示例
从游标中一条一条获取查询出来的数据,如果取得次数多余查询出来数据得行数,会报错。
create procedure pro_test11()
begin
declare e_id int(11);
declare e_name varchar(50);
declare e_age int(11);
declare e_salary int(11);
declare emp_result cursor for select * from emp;//定义游标
open emp_result;//打开游标
//取出游标中数据
fetch emp_result into e_id,e_name,e_age,e_salary;
select concat('id=',e_id , ', name=',e_name, ', age=', e_age, ', 薪资为:
',e_salary);
fetch emp_result into e_id,e_name,e_age,e_salary;
select concat('id=',e_id , ', name=',e_name, ', age=', e_age, ', 薪资为:
',e_salary);
close emp_result;//关闭游标
end$
環状構造によって、データカーソルを取得します。
DELIMITER $
create procedure pro_test12()
begin
DECLARE id int(11);
DECLARE name varchar(50);
DECLARE age int(11);
DECLARE salary int(11);
DECLARE has_data int default 1;
DECLARE emp_result CURSOR FOR select * from emp;//定义游标
//声明了handler,当从游标中取不到数据得时候退出。
//注意,声明得位置要在游标下面,has_data在上面定义。
DECLARE EXIT HANDLER FOR NOT FOUND set has_data = 0;
open emp_result;//打开游标
repeat
fetch emp_result into id , name , age , salary;
select concat('id为',id, ', name 为' ,name , ', age为 ' ,age , ', 薪水为: ',
salary);
until has_data = 0//退出条件
end repeat;
close emp_result;
end$
2,2ストアドプロシージャを呼び出します
call procedure_name() ;
2,3ビューストアドプロシージャ
-- 查询db_name数据库中的所有的存储过程
select name from mysql.proc where db='db_name';
-- 查询存储过程的状态信息
show procedure status;
-- 查询某个存储过程的定义
show create procedure test.pro_test1 \G;
2,4削除ストアドプロシージャ
DROP PROCEDURE [IF EXISTS] sp_name ;
3、メモリ機能
サンプルを作成します。3,1
delimiter $
create function count_city(countryId int)
returns int//返回值类型定义
begin
declare cnum int ;
select count(*) into cnum from city where country_id = countryId;
return cnum;
end$
3,2保存された関数を呼び出します
不能用call调用了
select count_city(1);
select count_city(2);
3,3削除保存された機能
drop function fun1$
図4に示すように、フリップフロップ
説明:トリガーは、データベース・オブジェクトのテーブルに関連付けられた挿入/更新/削除の前または後にトリガーを参照し、トリガーコレクションに定義されたSQL文を実行しています。この機能は、データの整合性、ロギング、データ検証動作を保証するために、データベースのトリガ最後のアプリケーションを容易にすることができます。
他のデータベースに似ているフリップフロップの内容のレコード変化にOLDとNEWの参照別名を使用します。今もトリガーのみ、行レベルのトリガをサポートして文レベルのトリガをサポートしていません。
4,1文法構造
create trigger trigger_name
before/after insert/update/delete
on tbl_name
[ for each row ] -- 行级触发器
begin
trigger_stmt ;
end;
例
创建 insert 型触发器,完成插入数据时的日志记录 :
After insert 在插入后执行的触发器
For each row 行级触发器
On emp 作用于emp表
DELIMITER $
create trigger emp_logs_insert_trigger
after insert
on emp
for each row
begin
insert into emp_logs (id,operation,operate_time,operate_id,operate_params)
values(null,'insert',now(),new.id,concat('插入后(id:',new.id,', name:',new.name,',
age:',new.age,', salary:',new.salary,')'));
end $
创建 update 型触发器,完成更新数据时的日志记录 :
DELIMITER $
create trigger emp_logs_update_trigger
after update
on emp
for each row
begin
insert into emp_logs (id,operation,operate_time,operate_id,operate_params)
values(null,'update',now(),new.id,concat('修改前(id:',old.id,', name:',old.name,',
age:',old.age,', salary:',old.salary,') , 修改后(id',new.id, 'name:',new.name,',
age:',new.age,', salary:',new.salary,')'));
end $
创建delete 行的触发器 , 完成删除数据时的日志记录 :
DELIMITER $
create trigger emp_logs_delete_trigger
after delete
on emp
for each row
begin
insert into emp_logs (id,operation,operate_time,operate_id,operate_params)
values(null,'delete',now(),old.id,concat('删除前(id:',old.id,', name:',old.name,',
age:',old.age,', salary:',old.salary,')'));
end $
テスト
insert into emp(id,name,age,salary) values(null, '光明左使',30,3500);
insert into emp(id,name,age,salary) values(null, '光明右使',33,3200);
update emp set age = 39 where id = 3;
delete from emp where id = 5;
4,2削除トリガー
//なしSCHEMA_NAME場合は、現在のデータベースへのデフォルト。
ドロップトリガー[SCHEMA_NAME。] TRIGGER_NAME
4,3ビュー、トリガ
情報のトリガは、SHOW TRIGGERSコマンド状態と文法を実行することで表示することができます。
トリガを示し;