mysql学习笔记(进阶一)
### 会话变量与全局变量 各客户端的session互不干扰 查看全部会话变量 show session variables; 查看单条 show session variables like 'auto%'; 更改会话变量 set 变量名 = '值' set @@session.变量名='值' 全局变量 全部 show global variables; 单条 show global variables like 'auto%' 更改 set global 变量名 = '值' set @@global.变量名='值' ### 存储过程 步骤: 1. 选择一个数据库 2. 改变分隔符(结束符) :delimiter $$; 3. 创建存储过程 create procedure p_hello() begin select "hello"; select "ddd"; end $$; 4. 执行存储过程 恢复结束符 delimiter ; 执行 call p_hello; 局部变量 定义:declare 变量名 数据类型 default 默认值 三参数: in输入参数 out输出参数 inout输入输出参数 ----declare--- begin declare inta int; set inta = 88; select inta; end ----in----- create procedure p_hello(in p_int int) begin set p_int = p_int+1; select p_int; end $$; delimiter ; set @p_int = 3; call p_hello(@p_int) //@p_int 的值在存储外并不会改变; ----out--- create procedure p_hello(out p_int int) begin set p_int = p_int+1; select p_int; end $$; delimiter ; set @p_int = 3; call p_hello(@p_int) //进入时所赋的值并不生效,不认可,为null //@p_int 的值在存储外会改变; ----inout--- create procedure p_hello(inout p_int int) begin set p_int = p_int+1; select p_int; end $$; delimiter ; set @p_int = 3; call p_hello(@p_int) //进入的值被认可 //@p_int 的值在存储外不会会改变;### 流程控制语句 选择语句 if else
begin ----- if age >=18 then select '成年人'; else select '未成年人'; end if ----- if age >=18 then select '成年人'; elseif age>=60 then select '未成年人'; else select '未成年人'; end if ----- end $$;case select id,name,(case gender when '1' then '男' else '女' end) from user; select ifnull(null,'不是空值') from uu; ifnull(exp1,exp2)//exp1如果为null,返回exp2的值
begin declare aaa int; case v_empno when 1 then set aaa ='我是1'; when 2 then set aaa = '我是2'; when 3 then set aaa = '我是3'; else set aaa = 'null'; end case; end; $$;循环语句 while
begin declare i int default 1; declare result int default 0; while i<=100 do set result = result+i; set i=i+1; end while;repeat
repeat 内容 until 条件 //退出循环的条件 end repeat; ------------- begin declare imin int default 1; declare imax int default 1; seelct max(a) into imax from user; seelct min(a) into imin from user; repeat if imin % 2 = 0 then update....... endif; set imin = imin+1; until imin>imax (没有分号) endrepeat; end;loop
loop名字:loop 内容 if 条件 then leave loop名字 endif; end loop;
myloop:loop if imax %2=1 then ipdate........ end if; set imax = imax+1 if imin > imax then leave loop myloop;//离开循环 end if; end loop;定义条件和处理 定义条件(在begin中定义) declare continue handler for sqlstate '错误代码值' set 变量=变量值
存储过程中,前一句sql出错,不影响下一条
存储过程
 删除存储过程 drop procedure if exists 存储过程名称
 函数的创建

 视图
本站文章如未注明出处均为原创,转载请注明出处,如有侵权请邮件联系站长。