sql
复制代码
CREATE TABLE `classes` (
`ClassID` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '班级ID',
`ClassName` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '班级名称',
`TeacherID` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '老师ID',
PRIMARY KEY (`ClassID`) USING BTREE,
INDEX `TeacherID`(`TeacherID`) USING BTREE,
CONSTRAINT `classes_ibfk_1` FOREIGN KEY (`TeacherID`) REFERENCES `teachers` (`TeacherID`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `classes` VALUES ('001', '计算机科学', 'T001');
INSERT INTO `classes` VALUES ('002', '物理学', 'T002');
INSERT INTO `classes` VALUES ('003', '化学', 'T003');
-- ----------------------------
CREATE TABLE `students` (
`StudentID` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '学生ID',
`StudentName` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '学生姓名',
`ClassID` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '班级ID',
PRIMARY KEY (`StudentID`) USING BTREE,
INDEX `ClassID`(`ClassID`) USING BTREE,
CONSTRAINT `students_ibfk_1` FOREIGN KEY (`ClassID`) REFERENCES `classes` (`ClassID`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `students` VALUES ('S001', '小明', '001');
INSERT INTO `students` VALUES ('S002', '小红', '002');
INSERT INTO `students` VALUES ('S003', '小刚', '003');
INSERT INTO `students` VALUES ('S004', '小李', NULL);
INSERT INTO `students` VALUES ('S006', '小张', '002');
-- ----------------------------
CREATE TABLE `subjects` (
`SubjectID` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '科目ID',
`SubjectName` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '科目名称',
`TeacherID` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '老师ID',
PRIMARY KEY (`SubjectID`) USING BTREE,
INDEX `TeacherID`(`TeacherID`) USING BTREE,
CONSTRAINT `subjects_ibfk_1` FOREIGN KEY (`TeacherID`) REFERENCES `teachers` (`TeacherID`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
INSERT INTO `subjects` VALUES ('SUB001', '计算机编程', 'T001');
INSERT INTO `subjects` VALUES ('SUB002', '高级物理', 'T002');
INSERT INTO `subjects` VALUES ('SUB003', '有机化学', 'T003');
INSERT INTO `subjects` VALUES ('SUB004', '数据结构', 'T001');
-- ----------------------------
CREATE TABLE `teachers` (
`TeacherID` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '老师ID',
`TeacherName` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '老师姓名',
`Specialty` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '专业领域',
PRIMARY KEY (`TeacherID`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
INSERT INTO `teachers` VALUES ('T001', '李老师', '计算机科学');
INSERT INTO `teachers` VALUES ('T002', '王老师', '物理学');
INSERT INTO `teachers` VALUES ('T003', '张老师', '化学');
INSERT INTO `teachers` VALUES ('T004', '刘校长', NULL);