存储过程可以减少数据在应用服务器和数据库之间的网络传输,提高对数据处理的效率,操作就是对sql语句进行封装
create view view1 as select * from user;
show create view view1;
select * from view1;
create or replace view view1 as select id, name from user;
drop view if exists view1;
create view view1 as select * from user with cascaded check option;
create view view1 as select * from user with local check option;
create procedure p1()
begin
select count(*) from user;
end;
call p1();
select * from information_schema.ROUTINES where ROUTINE_SCHEMA = ‘user’;
show create procedure p1;
drop procedure if exists p1;
show global variables;
show session variables;
set session autocommit = 1;
select @@session.autocommit;
set @test = ‘test’;
select @test := “mysql”;
select @test;
drop procedure if exists test2;
create procedure test2()
begin
declare temp int default 0;
# 赋值
#set temp := 9;
# 将查询结果赋值给变量
select count(*) into temp from user;
select temp;
end;
drop procedure if exists test3;
create procedure test3()
begin
declare score int default 60;
declare result varchar(10);
if score = 100 then
set result := ‘A’;
elseif score >= 60 then
set result := ‘B’;
else
set result := ‘C’;
end if;
select result;
end;
drop procedure if exists test4;
create procedure test4(in score int, out result varchar(10))
begin
if score = 100 then
set result := ‘A’;
elseif score >= 60 then
set result := ‘B’;
else
set result := ‘C’;
end if;
#select result;
end;
call test4(78,@result);
select @result;
create procedure test5(in month int)
begin
declare result varchar(10);
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 := ‘第四季度’;
else
set result := ‘error’;
end case;
select concat('输入月份: ‘, month, ’ 判定为:’, result);
end;
call test5(9);
drop procedure if exists test6;
create procedure test6(in i int)
begin
declare sum int default 0;
while i <= 100 do
set sum := sum + i;
set i := i+1;
end while;
select sum;
end;
call test6(0);
select @sum;
drop procedure if exists test7;
create procedure test7(in i int)
begin
declare sum int default 0;
repeat
set sum := sum + i;
set i := i + 1;
until
i > 100
end repeat;
select sum;
end;
call test7(0);
drop procedure if exists test8;
create procedure test8(in i int)
begin
declare sum int default 0;
goto:loop
if i <= 0 thenleave goto;end if;if i%2 = 1 thenset i := i-1;iterate goto;end if;set sum := sum + i;set i := i-1;end loop goto;select sum;
end;
#10之内偶数之和
call test8(10);
#declare 游标名称 cursor for 查询语句 :定义游标
#open 游标名称 :打开游标
#fetch 游标名称 into 变量 :获取游标记录
#close 游标名称 :关闭游标
#条件处理程序
#declare handler_action handler for condition_value;
#handler_action: continue(继续执行当前程序)、exit(终止当前程序)
#condition_value:sqlstate(状态码)、sqlwarning(所有以01开头sqlstate代码的简写)、not found(所有以02开头的sqlstate代码的简写)、sqlexception(没有被sqlstate、not found捕获的代码的简写)
#使用条件处理程序解决游标跳出问题
drop procedure if exists test9;
create procedure test9(in n int)
begin
declare uname varchar(30);
declare uage int(3);
declare u_cursor cursor for select name,age from user where age <= n;
#02000是没数据
declare exit handler for sqlstate '02000' close u_cursor;drop table if exists user_pro;
create table if not exists user_pro(id int primary key auto_increment,name varchar(30),age int(3)
);open u_cursor;
while true dofetch u_cursor into uname,uage;insert into user_pro values (null,uname,uage);
end while;close u_cursor;
end;
call test9(10);
#存储函数是有返回值的存储过程,存储函数的参数只能是in类型
/**
create function 函数名称(参数)
returns type [characteristic…]
begin
return …;
end;
characteristic: determinstic(相同的参数总是产生相同的结果)、no sql(不包含sql)、reads sql data(包含读取数据的语句,不包含写入数据的语句)
*/
drop function if exists sumTest;
create function sumTest(n int)
returns int deterministic
begin
declare sum int default 0;
declare i int default 0;
while i <= n doset sum = sum+i;set i = i+1;
end while;return sum;
end
select sumTest(100);
show triggers;
drop table if exists user_logs;
create table if not exists user_logs(
id int(6) primary key auto_increment,
operation varchar(30) not null comment ‘操作’,
operation_time datetime not null comment ‘操作时间’,
operation_user varchar(30) not null comment ‘操作人’
)engine=innodb,default charset=utf8;
drop trigger if exists user_trigger;
create trigger user_trigger
after insert on user for each row
begin
insert into user_logs(id,operation,operation_time,operation_user) values
(null,‘insert’,now(),new.name);
end;
insert into user(id,name,age,email,type) values (null,‘wh’,3,‘3232’,‘student’);