数据库初始化:
CREATE DATABASE school; -- 创建表:course CREATE TABLE `course` ( `cno` VARCHAR (12) NOT NULL DEFAULT ‘‘, `cname` VARCHAR (12) NOT NULL, `cpno` VARCHAR (12) DEFAULT NULL, `ccredit` INT (11) NOT NULL, PRIMARY KEY (`cno`) ) ENGINE = INNODB DEFAULT CHARSET = utf8; INSERT INTO `course` VALUES (‘1‘, ‘java‘, ‘2‘, ‘3‘); INSERT INTO `course` VALUES (‘2‘, ‘c++‘, ‘4‘, ‘2‘); INSERT INTO `course` VALUES (‘3‘, ‘数学‘, NULL, ‘5‘); INSERT INTO `course` VALUES (‘4‘, ‘c‘, ‘3‘, ‘3‘); INSERT INTO `course` VALUES (‘5‘, ‘英语‘, NULL, ‘5‘); INSERT INTO `course` VALUES (‘6‘, ‘数据结构‘, ‘3‘, ‘3‘); INSERT INTO course VALUES (‘7‘, ‘c‘, ‘3‘, ‘5‘); DELETE FROM course WHERE cno = ‘7‘; CREATE TABLE `student` ( `id` INT (11) NOT NULL, `sno` VARCHAR (12) NOT NULL, `sname` VARCHAR (12) NOT NULL, `ssex` VARCHAR (1) DEFAULT ‘女‘, `sage` INT (11) NOT NULL, `sdept` VARCHAR (12) NOT NULL, `sprovince` VARCHAR (255) DEFAULT NULL, `scity` VARCHAR (255) DEFAULT NULL, `sstreet` VARCHAR (255) DEFAULT NULL, `sbirthday` date DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `sno` (`sno`), FULLTEXT KEY `index_name` (`sname`) ) ENGINE = INNODB DEFAULT CHARSET = utf8; INSERT INTO `student` VALUES ( ‘2‘, ‘002‘, ‘李丽‘, ‘女‘, ‘19‘, ‘数学系‘, ‘江苏‘, ‘苏州‘, ‘长桥‘, ‘1997-07-18‘ ); INSERT INTO `student` VALUES ( ‘3‘, ‘003‘, ‘李彤‘, ‘女‘, ‘22‘, ‘数学系‘, ‘江苏‘, ‘南京‘, ‘解放‘, ‘1994-08-02‘ ); INSERT INTO `student` VALUES ( ‘4‘, ‘004‘, ‘张天宇‘, ‘男‘, ‘25‘, ‘计算机科学与技术系‘, ‘上海‘, ‘上海‘, ‘仁义‘, ‘1991-07-07‘ ); INSERT INTO `student` VALUES ( ‘5‘, ‘005‘, ‘李大奎‘, ‘男‘, ‘20‘, ‘计算机科学与技术系‘, ‘浙江‘, ‘杭州‘, ‘东营‘, ‘1996-02-08‘ ); INSERT INTO `student` VALUES ( ‘6‘, ‘006‘, ‘张思源‘, ‘男‘, ‘27‘, ‘数学系‘, ‘广东‘, ‘深圳‘, ‘华强‘, ‘1989-02-06‘ ); INSERT INTO `student` VALUES ( ‘7‘, ‘007‘, ‘tony‘, ‘男‘, ‘20‘, ‘中文‘, ‘浙江‘, ‘杭州‘, ‘东营‘, ‘1996-05-29‘ ); INSERT INTO `student` VALUES ( ‘9‘, ‘0l9‘, ‘nicy‘, ‘女‘, ‘21‘, ‘中文‘, NULL, NULL, NULL, NULL ); CREATE TABLE `sc` ( `id` INT (11) NOT NULL AUTO_INCREMENT, `sno` VARCHAR (12) NOT NULL, `cno` VARCHAR (12) NOT NULL, `grade` INT (11) NOT NULL, PRIMARY KEY (`id`), KEY `sno` (`sno`), KEY `cno` (`cno`), CONSTRAINT `sc_ibfk_1` FOREIGN KEY (`sno`) REFERENCES `student` (`sno`), CONSTRAINT `sc_ibfk_2` FOREIGN KEY (`cno`) REFERENCES `course` (`cno`) ) ENGINE = INNODB AUTO_INCREMENT = 9 DEFAULT CHARSET = utf8; INSERT INTO `sc` VALUES (‘1‘, ‘002‘, ‘2‘, ‘50‘); INSERT INTO `sc` VALUES (‘2‘, ‘002‘, ‘3‘, ‘70‘); INSERT INTO `sc` VALUES (‘3‘, ‘003‘, ‘2‘, ‘30‘); INSERT INTO `sc` VALUES (‘4‘, ‘005‘, ‘3‘, ‘95‘); INSERT INTO `sc` VALUES (‘5‘, ‘003‘, ‘4‘, ‘100‘); INSERT INTO `sc` VALUES (‘6‘, ‘002‘, ‘4‘, ‘90‘); INSERT INTO `sc` VALUES (‘7‘, ‘004‘, ‘4‘, ‘70‘); INSERT INTO `sc` VALUES (‘8‘, ‘005‘, ‘4‘, ‘90‘); CREATE TABLE `test` ( `id` VARCHAR (255) NOT NULL, `name` VARCHAR (255) NOT NULL, `birthday` date DEFAULT NULL ) ENGINE = INNODB DEFAULT CHARSET = utf8; INSERT INTO `test` VALUES (‘001‘, ‘tom‘, NULL); INSERT INTO `test` VALUES (‘002‘, ‘tony‘, NULL); INSERT INTO `test` VALUES (‘0l3‘, ‘helen‘, NULL); INSERT INTO `test` VALUES (‘003‘, ‘jack‘, ‘2015-08-17‘); INSERT INTO `test` VALUES (‘004‘, ‘jack‘, ‘2015-03-05‘); INSERT INTO `test` VALUES (‘005‘, ‘jack‘, ‘2015-03-05‘);
创建函数:
CREATE FUNCTION fun_getage(str varchar(10)) -- 函数必须带返回值 RETURNS VARCHAR(10) BEGIN RETURN (SELECT sage FROM student WHERE sname=str); END;
调用函数:
SELECT fun_getage(‘李丽‘);
output:
19
在函数中声明变量:
CREATE FUNCTION fun_getcorce(stu_name VARCHAR(10),course_name varchar(10)) RETURNS INT BEGIN DECLARE corce INT; SET corce=0; -- 还可以用 DECLARE corce_temp INT DEFAULT 0; 声明变量。 SELECT sc.grade INTO corce FROM sc WHERE sc.sno=(SELECT student.sno FROM student WHERE student.sname=stu_name) AND sc.cno=(SELECT course.cno from course where course.cname=course_name); RETURN corce; END;
在函数中使用 if 语句:
CREATE FUNCTION fun_isPass(stu_name VARCHAR(10),course_name VARCHAR(10)) RETURNS VARCHAR(10) BEGIN DECLARE stu_core INT DEFAULT 0; DECLARE res VARCHAR(10) DEFAULT ‘‘; SELECT grade INTO stu_core FROM stu_sno_sname_cname_grade_view WHERE sname=stu_name AND cname=course_name; IF stu_core>60 AND stu_core<100 THEN SET res =‘及格‘; ELSEIF stu_core=100 THEN SET res=‘满分‘; ELSEIF stu_core<60 THEN SET res =‘不及格‘; END IF; RETURN res ; END;
使用procedure:
创建存储过程:
CREATE PROCEDURE pro_test() BEGIN SELECT ‘hello world‘; END;
调用procedure:
CALL pro_test();
创建一个带输入参数的procedure:
CREATE PROCEDURE pro_test1(in n INT) BEGIN SELECT n*n; END;
调用:
CALL pro_test1(10); -- 或 SET @n =20; CALL pro_test1(@n);
带返回参数的procedure:
CREATE PROCEDURE pro_test2(in n int,INOUT m INT) BEGIN SET m=n*m; END;
IN表示输入参数,OUT表示输出,INOUT表示既可以输入也可以输出。
调用:
SET @n=10; SET @m=20; CALL pro_test2(@n,@m); SELECT @m;
在procedure中使用循环:
CREATE PROCEDURE pro_add_grade() BEGIN DECLARE grade_temp INT DEFAULT 0; SELECT MIN(grade) INTO grade_temp FROM sc; WHILE grade_temp < 60 DO UPDATE sc SET grade=grade+10; SELECT MIN(grade) INTO grade_temp FROM sc; END WHILE; UPDATE sc SET grade=100 WHERE grade > 100; END;
时间: 2024-10-13 16:55:40