mysql练习

1.写一个完整的包括参数、变量、变量赋值、条件判断、UPDATE语句、SELECT返回结果集的存储过程。
CREATE table zy3
(
	id int,
    name varchar(11),
    at int 
);

insert into zy3 values(1,'haha',3),(2,'hehe',2),(3,'hoho',1);

select * from zy3;



delimiter //
create procedure a (in at int,in name varchar(11),out id int)
begin
	update a set at = at where name = name;
	select id into id from a where name = name;
end //
delimiter ;

call a (1,'haha',@id);
select @id;

2.创建一个执行动态SQL的存储过程。
create table students
(
    id int,
	name varchar(11),
    class int
);
 
insert into students values(1,'zs',2),(2,'ls',1),(3,'ww',3);
 
select * from students;


delimiter //
create procedure name_class (in nm varchar(11),out bj int)
begin
	if nm not in ('zs','ls','ww') 
	then select 'not found';
	update students set class = 1 where name = 'zs';
	else select class into bj from students where name = nm;
	end if;
end //
delimiter ;


call name_class ('zs',@bj);
select @bj;


call name_class ('xxx',@bj);
select class from students where name = 'zs';

3.创建实现功能相同的存储函数,比较他们之间的不同点在什么地方?

        1.存储函数有且只有一个返回值,而存储过程可以有多个返回值,也可以没有返回值。

        2.存储函数只能接受输入参数,而存储过程可以接受输入、输出、输入输出参数。

        3.存储过程可以调用存储函数、但函数不能调用存储过程。

        4.存储过程中的语句功能更强大,存储过程可以实现很复杂的业务逻辑,而函数有很多限制,如不能在函数中使用insert,update,delete,create等语句;

        5.存储过程一般是作为一个独立的部分来执行(call调用)。而函数可以作为查询语句的一个部分来调用。

        6.存储函数只完成查询的工作,可接受输入参数并返回一个结果,也就是函数实现的功能针对性比较强。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值