MySQL练习

一、写一个完整的包括参数、变量、变量赋值、条件判断、UPDATE语句、SELECT返回结果集的存储过程。

二、创建一个执行动态SQL的存储过程。

1.进入MySQL

2.查看fruits

3.查看数据库

4.进入zhuzhuxia数据库并查看表的数量

5.查看表的内容

6.创建存储过程

(1)参数

delimiter //

create procedure countproc1 (in sid int,out num int)

begin

    select count(*) into num from fruits where s_id = sid;

end //

delimiter ;

(2)变量赋值

delimiter //

    create procedure countproc2  (in sid int,out num int)

begin

    declare cnt int default 0 ;

    select count(*) into num from fruits where s_id = sid;

    set cnt = 100 ;

    select cnt ;

end //

delimiter ;

(3)条件判断

delimiter //

create procedure countproc3  (in sid int,out num int)

begin

    declare cnt int default 0 ;

    declare tmp int default 0 ;

    select count(*) into num from fruits where s_id = sid;

    set cnt = 100 ,tmp =0;

    select cnt ;

    set tmp = cnt + 1;

     if tmp > 100 then

       select tmp;

    else

       select cnt;

    end if;

end //

delimiter ;

(4)UPDATE语句

delimiter //

     create procedure countproc4  (in sid int,out num int)

begin

    declare cnt int default 0 ;

    declare tmp int default 0 ;

         select count(*) into num from fruits where s_id = sid;

      set cnt = 100 ,tmp =0;

      select cnt ;

          set tmp = cnt + 1;

     if tmp > 100 then

       select tmp;

    else

       select cnt;

    end if;

       update fruits set f_name = 'OK' where f_id = 'a' ;

       select * from fruits;

end //

delimiter ;

(5)正常调用

call countproc1(101,@num);

删除 countproc3

drop procedure countproc3;

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

1.存储过程是用来执行一系列的SQL语句并返回结果集,而存储函数是用来返回单一值;

2. 存储过程可以包含输入和输出参数,而存储函数只能包含输入参数;

3. 存储过程可以包含数据操作语言(DML)语句,比如INSERT、UPDATE、DELETE,而存储函数不能包含这些语句;

4. 存储函数可以在SELECT语句中使用,而存储过程不能直接在SELECT语句中使用。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值