事件 + 存储过程 (基础)
一、事件(可以执行定时任务)
1、create even if not exists Event_Stat_Daily
even 【start/end 时间戳】 表示完成重复任务,时间单位可以是year,month,day,hour,minute,second
3、on completion [not] preserve 表示事件执行完任务不会被保存
存储过程: do call P_INS_UP_USER_LINE()$$
CREATE TABLE test1;//创建表(需要测试一下)
二、存储过程
存储过程就是将若干条sql语句封装起来,编译好放在mysql服务器中,需要时调用即可;
Declear 用来定义参数;
set 用来设置变量的值;
if then ... else...end if 用于判断 ;
while...do...end while 用于循环 ;
创建存储过程
案例1
Create procedure newTest(in name char(100))
begin
DECLARE age INT DEFAULT 16;
DECLARE height INT DEFAULT 170;
DECLARE weight INT DEFAULT 16;
set age:=age+10;
set weight:=weight*10;
SELECT name ;
if age>18 then
select '已成年';
else
select '未成年';
end if;
SELECT CONCAT('年龄',age);
SELECT concat('身高',height,'cm');
SELECT CONCAT('体重',weight,'公斤');
End;
案例2
Create procedure newTest()
begin
declare i int default 0;
declare sum1 int default 0;
while i<=10 do
set sum1:=i+sum1;
set i:=i+1;
end while;
select concat('1~10的和为',sum1);
end;
案例3
Create procedure newTest()
begin
DECLARE ave INT DEFAULT 10;
SET ave:=10/5;
CASE ave
WHEN 1 THEN SELECT '学神';
WHEN 2 THEN SELECT '学霸';
WHEN 3 THEN SELECT '学奴';
ELSE SELECT '学渣';
END CASE;
end;
执行存储过程