利用sql server语言对一个学生管理系统数据库,包括学生表stu(学号,姓名,性别,年龄,系别),课程表course(课程号,课程名,开课地点,任课老师),选课表score(学号,课程号,成绩),教师表teacher(教师编号,教师姓名,职称)和教授表teaching(课程号,教师编号,开课时间,开课地点),创建上述表的触发器和存储过程
时间: 2024-03-25 12:42:12 浏览: 160
1. 创建学生表:
```sql
CREATE TABLE stu (
stu_id INT PRIMARY KEY,
stu_name VARCHAR(50),
stu_gender VARCHAR(10),
stu_age INT,
stu_dept VARCHAR(50)
);
```
2. 创建课程表:
```sql
CREATE TABLE course (
course_id INT PRIMARY KEY,
course_name VARCHAR(50),
course_location VARCHAR(50),
course_teacher VARCHAR(50)
);
```
3. 创建选课表:
```sql
CREATE TABLE score (
stu_id INT,
course_id INT,
score INT,
PRIMARY KEY (stu_id, course_id),
FOREIGN KEY (stu_id) REFERENCES stu(stu_id),
FOREIGN KEY (course_id) REFERENCES course(course_id)
);
```
4. 创建教师表:
```sql
CREATE TABLE teacher (
teacher_id INT PRIMARY KEY,
teacher_name VARCHAR(50),
teacher_title VARCHAR(50)
);
```
5. 创建教授表:
```sql
CREATE TABLE teaching (
course_id INT,
teacher_id INT,
course_time VARCHAR(50),
course_location VARCHAR(50),
PRIMARY KEY (course_id, teacher_id),
FOREIGN KEY (course_id) REFERENCES course(course_id),
FOREIGN KEY (teacher_id) REFERENCES teacher(teacher_id)
);
```
6. 创建触发器,当学生表stu插入一条记录时,自动向教师表teacher插入一条记录:
```sql
CREATE TRIGGER add_teacher
ON stu
AFTER INSERT
AS
BEGIN
INSERT INTO teacher (teacher_id, teacher_name, teacher_title)
VALUES (NEWID(), inserted.stu_name, '教师');
END;
```
7. 创建存储过程,根据学生的成绩查询其所选的课程信息:
```sql
CREATE PROCEDURE get_score_course
@stu_id INT
AS
BEGIN
SELECT c.course_name, c.course_location, t.teacher_name, s.score
FROM course c
JOIN teaching t ON c.course_id = t.course_id
JOIN score s ON c.course_id = s.course_id
WHERE s.stu_id = @stu_id;
END;
```
以上是一个简单的学生管理系统的数据库设计,包括了学生表、课程表、选课表、教师表和教授表。同时,我们还创建了一个触发器,当学生表stu插入一条记录时,自动向教师表teacher插入一条记录;以及一个存储过程,根据学生的成绩查询其所选的课程信息。
阅读全文