USE testdb;
CREATE TABLE sch(
id INT PRIMARY KEY,
NAME VARCHAR(50) NOT NULL,
glass VARCHAR(50) NOT NULL
);
INSERT INTO sch VALUES
(1,'xiaoming','glass1'),
(2,'xiaojun','glass2');
SELECT * FROM sch;
DESC sch;
DELIMITER //
-- 创建一个存储函数用来统计表sch中的记录数
CREATE FUNCTION count_sch()
RETURNS INT
RETURN(SELECT COUNT(*) FROM sch);
//
-- 创建一个存储过程,通过调用存储函数的方法来获取表sch中的记录数和sch表中id的和
CREATE PROCEDURE add_id(OUT COUNT INT)
BEGIN
DECLARE itmp INT;
DECLARE cur_id CURSOR FOR SELECT id FROM sch;
DECLARE EXIT HANDLER FOR NOT FOUND CLOSE cur_id;
SELECT count_sch() INTO COUNT;
SET @sum=0;
OPEN cur_id;
REPEAT
FETCH cur_id INTO itmp;
IF itmp < 10
THEN SET @sum = @sum+itmp;
END IF;
UNTIL 0 END REPEAT;
CLOSE cur_id;
END//
-- 写个hello word的存储过程和函数
CREATE PROCEDURE helloword()
SELECT 'hello word';
//
CREATE FUNCTION helloword1()
RETURNS VARCHAR(20)
RETURN (SELECT 'hello word');
//
-- 写一个完整的包括参数、变量、变量赋值、条件判断、update语句、select返回结果集的存储过程
CREATE PROCEDURE plogin(p_username CHAR(15), p_password CHAR(32), p_ip CHAR(18), p_logintime DATETIME)
BEGIN
DECLARE v_uid MEDIUMINT(8);
DECLARE v_realpassword CHAR(32);
DECLARE v_nickname VARCHAR(30);
DECLARE v_oltime SMALLINT(6);
SELECT u.uid, u.password, f.nickname, u.oltime INTO v_uid, v_realpassword, v_nickname, v_oltime
FROM cdb_members u INNER JOIN cdb_memberfields f ON f.uid = u.uid WHERE u.username = p_username;
IF (v_uid IS NULL) THEN
SELECT 2 AS ErrorCode;
LEAVE LABEL_PROC;
END IF;
IF (p_password <> v_realpassword) THEN
SELECT 3 AS ErrorCode;
LEAVE LABEL_PROC;
END IF;
UPDATE ipsp_userexpands SET lastloginip = p_ip, lastlogintime = p_logintime WHERE uid = v_uid;
SELECT 0 AS ErrorCode, v_uid AS uid, v_nickname AS nickname, v_oltime AS oltime;
END //
-- 创建一个执行动态sql的存储过程
CREATE PROCEDURE ipsp_getresourcedir(p_hashcode CHAR(40))
BEGIN
DECLARE v_sql VARCHAR(200);
SET v_sql = CONCAT('SELECT filedir FROM ipsp_resources WHERE hashcode ='', p_hashcode, '' LIMIT 0, 1');
SET @sql = v_sql;
PREPARE sl FROM @sql;
EXECUTE sl;
DEALLOCATE PREPARE sl;
END //
-- 创建程序员工资表
CREATE TABLE proWage(
id INT PRIMARY KEY AUTO_INCREMENT,
PName CHAR(10),
Wage INT
);//
-- 插入数据--
INSERT INTO ProWage(PName,Wage)VALUES('来棱',1700)//
INSERT INTO ProWage(PName,Wage)VALUES('张三',1200)//
INSERT INTO ProWage(PName,Wage)VALUES('李四',1800)//
INSERT INTO ProWage(PName,Wage)VALUES('二月',3500)//
INSERT INTO ProWage(PName,Wage)VALUES('蓝天',2780)//
-- 创建一个存储过程,对程序员的工资进行分析,月薪1500到10000不等,
-- 如果有百分之五十的人薪水不到2000元,给所有人加薪,每次加100,再
-- 进行分析,直到有一半以上的人大于2000元为止,存储过程执行完后,最终加了多少钱?
-- 1)创建存储过程,查询是否有一半程序员的工资在2200、3000、3500、4000、5000或6000元之上,
-- 如果不到分别每次给每个程序员加薪100元,至之一半程序员的工资达到2200,3000,3500,4000,
-- 5000或6000元。
CREATE PROCEDURE pw2(money INT)
BEGIN
DECLARE sum1 INT;
DECLARE count1 INT;
DECLARE num INT DEFAULT 0;
DECLARE percent DOUBLE DEFAULT 0;
REPEAT
SELECT COUNT(*) INTO sum1 FROM proWage;
SELECT COUNT(*) INTO count1 FROM proWage WHERE wage < money;
SET percent := count1/sum1;
IF percent >= 0.5 THEN
UPDATE proWage SET wage := wage + 100;
SET num := num + 1;
END IF;
UNTIL percent < 0.5 END REPEAT;
SELECT sum1;
SELECT num;
SELECT sum1 * num * 100;
END//
-- 2)创建存储过程,查询程序员平均工资在4500元,如果不到则每个程序员每次加200元,
-- 至到所有程序员平均工资达到4500元。
CREATE PROCEDURE pw3()
BEGIN
DECLARE avg1 INT;
DECLARE num INT DEFAULT 0;
REPEAT
SELECT AVG(wage) INTO avg1 FROM proWage;
IF avg1 < 4500 THEN
UPDATE proWage SET wage := wage + 200;
SET num := num + 1;
END IF;
UNTIL avg1 > 4500 END REPEAT;
SELECT num;
SELECT (SELECT COUNT(*) FROM prowage) * num * 200;
END//
CREATE TABLE Member -- 学生表
(
MID CHAR(10) PRIMARY KEY, -- 学生号
MName CHAR(50) NOT NULL -- 姓名
)//
CREATE TABLE F -- 课程表
(
FID CHAR(10) PRIMARY KEY, -- 课程号
FName CHAR(50) NOT NULL -- 课程名
)//
CREATE TABLE score -- 学生成绩表
(
SID INT PRIMARY KEY AUTO_INCREMENT, -- 成绩记录号
FID CHAR(10), -- 课程号
MID CHAR(10), -- 学生号
Score INT NOT NULL, -- 成绩
CONSTRAINT fk_score1 FOREIGN KEY(FID) REFERENCES F(FID),
CONSTRAINT fk_score2 FOREIGN KEY(MID) REFERENCES Member(MID)
)//
-- 课程表中插入数据--
INSERT INTO F(FID,FName)VALUES('F001','语文');
INSERT INTO F(FID,FName)VALUES('F002','数学');
INSERT INTO F(FID,FName)VALUES('F003','英语');
INSERT INTO F(FID,FName)VALUES('F004','历史');
-- 学生表中插入数据--
INSERT INTO Member(MID,MName)VALUES('M001','张萨');
INSERT INTO Member(MID,MName)VALUES('M002','王强');
INSERT INTO Member(MID,MName)VALUES('M003','李三');
INSERT INTO Member(MID,MName)VALUES('M004','李四');
INSERT INTO Member(MID,MName)VALUES('M005','阳阳');
INSERT INTO Member(MID,MName)VALUES('M006','虎子');
INSERT INTO Member(MID,MName)VALUES('M007','夏雪');
INSERT INTO Member(MID,MName)VALUES('M008','璐璐');
INSERT INTO Member(MID,MName)VALUES('M009','珊珊');
INSERT INTO Member(MID,MName)VALUES('M010','香奈儿');
-- 成绩表中插入数据--
INSERT INTO Score(FID,MID,Score)VALUES('F001','M001',78);
INSERT INTO Score(FID,MID,Score)VALUES('F002','M001',67);
INSERT INTO Score(FID,MID,Score)VALUES('F003','M001',89);
INSERT INTO Score(FID,MID,Score)VALUES('F004','M001',76);
INSERT INTO Score(FID,MID,Score)VALUES('F001','M002',89);
INSERT INTO Score(FID,MID,Score)VALUES('F002','M002',67);
INSERT INTO Score(FID,MID,Score)VALUES('F003','M002',84);
INSERT INTO Score(FID,MID,Score)VALUES('F004','M002',96);
INSERT INTO Score(FID,MID,Score)VALUES('F001','M003',70);
INSERT INTO Score(FID,MID,Score)VALUES('F002','M003',87);
INSERT INTO Score(FID,MID,Score)VALUES('F003','M003',92);
INSERT INTO Score(FID,MID,Score)VALUES('F004','M003',56);
INSERT INTO Score(FID,MID,Score)VALUES('F001','M004',80);
INSERT INTO Score(FID,MID,Score)VALUES('F002','M004',78);
INSERT INTO Score(FID,MID,Score)VALUES('F003','M004',97);
INSERT INTO Score(FID,MID,Score)VALUES('F004','M004',66);
INSERT INTO Score(FID,MID,Score)VALUES('F001','M006',88);
INSERT INTO Score(FID,MID,Score)VALUES('F002','M006',55);
INSERT INTO Score(FID,MID,Score)VALUES('F003','M006',86);
INSERT INTO Score(FID,MID,Score)VALUES('F004','M006',79);
INSERT INTO Score(FID,MID,Score)VALUES('F002','M007',77);
INSERT INTO Score(FID,MID,Score)VALUES('F003','M008',65);
INSERT INTO Score(FID,MID,Score)VALUES('F004','M007',48);
INSERT INTO Score(FID,MID,Score)VALUES('F004','M009',75);
INSERT INTO Score(FID,MID,Score)VALUES('F002','M009',88);
-- 1)查询各个学生语文、数学、英语、历史课程成绩,例如下表:
SELECT m.MName AS 姓名,
SUM(CASE F.FName WHEN '语文' THEN s.Score END) 语文,
SUM(CASE F.FName WHEN '数学' THEN s.Score END) 数学,
SUM(CASE F.FName WHEN '英语' THEN s.Score END) 英语,
SUM(CASE F.FName WHEN '历史' THEN s.Score END) 历史
FROM Score s INNER JOIN Member m ON s.mid = m.mid
INNER JOIN F ON s.fid = f.fid GROUP BY m.MName;
-- 2)查询四门课中成绩低于70分的学生及相对应课程名和成绩。
SELECT m.mname AS 姓名, f.fname AS 课程名, s.score AS 成绩
FROM Score s
INNER JOIN Member m ON s.mid = m.mid
INNER JOIN F ON s.fid = f.fid
WHERE s.score < 70;
-- 3)统计各个学生参加考试课程的平均分,且按平均分数由高到底排序。
SELECT mname AS 姓名, AVG(score) AS 平均分
FROM Score s
INNER JOIN Member m ON s.mid = m.mid
INNER JOIN F ON s.fid = f.fid
GROUP BY 姓名
ORDER BY 平均分 DESC;
-- 4)创建存储过程,分别查询参加1、2、3、4门考试及没有参加考试的学生名单,要求显示姓名、学号。
CREATE PROCEDURE search(num INT)
BEGIN
SELECT mname AS 姓名, m.mid AS 学号,COUNT(*) AS 参加考试数
FROM Score s
INNER JOIN Member m ON s.mid = m.mid
INNER JOIN F ON s.fid = f.fid
GROUP BY 姓名
HAVING COUNT(*) = num;
END;//
CALL search(2);
本文链接:https://blog.csdn.net/oneeyear/article/details/83018708