存储过程及函数

课程名称

MySQL数据库技术


实验

成绩

实验名称

实验七:存储过程及函数

学号

 辅导老师;陶荣

姓名

班级

日期

实验目的:

1. 掌握变量的分类及其使用;掌握各种运算符的使用;掌握系统内置函数的使用;

2. 掌握流程控制语言的使用;

3. 掌握存储过程的创建和调用方法;

4. 掌握函数的创建和调用方法。

实验平台:

MySQL+SQLyog;

实验内容与步骤:

以下操作均在YGGL数据库中进行。

一、MySQL语言

1. 计算194和142的乘积。

2. 显示这串字母的值‘I\nlove\nMySQL’。

3. 获得现在使用的MySQL版本。

4. 或得系统当前的时间。

5. 创建一个名为female的用户变量,并在select语句中,使用该局部变量查找表中所有女职工的编号、姓名。

6. 定义一个用户变量,用于获取号码为102201的员工的电话号码,并显示该变量的值。

7. 查询salary表中员工000001的实际收入。

8. 获得3.7456的四舍五入的整数值。

9. 获得-23的绝对值。

10. 获得9的平方根。

11. 查询市场部雇员的总人数。

12. 求市场部收入最高的员工姓名。

13. 连接两个字符串‘I love’,’MySQL’。

14. 获取字符串‘abcdef’左边开始的3个字符。

15. 获取当前的日期和时间。

16. 获得员工000001的员工的出生年份。

二、存储过程的应用

1. 创建一个带输入参数K的存储过程,功能是求出1+2+...+K的累加和。

2. 创建一个带输出参数的存储过程,功能是获取Employees表中的员工人数,并调用这个存储过程。

3. 创建存储过程,比较两个员工的实际收入,若前者比后者高就输出0,否则输出1,并调用该存储过程。

4. 创建一个存储过程,要求当一个员工的工作年份大于6年时将其转到经理办公室工作。

5. 创建存储过程,使用游标计算本科及其以上学历的员工在总员工数中所占的比例。

6. 创建一个存储过程,判断指定员工的实际收入状况,当实际收入在2000元以下时,显示‘低收入’,当实际收入在2000-5000元时,显示‘中等收入’,当实际收入在5000元以上时,显示‘高收入’,否则显示‘无’。

三、函数的应用

1. 创建一个带输入参数K的函数,返回1+2+...+K的累加和。

2. 创建一个函数,返回员工的总人数,并调用该函数。

3. 创建一个函数,删除在salary表中有但在Employees表中不存的员工号。若在Employees表中存在返回false,若不在则删除该员工号并返回true,并调用该函数。

4. 创建函数,判断员工是否在研发部工作,若是则返回其学历,若不是则返回字符串‘NO’。

5. 创建一个函数,将工作时间满4年的员工收入增加500元。

实验总结(结论或问题分析):

select 194*142 as 乘积;
select 'I\nlove\nMySQL' 字符串, length('I\nlove\nMySQL') zz;
select version();
select now();
use yggl;
 
/**DECLARE @fe char(79)default 500;
set @fe=0;
select 姓名,编号 into @fe  from employees 
where 性别=fe;*/
set @ff  =0;
select 姓名,编号  from employees 
where 性别=@ff;
set @cc=000001;
select 电话 from employees where 编号=@cc;
/**declare @balance FLOAT
set @balance=(select(收入-支出) as 余额  from salary where 编号='000001');
select @balance;*/
set @balanc=(select(收入-支出) as 余额  from salary where 编号='000001');
select @balanc;
select round(3.7456);
select abs(-23);
select sqrt(9);
set @sum=(select COUNT(*) 总人数 from `departments`d,employees e where d.部门号=e.部门号 and e.部门号=5);
select @sum;
set @best=(select 姓名 FROM salary s,employees e ,  departments d   where 收入=(select Max(收入) from salary s  )  and e.`编号`=s.`编号` and d.`部门号`=e.`部门号`);
select @best;
select concat('i love','mysql');
select 'abcdef',left ('abcdef',3);
select NOW(),CURRENT_TIMESTAMP(), LOCALTIME(), SYSDATE();
select YEAR(出生日期) from employees where 编号='000001';

二
use yggl;
DELIMITER $$
create PROCEDURE t2(in k int(4),out sum1 int )
begin
     declare i int;
     set i=1;
     set sum1=0;
     while i<=k do
     set sum1=sum1+i;
     set i=i+1;
     end while;
END $$ 
DELIMITER ;  
call t2(5,@sum1); 




DELIMITER $$
create PROCEDURE xa(out k int)
begin
    -- declare w int;
    set k=(select count('编号') FROM employees);
END $$    
DELIMITER ;
call xa(@w)
SELECT @w;
DELIMITER $$
create PROCEDURE yy(in o double,in a double,out str1 char(1))
begin
    declare u double;
    declare p double;
        set u=(select (收入-支出)   from salary where 编号=o);
        set p=(select (收入-支出)   from salary where 编号=a);
        if u>p then
        SET str1='0';
        else
        set str='1';
        END if;


END $$    
DELIMITER ;
call  yy('000001','000008',@str1);
SELECT @str1;
DELIMITER $$
create PROCEDURE pl(in bh varchar(19))
begin
declare sj int;
declare bmh int(4);
select 工作时间 into sj from employees where 编号=bh;
select 部门号 into bmh from departments
                  where 部门名称='市场部' ;
if   sj>6 then
    update  employees
    set 部门号=bmh where 编号=bh;
end if;
END $$    
DELIMITER ;
call pl('000001');
select * from employees;




DELIMITER $$
create PROCEDURE x(out tongji float)
begin
   declare zongshu int;
   declare number int;
   declare found1 boolean;
   declare xueli char(4);
    declare  cur_1  cursor for select 学历 from employees;
    declare  continue handler for not found set found1=false ;
           
    set found1=true;
    set number=0;
    open cur_1;
    fetch cur_1 into xueli;
    while  found1 do 
         if  xueli='本科' or xueli='硕士' then
            set number=number+1;
         end if;
       fetch cur_1 into xueli;
    end while;
    select count(*)  into zongshu from employees;
    set tongji=number/zongshu;
  END $$    
DELIMITER ;


call x(@tongji)
select @tongji
DELIMITER $$              
create PROCEDURE  p(in  bh  varchar(19))
begin
    select 收入-支出 实际收入  , case
                     when 收入-支出<2000 then '低收入'
                     when 收入-支出>=2000 and 收入-支出<=5000 then '中等收入'
                      when 收入-支出>5000 then '高收入'
                      end   收入等级
                      from salary where 编号=bh;
         END $$                    
DELIMITER ;
    
    
 call p ('000001');
DELIMITER  $$
CREATE FUNCTION tc (k int(4))
RETURNS int (4)
begin
declare m int(4);
declare si int(4);
     set  m=1;
     set si=0;
     while m<=k do
     set si=si+m;
     set m=m+1;
     end while;
    RETURN si;
     END $$    
DELIMITER ;
select tc(5);
--成功--
CREATE FUNCTION jk()
RETURNS INTEGER 
    
    RETURN(select count(*)  FROM employees);
    
SELECT jk();
  --成功--
DELIMITER $$
CREATE FUNCTION DE( EMu CHAR(6))
	RETURNS BOOLEAN
BEGIN 
	DECLARE EMp CHAR(10);
	SELECT 姓名 INTO EMp FROM employees WHERE 编号 =EMu;
	IF EMp IS NULL THEN
		DELETE FROM salary WHERE  编号 =EMu;
		RETURN TRUE;
	ELSE
		RETURN FALSE;
	END IF;
END$$
DELIMITER ;
SELECT DE('123456');
select * from salary;






DELIMITER $$
CREATE FUNCTION ik( id VARCHAR(19))
RETURNS VARCHAR(20)
begin
      declare bmh VARCHAR (20);
      set  bmh=(select 部门名称  from departments d, employees e
where e.编号=id limit 1);
if bmh='研发部' then RETURN(select 学历 from employees where 编号=id); 
ELSE RETURN 'no';
 END if;
END $$
 DELIMITER ;
SELECT ik ('000008')
DELIMITER $$
CREATE FUNCTION UII()
RETURNS DOUBLE
begin
 UPDATE salary set 收入 = 收入+500 where 编号 in(SELECT 编号 from employees where 工作时间>4);
 return 0.0; 
END $$ 
DELIMITE ;
SELECT UII();
SELECT * FROM salary
总结存储函数只有输入参数(不用加in关键字),没有输出参数,并且返回值只能有一个,不能返回结果集,调用时直接使用即可,不需加call, 









 

 

 

 

猜你喜欢

转载自blog.csdn.net/zs_pnzzz/article/details/80301608
今日推荐