Mysql 学校信息管理系统

1.创建数据库语句:

#创建数据库

CREATE DATABASE `schoolDB`;

USE `schoolDB`;

 

#创建学生表

CREATE TABLE `student`(

`sid` INT(4) AUTO_INCREMENT NOT NULL PRIMARY KEY COMMENT'学生编号',

`stuName` VARCHAR(10) NOT NULL COMMENT'学生姓名',

`age` INT(4) NOT NULL COMMENT'年龄',

`sex` INT(1) NOT NULL COMMENT'学生性别'

)ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT='学生信息表';

 

#创建课程表

CREATE TABLE `course`(

`cid` INT(4) PRIMARY KEY COMMENT'课程编号',

`cName` VARCHAR(50) NOT NULL COMMENT'课程名称',

`tid` INT(4) NOT NULL COMMENT'教师编号'

)ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT='课程信息表';

 

#创建成绩表

CREATE TABLE `score`(

`sid` INT(4) COMMENT'学生编号',

`cid` INT(4) COMMENT'课程便号',

`score` INT(4) NOT NULL COMMENT'成绩'

)ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT='成绩表';

 

#创建教师 表

CREATE TABLE `teacher`(

`tid` INT(4) PRIMARY KEY COMMENT'教师编号',

`teaName` VARCHAR(50) NOT NULL COMMENT'教师姓名'

)ENGINE=INNODB DEFAULT CHARSET=utf8 COMMENT='教师信息表';

 

#创建`score表`主键

ALTER TABLE `score` ADD CONSTRAINT `pk_score` PRIMARY KEY `score`(`sid`,`cid`);

#创建课程表外键外键关联 教师表 教师编号

ALTER TABLE `course` ADD CONSTRAINT `fk_course_teacher` FOREIGN KEY (`tid`) REFERENCES `teacher`(`tid`);

#创建成绩表外键关联 学生表 学生编号

ALTER TABLE `score` ADD CONSTRAINT `fk_score_student` FOREIGN KEY (`sid`) REFERENCES `student`(`sid`);

#创建成绩表外键关联 课程表 课程编号

ALTER TABLE `score` ADD CONSTRAINT `fk_score_course` FOREIGN KEY (`cid`) REFERENCES `course`(`cid`);

 

#插入学生表 数据

INSERT INTO `student` VALUES(1,'张三',16,1);

INSERT INTO `student` VALUES(2,'李三思',17,1);

INSERT INTO `student` VALUES(3,'李大齐',16,1);

INSERT INTO `student` VALUES(4,'王晓花',16,0);

INSERT INTO `student` VALUES(5,'刘得住',17,1);

INSERT INTO `student` VALUES(6,'谢谢你',16,1);

INSERT INTO `student` VALUES(7,'吴三桂',16,1);

INSERT INTO `student` VALUES(8,'桂花',16,0);

 

#插入教师表 数据

INSERT INTO `teacher` VALUES(101,'朱大胖');

INSERT INTO `teacher` VALUES(102,'方芳');

INSERT INTO `teacher` VALUES(103,'刘博士');

 

#插入课程表 数据

INSERT INTO `course` VALUES(1011,'语文',101);

INSERT INTO `course` VALUES(1012,'英语',102);

INSERT INTO `course` VALUES(1013,'数学',103);

 

#插入成绩表 数据

INSERT INTO `score` VALUES(1,1011,90);

INSERT INTO `score` VALUES(1,1012,70);

INSERT INTO `score` VALUES(1,1013,60);

INSERT INTO `score` VALUES(2,1011,88);

INSERT INTO `score` VALUES(2,1012,70);

INSERT INTO `score` VALUES(2,1013,70);

INSERT INTO `score` VALUES(3,1011,92);

INSERT INTO `score` VALUES(3,1012,71);

INSERT INTO `score` VALUES(3,1013,65);

INSERT INTO `score` VALUES(4,1011,99);

INSERT INTO `score` VALUES(4,1012,78);

INSERT INTO `score` VALUES(4,1013,88);

INSERT INTO `score` VALUES(5,1011,87);

INSERT INTO `score` VALUES(5,1012,79);

INSERT INTO `score` VALUES(5,1013,62);

INSERT INTO `score` VALUES(6,1011,56);

INSERT INTO `score` VALUES(6,1012,65);

INSERT INTO `score` VALUES(6,1013,75);

INSERT INTO `score` VALUES(7,1011,50);

INSERT INTO `score` VALUES(7,1012,55);

INSERT INTO `score` VALUES(7,1013,51);

INSERT INTO `score` VALUES(8,1011,95);

INSERT INTO `score` VALUES(8,1012,59);

INSERT INTO `score` VALUES(8,1013,64);

2.执行SQL查询

USE `schooldb`;

/*1.查询学过“方芳”老师所教课程的学生的学号、姓名。(假定学生所学的课程均参加了考试)。*/

#测试通过

SELECT `sid`,`stuName` FROM `student` WHERE `sid` IN(

         SELECT `sid` FROM `score` WHERE `cid`=(

                   SELECT `cid` FROM `course` WHERE `tid`=(

                            SELECT `tid` FROM `teacher` WHERE `teaName`='方芳'

                   )

         )

);

3.NOT IN取反的经典应用

/*2.查询所有课程成绩均小于60分的学生学号和姓名。*/

#改一下数据才有符合要求的数据

UPDATE `score` SET `score`=50 WHERE `sid`=7 AND `cid`=1013;

SELECT `sid`,`stuName` FROM `student` WHERE `sid` NOT IN(

         SELECT `sid` FROM `score`

         WHERE `score`>60

);

/*难点:步骤分析:

         1.查询所有分数大于60的学生编号

         2.取反,不在所有大于60分的里面的学生,就是所有成绩小于60的学生

*/

4.LIMIT不支持子查询的解决方法

/*3.查询“方芳”老师所教的“数据库”课程成绩排名在第3~6名的学生学号、姓名,

并将此记录插入新表tempScore中。(要求使用limit子句)*/

CREATE TABLE `tempScore`(

         SELECT `sid`,`stuName` FROM `student` WHERE `sid` IN(

                   #查询方芳老师所教课程第3-6名学员的编号

                   SELECT `sid` FROM(

                            SELECT `sid` FROM `score` WHERE `cid` IN(

                                     SELECT `cid` FROM `course` WHERE `tid`=(

                                     SELECT `tid` FROM `teacher` WHERE `teaName`='方芳'

                                     )

                            )GROUP BY `score` DESC LIMIT 2,4

                   )AS ta

         )

);

/*难点:问题出现在mysql不支持在子查询中使用limit报错:This version of MySQL doesn’t yet support ‘LIMIT & IN/ALL/ANY/SOME subquery

解决的方法是:在limit子句外面加上一层查询"SELECT `sid` FROM()

并且给from的结果集取上别名,就不会报错

*/

4.HAVING进行分组的应用

/*4.查询有两门以上课程不及格的学生学号及其平均成绩。*/

SELECT  s.sid, AVG(sc.score)

FROM `student` AS s, `score` AS sc

WHERE s.sid IN

(

      SELECT `sid` FROM `score`

      WHERE `score` < 60

      GROUP BY `sid`

      HAVING COUNT(`cid`)>= 2

)

AND s.sid=sc.sid

GROUP BY s.sid

/*用到了having 分组*/

5.创建视图

/*5.创建视图student_view,用于查询所有学生的学号、姓名、参加考试课程数、各科总成绩。*/

CREATE VIEW `student_view`

AS

SELECT s.sid,s.stuName,COUNT(c.cid),SUM(sc.score) FROM `student`AS s

INNER JOIN `score` AS sc ON sc.sid=s.sid

INNER JOIN `course` AS c ON c.cid = sc.cid

GROUP BY s.sid;

posted @ 2018-06-14 15:08  H丶  阅读(1570)  评论(0编辑  收藏  举报