Create table student
(Id varchar(255), #学号
Name varchar(255), #姓名
Roomid varchar(255), #班级
Sex char(1), #性别
Degree varchar(3) #成绩);
Insert into student values('1','amy','8','0','65');
Insert into student values('2','bob','4','1','80');
Insert into student values('3','candy','6','0','70');
Insert into student values('4','danny','8','1','90');
Insert into student values('5','edward','8','1','95');
Insert into student values('6','frank','7','1','100');
1:创建一个存储过程,实现查询表student中的记录信息,并执行存储过程
delimiter //
drop procedure if exists aaaa;
CREATE procedure aaaa()
begin
SELECT * from student;
END
//
call aaaa()
2:创建一个存储过程,根据给定的学生学号返回该学生的姓名
delimiter //
drop procedure if EXISTS bbbb;
CREATE procedure bbbb(in n int)
begin
SELECT `Name` from student where Id=n;
END
//
call bbbb(1)
3:创建一个存储过程,根据所给的班级的编号,统计该班人数,并将人数以输出变量返回给用户。
delimiter//
-- drop PROCEDURE if EXISTS stu_count;
-- CREATE PROCEDURE stu_count(in rid INT,OUT cb int )
-- BEGIN
-- SELECT COUNT(room_id) INTO cb FROM student3 WHERE room_id = rid;
-- end
-- //
--
-- SET @rid = 8;
-- call stu_count(@rid,@cb);
--SELECT @cb;
4:创建一个存储过程,根据所给的学号查询该学生的考试分数是否超过了85分, 若超过则输出“ 考得好”, 否则输出 “考得不好”。
方法一:
delimiter //
drop procedure if exists cun4;
create procedure cun4 (in x int)
begin
if (select degree from student1 where id=x)>85 then
select '考得好';
else
select '考的不好';
end if;
end //
call cun4(2);
方法二:
delimiter //
drop procedure if exists st_04;
create procedure st_04(in x int )
BEGIN
declare i int default (select degree from student where id=x );
if i>85 THEN
select '考得好';
else
select '考得不好';
end if;
END
//
call st_04(6);
方法三:
-- -- delimiter //
-- -- drop procedure if exists d;
-- -- create procedure d(in n int,out m int)
-- -- begin
-- -- select Degree into m from student001 where Id=n;
-- -- if m>=85 THEN
-- -- select '考得好';
-- -- else
-- -- select '考得不好';
-- -- end if;
-- -- end
-- -- //
call d(6,@m);
方法四:
-- delimiter //
-- drop procedure if exists d;
-- create procedure d(in n int,out m int)
-- begin
-- select Degree into m from student001 where Id=n;
-- select if( m>85 ,'考得好','考的不好');
-- end
-- //
call d(2,@m);
实验5:创建一个存储过程,对表student增加两条记录。
方法一:
delimiter //
drop procedure if EXISTS eeee;
CREATE procedure eeee(in n int)
BEGIN
insert into student VALUES(n,n,n,n,n);
insert into student VALUES(n,n,n,n,n);
END
//
call eeee(1);
SELECT * from student
方法二:
delimiter //
drop procedure if exists stu;
create procedure stu()
begin
declare i int DEFAULT (select count(*) from student);
insert into student(id) values(i+1);
insert into student(id) values(i+2);
select * from student ;
end;
//
call stu ();
6:请撰写一个存储过程,输出各班学生的平均成绩。
方法一: 直接查询出来(平均成绩)
delimiter //
drop procedure if EXISTS ffff;
CREATE procedure ffff()
begin
SELECT Roomid,avg(Degree) from student GROUP BY Roomid;
END
//
CALL ffff()
方法二:
7:请撰写一个存储过程,能根据用户输入的学号,输出学生的姓名、性别到两个参数变量中,以供其它程序使用。
delimiter //
drop procedure if exists gg;
create procedure gg(in n int,out m varchar(255),out l varchar(255))
BEGIN
select name into m from student where id=n;
select sex into l from student where id=n;
END
//
call gg(1,@m,@l);
select @m,@l
8:撰写一个存储过程,根据用户输入的学号,输出学生的姓名、年龄两个参数到新表temp中
场景一:你已经建了一个新表temp
场景二:在存储中建新表temp
两张表的字段一致,插入数据:
方法一:insert into 目标表 select * from 来源表;#插入全部数据
方法二:insert into 目标表(字段 )select 字段1,字段2 from 来源表。
场景一:
1.1
#create table temp(sex varchar(255),degree varchar(255));
#SELECT * FROM temp ;
-- delimiter //
-- drop procedure if exists hh;
-- create procedure hh(in n int)
-- BEGIN
-- insert into temp(sex,degree) select sex,degree from student where id=n ;
-- END//
#call hh(4)
#select * from temp ;
1.2
delimiter //
drop procedure if exists hh;
create procedure hh(in n int,out a char(20), out b char(20))
BEGIN
select sex ,degree into a,b from student001 where id=n ;
insert into temp (sex,degree)values (a,b) ;
END//
call hh(2,@a,@b);
select @a,@b;
select * from temp ;
--
场景二:在存储中建新表temp
delimiter //
drop procedure if exists hh;
create procedure hh(in n int,out a char(20), out b char(20))
BEGIN
drop table if exists temp ;
create table temp(sex varchar(255),degree varchar(255));
select sex ,degree into a,b from student where id=n ;
insert into temp (sex,degree)values (a,b) ;
END//
call hh(3,@a,@b);
select @a,@b;
select * from temp ;
select * from student ;
9:请撰写一个存储过程,求1+2+3+…x的值。
方法一:
delimiter //
drop procedure if exists iiii;
create procedure iiii(in n int)
begin
declare a int;
set a=(n+1)*n/2;
SELECT a;
END
//
方法二:
delimiter //
drop procedure if exists st_9;
create procedure st_9(in x int)
BEGIN
declare i int default 1;
declare s int default 0;
while (i<x+1)do
set s=s+i;
set i=i+1;
end while;
select s;
END
//
call st_9(100)
9.1求1+2+3+…x的奇数和。
delimiter //
drop procedure if exists st_9;
create procedure st_9(in x int)
BEGIN
declare i int default 1;
declare s int default 0;
while (i<x+1)do
set s=s+i;
set i=i+2;
end while;
select s;
END
//
call st_9(100)
9.2求1+2+3+…x的偶数和
delimiter //
drop procedure if exists st_9;
create procedure st_9(in x int)
BEGIN
declare i int default 0;
declare s int default 0;
while (i<x+1)do
set s=s+i;
set i=i+2;
end while;
select s;
END
//
call st_9(100)