视图
存储过程
基本命令:
变量:
参数:
//带参存储过程的用法
//创建
create procedure p4(in score int, out result varchar(10))
begin
if score >= 85 then
set result :='优秀';
elseif score >= 60 then
set result :='及格';
else
set result :='不及格'
end if;
end;
//调用
call p4( score: 68,result: @result):
游标:
为什么要是用游标?因为变量不能存储多行数据
//游标的基本用法
A.声明游标,存储查询结果集
B.准备:创建表结构
C.开启游标
D.获取游标中的记录
E:插入数据到新表中
F.关闭游标
//示例代码
create procedure p(in uage int)
begin
declare uname varchar(100);
declare upro varchar(100);
//定义游标,把数据放进去
declare u_cursor cursor for select name,profession from tb user where age <= uage;
//程序条件控制,循环退出
declare exit handler forrSOLSTATE '02000' close u cursor:
//创建表
create table if not exists tb user pro(
id int primary key auto_increment
name varchar(100)
profession varchar(100)
)
//开启游标
open u_cursor;
//循环取数据并插入表
while true do
fetch u cursor into uname,upro;
insert into tb_user pro values(null,uname, upro);end while;
end;
call p(30);
触发器
//插入触发器
create trigger tb_user_insert_trigger
after insert on tb_user for each row
begin
insert into user_logs(id, operation, operate_time, operate_id, operate_params) VALUES
(null, 'insert', now(), new.id, concat('插入的数据内容为: id=',new.id,',name=',new.name, ', phone=', NEW.phone, ', email=', NEW.email, ', profession=', NEW.profession));
end;
//更新触发器
create trigger tb_user_update_trigger
after update on tb_user for each row
begin
insert into user_logs(id, operation, operate_time, operate_id, operate_params) VALUES
(null, 'update', now(), new.id,
concat('更新之前的数据: id=',old.id,',name=',old.name, ', phone=', old.phone, ', email=', old.email, ', profession=', old.profession,
' | 更新之后的数据: id=',new.id,',name=',new.name, ', phone=', NEW.phone, ', email=', NEW.email, ', profession=', NEW.profession));
end;
//删除触发器
create trigger tb_user_delete_trigger
after delete on tb_user for each row
begin
insert into user_logs(id, operation, operate_time, operate_id, operate_params) VALUES
(null, 'delete', now(), old.id,
concat('删除之前的数据: id=',old.id,',name=',old.name, ', phone=', old.phone, ', email=', old.email, ', profession=', old.profession));
end;
结束结束!!!