用MySQL写一个教务系统案例包括数据库、表、数据操纵、视图、索引、存储过程、存储函数、触发器、事件、事务、数据库用户及权限分配、恢复与备份
时间: 2023-08-07 12:05:55 浏览: 232
教务管理系统实例程序(含数据库)
好的,我会为您提供一个简单的教务系统案例,包括数据库设计、表设计、数据操纵、视图、索引、存储过程、存储函数、触发器、事件、事务、数据库用户及权限分配、恢复与备份。
1. 数据库设计
教务系统的数据库设计包括以下几个表:
- 学生表(student):存储学生的姓名、学号、性别、出生日期、班级等信息。
- 教师表(teacher):存储教师的姓名、工号、性别、职称等信息。
- 课程表(course):存储课程的名称、课程编号、学分、教师编号等信息。
- 成绩表(score):存储学生的成绩信息,包括学生编号、课程编号、成绩等信息。
- 班级表(class):存储班级的名称、班级编号、专业等信息。
2. 表设计
根据上述设计,我们可以创建以下表:
- 学生表
```
CREATE TABLE student (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
gender ENUM('男', '女'),
birthday DATE,
class_id INT UNSIGNED NOT NULL,
FOREIGN KEY (class_id) REFERENCES class(id)
);
```
- 教师表
```
CREATE TABLE teacher (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
gender ENUM('男', '女'),
title VARCHAR(20)
);
```
- 课程表
```
CREATE TABLE course (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
credit FLOAT UNSIGNED,
teacher_id INT UNSIGNED NOT NULL,
FOREIGN KEY (teacher_id) REFERENCES teacher(id)
);
```
- 成绩表
```
CREATE TABLE score (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
student_id INT UNSIGNED NOT NULL,
course_id INT UNSIGNED NOT NULL,
score FLOAT UNSIGNED,
FOREIGN KEY (student_id) REFERENCES student(id),
FOREIGN KEY (course_id) REFERENCES course(id)
);
```
- 班级表
```
CREATE TABLE class (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
major VARCHAR(50)
);
```
3. 数据操纵
插入数据:
```
INSERT INTO student (name, gender, birthday, class_id) VALUES ('张三', '男', '1999-01-01', 1);
INSERT INTO teacher (name, gender, title) VALUES ('李四', '男', '教授');
INSERT INTO course (name, credit, teacher_id) VALUES ('数据库原理', 3.0, 1);
INSERT INTO score (student_id, course_id, score) VALUES (1, 1, 90.0);
INSERT INTO class (name, major) VALUES ('计算机科学与技术1班', '计算机科学与技术');
```
查询数据:
```
-- 查询学生
SELECT * FROM student;
-- 查询教师
SELECT * FROM teacher;
-- 查询课程
SELECT * FROM course;
-- 查询成绩
SELECT * FROM score;
-- 查询班级
SELECT * FROM class;
```
更新数据:
```
-- 更新学生信息
UPDATE student SET name = '李四', gender = '女' WHERE id = 1;
-- 更新教师信息
UPDATE teacher SET name = '王五' WHERE id = 1;
-- 更新课程信息
UPDATE course SET credit = 4.0 WHERE id = 1;
-- 更新成绩信息
UPDATE score SET score = 80.0 WHERE id = 1;
-- 更新班级信息
UPDATE class SET name = '计算机科学与技术2班' WHERE id = 1;
```
删除数据:
```
-- 删除学生
DELETE FROM student WHERE id = 1;
-- 删除教师
DELETE FROM teacher WHERE id = 1;
-- 删除课程
DELETE FROM course WHERE id = 1;
-- 删除成绩
DELETE FROM score WHERE id = 1;
-- 删除班级
DELETE FROM class WHERE id = 1;
```
4. 视图
创建视图:
```
CREATE VIEW student_view AS
SELECT s.id, s.name, s.gender, s.birthday, c.name AS class_name
FROM student s
JOIN class c ON s.class_id = c.id;
```
查询视图:
```
SELECT * FROM student_view;
```
5. 索引
为表创建索引:
```
CREATE INDEX idx_student_name ON student (name);
```
删除索引:
```
DROP INDEX idx_student_name ON student;
```
6. 存储过程
创建存储过程:
```
DELIMITER //
CREATE PROCEDURE get_student(IN student_id INT)
BEGIN
SELECT * FROM student WHERE id = student_id;
END //
DELIMITER ;
```
调用存储过程:
```
CALL get_student(1);
```
7. 存储函数
创建存储函数:
```
DELIMITER //
CREATE FUNCTION get_student_name(IN student_id INT)
RETURNS VARCHAR(20)
BEGIN
DECLARE student_name VARCHAR(20);
SELECT name INTO student_name FROM student WHERE id = student_id;
RETURN student_name;
END //
DELIMITER ;
```
调用存储函数:
```
SELECT get_student_name(1);
```
8. 触发器
创建触发器:
```
CREATE TRIGGER update_student_score
AFTER UPDATE ON score
FOR EACH ROW
BEGIN
UPDATE student SET score = NEW.score WHERE id = NEW.student_id;
END;
```
删除触发器:
```
DROP TRIGGER update_student_score;
```
9. 事件
创建事件:
```
CREATE EVENT backup_database
ON SCHEDULE EVERY 1 WEEK
DO
BEGIN
-- 备份数据库
mysqldump -u root -p mydatabase > mydatabase_backup.sql;
END;
```
删除事件:
```
DROP EVENT backup_database;
```
10. 事务
开启事务:
```
START TRANSACTION;
```
提交事务:
```
COMMIT;
```
回滚事务:
```
ROLLBACK;
```
11. 数据库用户及权限分配
创建用户:
```
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
```
授权:
```
GRANT SELECT, INSERT, UPDATE, DELETE ON mydatabase.* TO 'myuser'@'localhost';
```
撤销权限:
```
REVOKE SELECT, INSERT, UPDATE, DELETE ON mydatabase.* FROM 'myuser'@'localhost';
```
删除用户:
```
DROP USER 'myuser'@'localhost';
```
12. 恢复与备份
备份数据库:
```
mysqldump -u root -p mydatabase > mydatabase_backup.sql;
```
恢复数据库:
```
mysql -u root -p mydatabase < mydatabase_backup.sql;
```
以上是一个简单的教务系统案例,包括数据库设计、表设计、数据操纵、视图、索引、存储过程、存储函数、触发器、事件、事务、数据库用户及权限分配、恢复与备份。
阅读全文