DROP DATABASE IF EXISTS `2018210000`;
CREATE DATABASE `2018210000`;
(1)有关学生的信息,包括学生的学号,姓名,班级,性别,出生时间,政治面貌,籍贯,入学时间,联系电话,简历;
Student(Sno,Sname,Ssex,ClassNo,Sbirth,Status,Hometown,Enrollment,Telephone,Resume)
(2)有关班级的信息,包括班级的班名,专业,年级,人数,班主任;SClass(ClassNo,ClassName,Major,Grade,Number,Advisor)
(3)有关课程的信息,包括课程编号,课程名称,课程类别(是指该课程为必修,指选还是任选),适用专业,适用年级,开课时间(是指在每个学年的第一学期,还是第二学期),学分,周学时;Course(CourseNo,CourseName,Category,Major,Grade,StartTM,Credits,WeekHours)
(4)有关学年课程安排的信息,包括课程编号,开设课程的学年(例如:2019学年),开设课程的班级,任课教师;
Schedule(CourseNo,StartYear,ClassNo,Teacher)
(5)有关学生选课的信息,包括课程编号,学生的学号,成绩;
SC(CourseNo,Sno,Score)
DROP DATABASE IF EXISTS `school`;
CREATE DATABASE `school`;
USE `school`;
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
`CourseNo` int(11) NOT NULL COMMENT '课程编号',
`CourseName` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '课程名称',
`Category` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '课程类别',
`Major` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '适用专业',
`Grade` int(11) NULL DEFAULT NULL COMMENT '适用年级',
`StartTM` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '开课时间(一二学期)',
`Credits` int(11) NULL DEFAULT NULL COMMENT '学分',
`WeekHours` int(11) NULL DEFAULT NULL COMMENT '周学时'
) ENGINE = MyISAM CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = DYNAMIC;
DROP TABLE IF EXISTS `sc`;
CREATE TABLE `sc` (
`CourseNo` int(11) NOT NULL COMMENT '课程编号',
`Sno` int(11) NOT NULL COMMENT '学生学号',
`Score` int(11) NULL DEFAULT NULL COMMENT '成绩'
) ENGINE = MyISAM CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = FIXED;
DROP TABLE IF EXISTS `schedule`;
CREATE TABLE `schedule` (
`CourseNo` int(11) NOT NULL COMMENT '课程编号',
`StartYear` date NULL DEFAULT NULL COMMENT '开设课程的学年,如2019年',
`ClassNo` int(11) NULL DEFAULT NULL COMMENT '开设课程的班级',
`Teacher` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '任课教师'
) ENGINE = MyISAM CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = DYNAMIC;
DROP TABLE IF EXISTS `sclass`;
CREATE TABLE `sclass` (
`ClassNo` int(11) NOT NULL COMMENT '班号',
`ClassName` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '班名',
`Major` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '专业',
`Grade` int(11) NULL DEFAULT NULL COMMENT '年级',
`Number` int(11) NULL DEFAULT NULL COMMENT '人数',
`Advisor` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '班主任'
) ENGINE = MyISAM CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = DYNAMIC;
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`Sno` int(11) NOT NULL COMMENT '学号',
`Sname` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '姓名',
`Ssex` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '性别',
`Classno` int(11) NULL DEFAULT NULL COMMENT '班级号',
`Sbirth` date NULL DEFAULT NULL COMMENT '出生日期',
`Status` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '政治面貌',
`Hometown` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '籍贯',
`Enrollment` date NULL DEFAULT NULL COMMENT '入学时间',
`Telephone` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL COMMENT '联系电话',
`Resume` varchar(255) CHARACTER SET utf8 NULL COMMENT '简历'
) ENGINE = MyISAM CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = DYNAMIC;
INSERT INTO `student` VALUES
(201815126, '张三', '男', 2018123, '1997-04-05', '团员', '北京', '2018-09-01', '13812345678', NULL);
INSERT INTO `course` VALUES (1, '数据库技术', '指选', '通信工程', '2', '第二学期', 2, 2);
INSERT INTO `sc` VALUES (1, 201815126, NULL);
UPDATE `course` SET `StartTM` = '每年第二学期' WHERE `CourseNo` = 1;
UPDATE `SC` SET `Score` = 90 WHERE `Sno` = 201815126 AND `CourseNo` = 1;
DELETE FROM `SC` WHERE `Sno` = 201815126 AND `CourseNo` = 1;
TRUNCATE TABLE `SC`;
DROP DATABASE IF EXISTS stu;
CREATE TABLE stu SELECT * FROM `student`;
为了后续方便操作便利,插入更多数据
-- 对course表
ALTER TABLE course ADD PRIMARY KEY(`CourseNo`);
INSERT INTO `school`.`course`(`CourseNo`, `CourseName`, `Category`, `Major`, `Grade`, `StartTM`, `Credits`, `WeekHours`) VALUES (2, 'C++程序设计', '必修', '信息工程', 1, '每年第一学期', 3, 3);
INSERT INTO `school`.`course`(`CourseNo`, `CourseName`, `Category`, `Major`, `Grade`, `StartTM`, `Credits`, `WeekHours`) VALUES (3, '区块链导论', '任选', '计算机', 1, '每年第二学期', 2, 2);
INSERT INTO `school`.`course`(`CourseNo`, `CourseName`, `Category`, `Major`, `Grade`, `StartTM`, `Credits`, `WeekHours`) VALUES (4, 'python程序设计', '指选', '电子信息工程', 1, '每年第二学期', 2, 2);
INSERT INTO `school`.`course`(`CourseNo`, `CourseName`, `Category`, `Major`, `Grade`, `StartTM`, `Credits`, `WeekHours`) VALUES (5, 'java程序设计', '指选', '信息工程', 1, '每年第一学期', 2, 2);
INSERT INTO `school`.`course`(`CourseNo`, `CourseName`, `Category`, `Major`, `Grade`, `StartTM`, `Credits`, `WeekHours`) VALUES (6, 'c程序设计', '指选', '通信工程', 1, '每年第一学期', 2, 2);
-- 对sclass表
ALTER TABLE sclass ADD PRIMARY KEY(`ClassNo`);
INSERT INTO `school`.`sclass`(`ClassNo`, `ClassName`, `Major`, `Grade`, `Number`, `Advisor`) VALUES (2018123, '一班', '通信工程', 2, 27, '史珍香');
INSERT INTO `school`.`sclass`(`ClassNo`, `ClassName`, `Major`, `Grade`, `Number`, `Advisor`) VALUES (2018456, '二班', '信息工程', 2, 28, '姬从良');
INSERT INTO `school`.`sclass`(`ClassNo`, `ClassName`, `Major`, `Grade`, `Number`, `Advisor`) VALUES (2018789, '三班', '电子信息工程', 2, 29, '庞光');
INSERT INTO `school`.`sclass`(`ClassNo`, `ClassName`, `Major`, `Grade`, `Number`, `Advisor`) VALUES (2017211113, '四班', '信息工程', 3, 29, '艾骤');
-- 对student表
ALTER TABLE student ADD PRIMARY KEY(Sno);
ALTER TABLE student ADD CONSTRAINT fk_student_sclass FOREIGN KEY(ClassNo) REFERENCES sclass(ClassNo);
INSERT INTO `school`.`student`(`Sno`, `Sname`, `Ssex`, `ClassNo`, `Sbirth`, `Status`, `Hometown`, `Enrollment`, `Telephone`, `Resume`) VALUES (201815226, '李四', '男', 2018456, '2000-04-05', '团员', '天津', '2018-08-01', '15812345678', NULL);
INSERT INTO `school`.`student`(`Sno`, `Sname`, `Ssex`, `ClassNo`, `Sbirth`, `Status`, `Hometown`, `Enrollment`, `Telephone`, `Resume`) VALUES (201815326, '王二麻子', '男', 2018789, '1997-08-28', '团员', '上海', '2018-09-01', '17812345678', NULL);
INSERT INTO `school`.`student`(`Sno`, `Sname`, `Ssex`, `ClassNo`, `Sbirth`, `Status`, `Hometown`, `Enrollment`, `Telephone`, `Resume`) VALUES (201815426, '魏生津', '女', 2018123, '2000-09-11', '团员', '深圳', '2018-09-01', '19812345678', NULL);
INSERT INTO `school`.`student`(`Sno`, `Sname`, `Ssex`, `ClassNo`, `Sbirth`, `Status`, `Hometown`, `Enrollment`, `Telephone`, `Resume`) VALUES (201815526, '范统', '男', 2018789, '1997-09-11', '团员', '上海', '2018-09-01', '19812345678', NULL);
INSERT INTO `school`.`student`(`Sno`, `Sname`, `Ssex`, `ClassNo`, `Sbirth`, `Status`, `Hometown`, `Enrollment`, `Telephone`, `Resume`) VALUES (201815626, '辣鸡小新', '女', 2018789, '1995-09-11', '团员', '深圳', '2018-09-01', '19812345678', NULL);
INSERT INTO `school`.`student`(`Sno`, `Sname`, `Ssex`, `ClassNo`, `Sbirth`, `Status`, `Hometown`, `Enrollment`, `Telephone`, `Resume`) VALUES (201815726, '沈京兵', '女', 2018456, '1990-09-11', '团员', '北京', '2018-09-01', '19812345678', NULL);
INSERT INTO `school`.`student`(`Sno`, `Sname`, `Ssex`, `ClassNo`, `Sbirth`, `Status`, `Hometown`, `Enrollment`, `Telephone`, `Resume`) VALUES (201815826, '王小明', '男', 2018123, '2001-09-11', '团员', '天津', '2018-09-01', '19812345678', NULL);
INSERT INTO `school`.`student`(`Sno`, `Sname`, `Ssex`, `ClassNo`, `Sbirth`, `Status`, `Hometown`, `Enrollment`, `Telephone`, `Resume`) VALUES (201815926, '王小红', '女', 2018123, '2001-05-11', '团员', '山东', '2018-09-01', '19812345678', NULL);
INSERT INTO `school`.`student`(`Sno`, `Sname`, `Ssex`, `ClassNo`, `Sbirth`, `Status`, `Hometown`, `Enrollment`, `Telephone`, `Resume`) VALUES (201815026, '王小绿', '女', 2017211113, '1995-06-11', '团员', '山东', '2018-09-01', '19812345678', NULL);
INSERT INTO `school`.`student`(`Sno`, `Sname`, `Ssex`, `ClassNo`, `Sbirth`, `Status`, `Hometown`, `Enrollment`, `Telephone`, `Resume`) VALUES (201815016, '王小黄', '男', 2017211113, '1996-07-11', '团员', '山东', '2018-02-01', '19812345678', NULL);
-- 对sc表
ALTER TABLE sc ADD CONSTRAINT fk_sc_course FOREIGN KEY(CourseNo) REFERENCES course(CourseNo);
ALTER TABLE sc ADD CONSTRAINT fk_sc_student FOREIGN KEY(Sno) REFERENCES student(Sno) ON DELETE CASCADE;
INSERT INTO sc VALUES(1,201815126,99);
INSERT INTO sc VALUES(2,201815226,77);
INSERT INTO sc VALUES(3,201815326,66);
INSERT INTO sc VALUES(4,201815126,69);
INSERT INTO sc VALUES(6,201815526,80);
INSERT INTO sc VALUES(5,201815626,NULL);
INSERT INTO sc VALUES(4,201815726,NULL);
INSERT INTO sc VALUES(6,201815826,55);
INSERT INTO sc VALUES(6,201815026,56);
INSERT INTO sc VALUES(5,201815026,94);
INSERT INTO sc VALUES(2,201815026,85);
INSERT INTO sc VALUES(6,201815016,95);
INSERT INTO sc VALUES(1,201815926,59);
-- 对schedule表
ALTER TABLE `schedule` ADD PRIMARY KEY(CourseNo,ClassNo);
ALTER TABLE `schedule` ADD CONSTRAINT fk_schedule_course FOREIGN KEY(CourseNo) REFERENCES course(CourseNo);
ALTER TABLE `schedule` ADD CONSTRAINT fk_schedule_sclass FOREIGN KEY(ClassNo) REFERENCES sclass(ClassNo) ;
INSERT INTO `school`.`schedule`(`CourseNo`, `StartYear`, `ClassNo`, `Teacher`) VALUES (1, '2019学年', 2018123, '梦梵');
INSERT INTO `school`.`schedule`(`CourseNo`, `StartYear`, `ClassNo`, `Teacher`) VALUES (2, '2019学年', 2018456, '姬从良');
INSERT INTO `school`.`schedule`(`CourseNo`, `StartYear`, `ClassNo`, `Teacher`) VALUES (3, '2020学年', 2018789, '杜子腾');
INSERT INTO `school`.`schedule`(`CourseNo`, `StartYear`, `ClassNo`, `Teacher`) VALUES (4, '2020学年', 2018123, '马分');
INSERT INTO `school`.`schedule`(`CourseNo`, `StartYear`, `ClassNo`, `Teacher`) VALUES (5, '2020学年', 2018456, '夏琉');
INSERT INTO `school`.`schedule`(`CourseNo`, `StartYear`, `ClassNo`, `Teacher`) VALUES (6, '2020学年', 2018789, '李建仁');
SELECT Sname AS '姓名',Ssex AS '性别',Hometown AS '籍贯',Sbirth AS '出生日期'
FROM student;
查询结果如下:
SELECT Sname AS '姓名',Ssex AS '性别',FLOOR(DATEDIFF(CURDATE(),student.Sbirth)/365.2422) AS '年龄'
FROM student;
SELECT Sname AS '姓名',FLOOR(DATEDIFF(CURDATE(),student.Sbirth)/365.2422) AS '年龄'
FROM student
WHERE Sname LIKE '王%';
SELECT student.Sname AS'姓名',course.CourseName AS'课程名',sc.Score AS'成绩'
FROM student,course,sc
WHERE student.Sno= sc.Sno
AND course.CourseNo = sc.CourseNo
AND sc.Score >=70
AND sc.CourseNo IN (1,2);
SELECT student.Sname '姓名',course.CourseName '课程名'
FROM student,course,sc
WHERE student.Sno = sc.Sno
AND course.CourseNo = sc.CourseNo
AND sc.Score IS NULL;
SELECT student.Sname AS '姓名',FLOOR(DATEDIFF(CURDATE(),student.Sbirth)/365.2422) AS '年龄'
FROM student
WHERE student.Ssex = '男'
AND FLOOR(DATEDIFF(CURDATE(),student.Sbirth)/365.2422) >= (
SELECT AVG(FLOOR(DATEDIFF(CURDATE(),student.Sbirth)/365.2422))
FROM student
WHERE student.Ssex = '女'
);
SELECT student.Sname '姓名',
(Select Count(sc.Sno) From sc Where sc.Sno=student.Sno) '选课数量'
FROM student
GROUP BY student.Sno;
Select student.Sname '姓名',
(Select Count(sc.Sno) From sc Where sc.Sno=student.Sno) '选课数量'
From student,sc
Group By student.Sno
Having Avg(Score)>60
SELECT student.Sno '学号',student.Ssex '性别'
FROM student
WHERE student.Hometown =(
SELECT Hometown FROM student WHERE student.Sname = '王小明'
)
SELECT student.Sname '姓名',
student.Sbirth '出生日期'
FROM student,sc,sclass,schedule
WHERE student.ClassNo = sclass.ClassNo
AND sc.CourseNo = `schedule`.CourseNo
AND sc.Sno=student.Sno
AND sclass.Advisor = schedule.Teacher
SELECT student.`Status` '政治面貌',sclass.ClassName '班级名称',course.CourseName
FROM student,sclass,course,sc
WHERE student.Sname = '王小明'
AND student.ClassNo = sclass.ClassNo
AND student.Sno = sc.Sno
AND course.CourseNo = sc.CourseNo
DROP TABLE IF EXISTS select_course;
CREATE TABLE select_course
SELECT student.Sname,(Select Count(sc.Sno) From sc Where sc.Sno=student.Sno) 'Scourse'
FROM student,sc
WHERE student.Sno = sc.Sno
GROUP BY student.Sno;
DROP TABLE IF EXISTS stuShandong;
CREATE TABLE stuShandong
SELECT *
FROM student
WHERE student.Hometown = '山东';
UPDATE sc
SET Score = Score + 1
WHERE sc.Sno IN (
SELECT student.Sno
FROM student
WHERE student.ClassNo = 2017211113);
# 利用11题添加的级联删除外键直接删除学生表中王小明的记录即可
DELETE FROM student WHERE student.Sname = '王小明';
CREATE UNIQUE INDEX name_index ON student(Sname);
#按照《数据库系统概论》一书中提供的方法,修改语句为
-- ALTER INDEX name_index RENAME TO Stu_name;
#mysql没有提供修改索引的方法,需要删除索引再重新创建
ALTER TABLE student
DROP INDEX name_index;
CREATE UNIQUE INDEX Stu_name ON student(Sname);
# 按照《数据库系统概论》一书中提供的方法,创建聚簇索引的语句为
-- CREATE CLUSTER INDEX sc_index ON sc(Sno,CourseNo);
# mysql的聚簇索引是bai指innodb引擎的特性
# MYSQL中没有CLUSTER索引的概念,只有INNODB的主键是使用CLUSTER方式存储的,直接添加学号与课程号为选课信息表的主键即可
ALTER TABLE sc ADD PRIMARY KEY(Sno,CourseNo);
# 先删除外键约束,否则无法删除主键
ALTER TABLE sc DROP FOREIGN KEY fk_sc_course;
ALTER TABLE sc DROP FOREIGN KEY fk_sc_student;
ALTER TABLE sc DROP PRIMARY KEY ;
SHOW INDEX FROM sc;
# 通信工程专业的班级是2018123班,选出班级为2018123的学生即可
CREATE VIEW stu_tongxin
AS
SELECT *
FROM student
WHERE student.ClassNo = 2018123;
DROP VIEW IF EXISTS stu_info;
CREATE VIEW stu_info
AS
SELECT student.Sno 'Sno', student.Sname 'Sname', course.CourseName 'CourseName', sc.Score 'Score'
FROM student,sc,course
WHERE student.Sno = sc.Sno
AND course.CourseNo = sc.CourseNo
DROP VIEW IF EXISTS course_info;
CREATE VIEW course_info
AS
SELECT course.CourseNo 'CourseNo', course.CourseName 'CourseName', sclass.ClassName 'ClassName', num
FROM course,sclass,
(SELECT course.CourseNo 'CourseNo',student.ClassNo 'ClassNo', count(*) AS num
FROM course,student,sc
WHERE course.CourseNo = sc.CourseNo
AND student.Sno = sc.Sno
GROUP BY course.CourseNo,student.ClassNo) tmp
WHERE course.CourseNo = tmp.CourseNo
AND sclass.ClassNo = tmp.ClassNo;
ALTER VIEW stu_tongxin
AS
SELECT *
FROM student
WHERE student.ClassNo = 2018123
WITH CHECK OPTION;
# 以下语句会报错,原因是mysql并不支持视图加密
DROP VIEW IF EXISTS stu_info;
ALTER VIEW stu_info
AS
SELECT student.Sno 'Sno', student.Sname 'Sname', course.CourseName 'CourseName', sc.Score 'Score'
FROM student,sc,course
WHERE student.Sno = sc.Sno
AND course.CourseNo = sc.CourseNo
with encryption;
SELECT stu_tongxin.Sname '姓名', stu_tongxin.Ssex '性别'
FROM stu_tongxin
WHERE year(stu_tongxin.Sbirth) >= 2000;
# 由于stu_info是由多张表组成的视图,mysql是无法间接插入数据的,原因是视图中并没有实体表中的全部字段(即建表的字段),故会报错。
INSERT INTO `school`.`stu_info`(`Sno`, `Sname`, `CourseName`, `Score`) VALUES (201815223, '黄龙', 'go程序设计', 99);
DROP VIEW course_info;