存储相关题目
实验要求:
- 理解存储过程的概念
- 掌握存储过程的语法格式、使用方法
- 掌握存
储过程的创建、执行
二、实验前提:
drop table if exists student;
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)返回值 就是要输出 out
(2)显示考的好,不好 select "考的好,不好"
1:创建一个存储过程,实现查询表student中的所有信息,并执行存储过程
delimiter//
create PROCEDURE ck()
BEGIN
SELECT* from student;
end//
call ck();
2:创建一个存储过程,根据给定的学生学号显示学生的姓名
delimiter//
drop PROCEDURE if EXISTS ck;
create PROCEDURE ck(in x int(10))
BEGIN
SELECT NAME from student where id=x;
end//
call ck(2);
3:创建一个存储过程,根据所给的班级的编号,统计该班人数,并将人数以输出变量返回给用户。
delimiter//
drop procedure if exists stu;
create procedure stu(x int(10),out y int(10))
begin
select count(*) into y from student where Roomid=x;
end//
call stu(8,@y);
select @y;
4:创建一个存储过程,根据所给的学号查询该学生的考试分数是否超过了85分, 若超过则输出“ 考得好”, 否则输出 “考得不好”。
delimiter//
drop PROCEDURE if EXISTS ck;
create PROCEDURE ck(in x int(10), out y int(10))
BEGIN
SELECT Degree into y from student where id=x;
if y>85 then
select "考的好";
ELSE
SELECT"考得不好";
end if;
end//
call ck(2,@y);
select @y;
实验5:创建一个存储过程,对表student增加两条记录。
delimiter //
drop PROCEDURE if EXISTS a1;
create procedure a1()
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 a1()
6:请撰写一个存储过程,输出各班学生的平均成绩。
delimiter//
DROP PROCEDURE if EXISTS ave;
CREATE PROCEDURE ave()
BEGIN
SELECT avg(Degree) FROM student GROUP BY Roomid;
END//
call ave();
7:请撰写一个存储过程,能根据用户输入的学号,输出学生的姓名、性别到两个参数变量中,以供其它程序使用。
delimiter//
drop procedure if exists stu;
create procedure stu(x int(10),out y1 varchar(20),out y2 int(10))
begin
select Name,Sex into y1,y2 from student where Id=x;
end//
call stu(2,@y1,@y2);
select @y1,@y2;
8:撰写一个存储过程,根据用户输入的学号,输出学生性别,成绩两个参数放到新表temp中Create table temp(Sex varchar(255),Degree varchar(255));
两张表的字段一致,插入数据:
方法一:insert into 目标表 select * from 来源表;#插入全部数据
方法二:insert into 目标表(字段 )select 字段1,字段2 from 来源表。
delimiter//
drop procedure if exists stu;
create procedure stu(x int(10))
begin
create table temp(Sex varchar(255),Degree varchar(255));
insert into temp(Sex,Degree) select Sex,Degree from student where Id=x;
end//
call stu(2);
select * from temp;
9:请撰写一个存储过程,求1+2+3+…x的值。
delimiter//
DROP PROCEDURE if EXISTS sn;
CREATE PROCEDURE sn(inout x int(10))
BEGIN
DECLARE i int DEFAULT 1;
DECLARE j int DEFAULT 0;
WHILE (i<=x) DO
set j=j+i;
set i=i+1;
end WHILE;
SET x=j;
end//
set @x=4;
call sn(@x);
SELECT @x;
请撰写一个存储过程,求0+2+4+…x的值。
delimiter//
drop procedure if exists stu;
create procedure stu(x int(10))
begin
declare i int(10) default 1; #开始值
declare j int(10) default 0; #结果
while (i<=x) do
set j=j+i; #相加
set i=i+2; #递增1
end while;
select j; #显示结果
end//
call stu(10);
请撰写一个存储过程,求1+3+5+…x的值。
delimiter//
drop procedure if exists stu;
create procedure stu(x int(10))
begin
declare i int(10) default 0; #开始值
declare j int(10) default 0; #结果
while (i<=x) do
set j=j+i; #相加
set i=i+2; #递增1
end while;
select j; #显示结果
end//
call stu(10);
浙公网安备 33010602011771号