mysql 初级篇(三)
触发器、存储过程、游标
触发器trigger
Creat trigger buggoodAfterInsertOn ordFor each rowBeginUpdate goods set num=num-new.much where id=new.gidEndNew insert 来引用,因为insert插入新的行,所以用new来引用
Old delete 来引用,因为delete删除记录,是因为记录原来就已经存在,所以用old来引用。For each row的作用:每一行受影响,触发器都执行,叫做行级触发器。 查看触发器:show trigger删除触发器:drop trigger triggerName 过程:封装了若干条语句,调用时,执行封装体,但没有返回值。函数:是一个有返回值的“过程”。存储过程:procedure;
Create procedure testName()BeginSql语句;end查看存储过程:show procedure status
调用存储过程:call procedure()Create procedure t1()
BeginDeclare age int;Select concat(‘age:’,age)End变量可以sql语句中合法的运算;
Set 变量名:=expressionCreate procedure t2()BeginDeclare age int default 18;Set age:=age+10;Select userInfo(“real age : ”, age);EndCreate procedure t3()
BeginDeclare age int default 18;If age>=18 thenSelect ‘已成年’ ;ElseSelect “未成年” ;End if ;存储过程中声明参数,语法是:[in/out/inout] 参数名 参数类型
Create procedure t3(age int)BeginIf age >=18 thenSelec “已成年” ;Else Select “未成年” ;End if ;end 控制结构:顺序,分支,循环Create procedure t4()BeginDeclare total int default 0 ;Declare num int default 0 ;While num <=100 doSet total := total + num ;Set num := num +1 ;End while;Select total ;End In 是指接收参数Create procedure t5(in n int)BeginDeclare total int default 0;Declare num int default 0;while num <=n doSet :=total+sum ;Set :=sum+ 1 ;End while;EndCall t5(100) --调用存储过程
Out 是指往外部发射一个参数,调用时,要提供参数用于保存结果的值
Create procedure t6(in n int,out total int)BeginDeclare total int default 0;Set total :=0;while num <=n doSet :=total+sum ;Set :=sum+ 1 ;End while;EndCall t6(100,@sumary) --调用存储过程
Inout 是指即可以传递参数,也可以发射出参数。
Create procedure t7()BeginDeclare age int default 1;Case ageWhen 10 then select “chid” ;When 20 then select “student” ;Else select “not data”End case;end Repeat 循环语句:Repeat Sql 语句;Until i>=100 end repeat; --结束循环条件Create procedure t8()
BeginDeclare i :=0;Declare total :=0;Repeat Set i:=i+1;Set total:=total+i ;Until i<=100 end repeat;Select total;End 游标 cursor ;对应N条结果集的资源,取出资源的接口/句柄,就是游标,沿着游标,可以一次取1行。Declare 声明游标;declare 游标名 cursor for select_statement;Open 打开游标,open 游标名Fetch 取值;fetch 游标名 into var1,var2....Close 关闭;close 游标名Create procedure t9()
BeginDeclare row_gid int;Declare row_num int ;Declare row_name varchar(20);Declare getgoods cursor for select gid,num,name from goods;Open cursor;
Fetch getgoods into row_gid,row_num,row_name;
Select row_gid,row_num ;Close getgoods;end Create procedure t10()BeginDeclare row_gid int ;Declare row_num int;Declare row_name int ;Declare cnt int default 0;Declare i int default 0;Declare getgoods cursor for select gid,num,name from goods;Select count(gid) into cnt from goods;
Open getgoods;
Repeat
Fetch getgoods into row_gid,row_num,row_name;Set cnt:= cnt -1;Select row_gid,row_num,row_name;Until cnt=0 end repeat;
Close getgoods;
End
游标取值越界时,利用标识来结束,在mysql cursor 中,可以declare continue handle 来操作1个越界标识。Declare continue handler for not Found statement; Create procedure t10()BeginDeclare row_gid int ;Declare row_num int;Declare row_name int ;Declare cnt int default 0;Declare yes int default 1;Declare getgoods cursor for select gid,num,name from goods;Declare continue handler for not Found set yes:=0;Select count(gid) into cnt from goods;
Open getgoods;
Fetch getgoods into row_gid,row_num,row_name; RepeatSelect row_gid,row_num,row_name;Fetch getgoods into row_gid,row_num,row_name;Until yes=0 end repeat;
Close getgoods;
End