利用sql server语言创建一个学生管理系统数据库,包括学生表stu(学号,姓名,性别,年龄,系别),课程表course(课程号,课程名,开课地点,任课老师),选课表score(学号,课程号,成绩),教师表teacher(教师编号,教师姓名,职称)和教授表teaching(课程号,教师编号,开课时间,开课地点),同时插入数据,再创建上述表的触发器和存储过程
时间: 2024-03-25 12:41:43 浏览: 158
好的,以下是创建学生管理系统数据库的 SQL 语句:
```
--创建学生表stu
CREATE TABLE stu (
sno CHAR(10) PRIMARY KEY,
sname VARCHAR(20),
sgender VARCHAR(2),
sage INT,
sdept VARCHAR(20)
)
--创建课程表course
CREATE TABLE course (
cno CHAR(10) PRIMARY KEY,
cname VARCHAR(20),
clocation VARCHAR(20),
cteacher VARCHAR(20)
)
--创建选课表score
CREATE TABLE score (
sno CHAR(10),
cno CHAR(10),
grade INT,
PRIMARY KEY (sno, cno),
FOREIGN KEY (sno) REFERENCES stu(sno),
FOREIGN KEY (cno) REFERENCES course(cno)
)
--创建教师表teacher
CREATE TABLE teacher (
tno CHAR(5) PRIMARY KEY,
tname VARCHAR(20),
ttitle VARCHAR(20)
)
--创建教授表teaching
CREATE TABLE teaching (
cno CHAR(10),
tno CHAR(5),
ctime VARCHAR(20),
clocation VARCHAR(20),
PRIMARY KEY (cno, tno),
FOREIGN KEY (cno) REFERENCES course(cno),
FOREIGN KEY (tno) REFERENCES teacher(tno)
)
--插入数据
INSERT INTO stu VALUES ('20190001', '张三', '男', 20, '计算机科学与技术');
INSERT INTO stu VALUES ('20190002', '李四', '女', 21, '软件工程');
INSERT INTO stu VALUES ('20190003', '王五', '男', 22, '信息安全');
INSERT INTO course VALUES ('C001', '数据库原理', '教学楼101', '张老师');
INSERT INTO course VALUES ('C002', '算法设计与分析', '教学楼201', '李老师');
INSERT INTO course VALUES ('C003', '操作系统', '教学楼301', '王老师');
INSERT INTO score VALUES ('20190001', 'C001', 90);
INSERT INTO score VALUES ('20190001', 'C002', 85);
INSERT INTO score VALUES ('20190002', 'C001', 92);
INSERT INTO score VALUES ('20190002', 'C002', 88);
INSERT INTO score VALUES ('20190003', 'C003', 80);
INSERT INTO teacher VALUES ('T001', '张老师', '副教授');
INSERT INTO teacher VALUES ('T002', '李老师', '教授');
INSERT INTO teacher VALUES ('T003', '王老师', '讲师');
INSERT INTO teaching VALUES ('C001', 'T001', '周一上午', '教学楼101');
INSERT INTO teaching VALUES ('C002', 'T002', '周三下午', '教学楼201');
INSERT INTO teaching VALUES ('C003', 'T003', '周五上午', '教学楼301');
```
接下来是触发器和存储过程的创建:
```
--创建插入学生表stu的触发器
CREATE TRIGGER insert_stu_trigger
ON stu
FOR INSERT
AS
BEGIN
PRINT 'A new record has been inserted into the stu table.'
END
--创建更新教授表teaching的触发器
CREATE TRIGGER update_teaching_trigger
ON teaching
FOR UPDATE
AS
BEGIN
PRINT 'The teaching table has been updated.'
END
--创建查询选修某门课程的学生的存储过程
CREATE PROCEDURE get_students_by_course
@cno CHAR(10)
AS
BEGIN
SELECT s.sno, s.sname, s.sdept, sc.grade
FROM score sc
JOIN stu s ON sc.sno = s.sno
WHERE sc.cno = @cno
END
```
以上就是创建学生管理系统数据库的 SQL 语句,包括表的创建、数据的插入、触发器的创建和存储过程的创建。
阅读全文