写mysql语言完成某高校学生选课管理系统, 来实现学生信息、课程学生管理; 实现教师及任课信息管理;实现选课登记,要保证学生不能选修重复的课程(实用触发器实现);创建存储过程统计各学生指定学期选修课程的总学分数; 创建视图查询2006-2007学年,没有被选修的课程;创建存储过程统计各教师任课的学生人数;创建表间关系。
时间: 2024-02-27 13:58:15 浏览: 128
为了回答这个问题,我会提供一些 MySQL 语言的代码作为示例,但是完整的选课系统建议由专业的开发人员完成。同时,我会尽力保证不透露您的要求。
1. 创建学生信息表
```sql
CREATE TABLE `student`
(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
`gender` ENUM('男', '女') NOT NULL,
`age` INT UNSIGNED NOT NULL,
`major` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
);
```
2. 创建课程信息表
```sql
CREATE TABLE `course`
(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
`credit` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`)
);
```
3. 创建教师信息表
```sql
CREATE TABLE `teacher`
(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
`gender` ENUM('男', '女') NOT NULL,
`age` INT UNSIGNED NOT NULL,
`department` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
);
```
4. 创建选课信息表
```sql
CREATE TABLE `selection`
(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`student_id` INT UNSIGNED NOT NULL,
`course_id` INT UNSIGNED NOT NULL,
`teacher_id` INT UNSIGNED NOT NULL,
`semester` VARCHAR(20) NOT NULL,
`grade` INT UNSIGNED,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_index` (`student_id`, `course_id`),
FOREIGN KEY (`student_id`) REFERENCES `student` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (`course_id`) REFERENCES `course` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
);
```
5. 创建触发器,保证学生不能选修重复的课程
```sql
CREATE TRIGGER `selection_before_insert` BEFORE INSERT ON `selection`
FOR EACH ROW
BEGIN
DECLARE count INT;
SELECT COUNT(*) INTO count FROM `selection` WHERE `student_id` = NEW.`student_id` AND `course_id` = NEW.`course_id`;
IF count > 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = '学生不能选修重复的课程';
END IF;
END;
```
6. 创建存储过程,统计各学生指定学期选修课程的总学分数
```sql
CREATE PROCEDURE `CalculateTotalCredit`(IN student_name VARCHAR(50), IN semester VARCHAR(20), OUT total_credit INT)
BEGIN
SELECT SUM(`course`.`credit`) INTO total_credit
FROM `selection`
JOIN `student` ON `selection`.`student_id` = `student`.`id`
JOIN `course` ON `selection`.`course_id` = `course`.`id`
WHERE `student`.`name` = student_name AND `selection`.`semester` = semester;
END;
```
7. 创建视图,查询2006-2007学年没有被选修的课程
```sql
CREATE VIEW `unselected_courses_0607` AS
SELECT `course`.`name`, `course`.`credit`
FROM `course`
LEFT JOIN `selection` ON `course`.`id` = `selection`.`course_id`
WHERE `selection`.`course_id` IS NULL AND `course`.`name` LIKE '%2006-2007%';
```
8. 创建存储过程,统计各教师任课的学生人数
```sql
CREATE PROCEDURE `CalculateStudentCount`(IN teacher_name VARCHAR(50), OUT student_count INT)
BEGIN
SELECT COUNT(*) INTO student_count
FROM `selection`
JOIN `teacher` ON `selection`.`teacher_id` = `teacher`.`id`
WHERE `teacher`.`name` = teacher_name;
END;
```
9. 创建表间关系
```sql
ALTER TABLE `selection` ADD CONSTRAINT `FK_student_id` FOREIGN KEY (`student_id`) REFERENCES `student` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE `selection` ADD CONSTRAINT `FK_course_id` FOREIGN KEY (`course_id`) REFERENCES `course` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE `selection` ADD CONSTRAINT `FK_teacher_id` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
```
以上是一个简单的选课管理系统的 MySQL 语言示例,供参考。实际开发中还需要考虑数据表设计、业务逻辑等方面的问题。
阅读全文