使用Mysql,与navicat工具
下面是mysql创建的代码,可做必要修改
-- 创建学生学籍信息表
CREATE TABLE StudentEnrollment (
-- 学号,作为主键
student_id VARCHAR(8) NOT NULL,
-- 学生姓名
stu_name VARCHAR(8) NOT NULL,
-- 学生性别
gender VARCHAR(6) NOT NULL,
-- 学生班级
class_ VARCHAR(20) NOT NULL,
-- 学生出生日期,存储格式为字符串
birth_date VARCHAR(80) NOT NULL,
-- 学生所在学校
school VARCHAR(200) NOT NULL,
-- 设置学号为主键
PRIMARY KEY (student_id)
) COMMENT='学生学籍信息表';
-- 创建班级信息表
CREATE TABLE ClassInfo (
-- 班级班号,作为主键
class_id VARCHAR(40) NOT NULL,
-- 班级排名
cls_rank VARCHAR(40) NOT NULL,
-- 班级班主任
head_teacher VARCHAR(40) NOT NULL,
-- 班级学生总数
student_count INT NOT NULL,
PRIMARY KEY (class_id)
) COMMENT='班级信息表';
-- 创建学生成绩信息表
CREATE TABLE StudentScores (
-- 学号,作为主键
student_id VARCHAR(8) NOT NULL,
-- 学生姓名
stu_name VARCHAR(8) NOT NULL,
-- 学生总成绩
total_score INT NOT NULL,
-- 学生分数排名
score_rank INT NOT NULL,
-- 学生所在班级
class_ VARCHAR(20) NOT NULL,
-- 设置学号为主键
PRIMARY KEY (student_id)
) COMMENT='学生成绩信息表';
-- 添加外键约束,将学生成绩信息表的学号字段与学生学籍信息表的学号字段关联
ALTER TABLE StudentScores
ADD CONSTRAINT fk_student_id
FOREIGN KEY (student_id) REFERENCES StudentEnrollment(student_id);
--
-- 插入班级信息
INSERT INTO ClassInfo (class_id, cls_rank, head_teacher, student_count) VALUES
('ClassA', '1', 'TeacherA', 10),
('ClassB', '2', 'TeacherB', 10),
('ClassC', '3', 'TeacherC', 10);
-- 为ClassA插入学生学籍信息
INSERT INTO StudentEnrollment (student_id, stu_name, gender, class_, birth_date, school) VALUES
('A01', 'Alice', 'Female', 'ClassA', '2002-01-01', '海南师范大学'),
('A02', 'Bob', 'Male', 'ClassA', '2002-02-02', '海南师范大学'),
('A03', 'Charlie', 'Male', 'ClassA', '2002-03-03', '海南师范大学'),
('A04', 'Diana', 'Female', 'ClassA', '2002-04-04', '海南师范大学'),
('A05', 'Ethan', 'Male', 'ClassA', '2002-05-05', '海南师范大学'),
('A06', 'Fiona', 'Female', 'ClassA', '2002-06-06', '海南师范大学'),
('A07', 'George', 'Male', 'ClassA', '2002-07-07', '海南师范大学'),
('A08', 'Hannah', 'Female', 'ClassA', '2002-08-08', '海南师范大学'),
('A09', 'Ivan', 'Male', 'ClassA', '2002-09-09', '海南师范大学'),
('A10', 'Julia', 'Female', 'ClassA', '2002-10-10', '海南师范大学');
-- 为ClassA的学生插入成绩信息
INSERT INTO StudentScores (student_id, stu_name, total_score, score_rank, class) VALUES
('A01', 'Alice', 90, 1, 'ClassA'),
('A02', 'Bob', 85, 2, 'ClassA'),
('A03', 'Charlie', 88, 3, 'ClassA'),
('A04', 'Diana', 92, 4, 'ClassA'),
('A05', 'Ethan', 82, 5, 'ClassA'),
('A06', 'Fiona', 87, 6, 'ClassA'),
('A07', 'George', 91, 7, 'ClassA'),
('A08', 'Hannah', 84, 8, 'ClassA'),
('A09', 'Ivan', 89, 9, 'ClassA'),
('A10', 'Julia', 86, 10, 'ClassA');
-- 为ClassB插入学生学籍信息
INSERT INTO StudentEnrollment (student_id, stu_name, gender, class_, birth_date, school) VALUES
('B01', '赵天宇', '男', 'ClassB', '2003-01-01', '海南师范大学'),
('B02', '钱晓晓', '女', 'ClassB', '2003-02-02', '海南师范大学'),
('B03', '孙丽丽', '女', 'ClassB', '2003-03-03', '海南师范大学'),
('B04', '李强', '男', 'ClassB', '2003-04-04', '海南师范大学'),
('B05', '周梅', '女', 'ClassB', '2003-05-05', '海南师范大学'),
('B06', '吴刚', '男', 'ClassB', '2003-06-06', '海南师范大学'),
('B07', '郑洁', '女', 'ClassB', '2003-07-07', '海南师范大学'),
('B08', '王磊', '男', 'ClassB', '2003-08-08', '海南师范大学'),
('B09', '陈静', '女', 'ClassB', '2003-09-09', '海南师范大学'),
('B10', '郭阳', '男', 'ClassB', '2003-10-10', '海南师范大学');
-- 为ClassB的学生插入成绩信息
INSERT INTO StudentScores (student_id, stu_name, total_score, score_rank, class) VALUES
('B01', '赵天宇', 85, 1, 'ClassB'),
('B02', '钱晓晓', 88, 2, 'ClassB'),
('B03', '孙丽丽', 82, 3, 'ClassB'),
('B04', '李强', 90, 4, 'ClassB'),
('B05', '周梅', 87, 5, 'ClassB'),
('B06', '吴刚', 84, 6, 'ClassB'),
('B07', '郑洁', 89, 7, 'ClassB'),
('B08', '王磊', 86, 8, 'ClassB'),
('B09', '陈静', 83, 9, 'ClassB'),
('B10', '郭阳', 80, 10, 'ClassB');
-- 为ClassC插入学生学籍信息
INSERT INTO StudentEnrollment (student_id, stu_name, gender, class_, birth_date, school) VALUES
('C01', '萧炎', '男', 'ClassC', '2004-01-01', '海南师范大学'),
('C02', '云韵', '女', 'ClassC', '2004-02-02', '海南师范大学'),
('C03', '美杜莎', '女', 'ClassC', '2004-03-03', '海南师范大学'),
('C04', '药老', '男', 'ClassC', '2004-04-04', '海南师范大学'),
('C05', '小医仙', '女', 'ClassC', '2004-05-05', '海南师范大学'),
('C06', '萧薰儿', '女', 'ClassC', '2004-06-06', '海南师范大学'),
('C07', '纳兰嫣然', '女', 'ClassC', '2004-07-07', '海南师范大学'),
('C08', '海波东', '男', 'ClassC', '2004-08-08', '海南师范大学'),
('C09', '紫妍', '女', 'ClassC', '2004-09-09', '海南师范大学'),
('C10', '林修崖', '男', 'ClassC', '2004-10-10', '海南师范大学');
-- 为ClassC的学生插入成绩信息
INSERT INTO StudentScores (student_id, stu_name, total_score, score_rank, class_) VALUES
('C01', '萧炎', 92, 1, 'ClassC'),
('C02', '云韵', 88, 2, 'ClassC'),
('C03', '美杜莎', 90, 3, 'ClassC'),
('C04', '药老', 87, 4, 'ClassC'),
('C05', '小医仙', 85, 5, 'ClassC'),
('C06', '萧薰儿', 91, 6, 'ClassC'),
('C07', '纳兰嫣然', 89, 7, 'ClassC'),
('C08', '海波东', 86, 8, 'ClassC'),
('C09', '紫妍', 84, 9, 'ClassC'),
('C10', '林修崖', 83, 10, 'ClassC');
-- 创建视图
-- 创建视图
CREATE VIEW StudentScoresWithTotalRank AS
SELECT
student_id, -- 学号
stu_name, -- 姓名
total_score, -- 总成绩
RANK() OVER (ORDER BY total_score DESC) AS score_rank, -- 总分排名
class_ -- 班级
FROM StudentScores; -- 从学生成绩表中选择数据
SELECT * FROM StudentScoresWithTotalRank;
DROP VIEW StudentScoresWithTotalRank;
DELETE FROM StudentScores
WHERE student_id = 'A01';
INSERT INTO StudentScores (student_id, stu_name, total_score, score_rank, class_) VALUES
('A02', 'Aliasae', 99, 1, 'ClassA');
CREATE VIEW ViewStudentEnrollment AS
SELECT *
FROM StudentEnrollment;
SELECT *
FROM ViewStudentEnrollment;
CREATE VIEW ViewClassInfo AS
SELECT *
FROM ClassInfo;
SELECT * FROM ViewClassInfo;
DROP VIEW IF EXISTS ViewClassInfo;
-- 修改 StudentEnrollment 表的 class_ 字段,使其数据类型与 ClassInfo 表的 class_id 字段匹配
ALTER TABLE StudentEnrollment MODIFY class_ VARCHAR(40);
-- 添加外键约束
ALTER TABLE StudentEnrollment
ADD CONSTRAINT fk_class FOREIGN KEY (class_) REFERENCES ClassInfo(class_id);
-- 班级表视图
CREATE VIEW ViewClassInfo AS
SELECT
CI.class_id,
CI.cls_rank,
CI.head_teacher,
COUNT(SE.class_) AS student_count
FROM
ClassInfo CI
LEFT JOIN StudentEnrollment SE ON CI.class_id = SE.class_
GROUP BY
CI.class_id,
CI.cls_rank,
CI.head_teacher;
SELECT * FROM ViewClassInfo;
前端链接:学生信息管理系统(简化版)-CSDN博客
后端接口链接:学生信息管理系统(简化版)后端接口-CSDN博客