请简单的写一个基于sql server期末成绩表
时间: 2024-06-12 13:07:37 浏览: 117
以下是一个基于SQL Server的期末成绩表的简单示例:
表格名称:FinalGrades
列名:StudentID, CourseID, Grade
- StudentID:整数类型,表示学生的唯一身份标识符
- CourseID:整数类型,表示课程的唯一身份标识符
- Grade:浮点数类型,表示学生在该课程中的成绩
示例SQL查询:
创建FinalGrades表格:
CREATE TABLE FinalGrades (
StudentID INT NOT NULL,
CourseID INT NOT NULL,
Grade FLOAT NOT NULL,
PRIMARY KEY (StudentID, CourseID)
);
插入数据:
INSERT INTO FinalGrades (StudentID, CourseID, Grade)
VALUES (1, 101, 85.5),
(1, 102, 90.2),
(2, 101, 92.7),
(2, 102, 88.1),
(3, 101, 78.9),
(3, 102, 93.6);
查询所有学生的成绩:
SELECT StudentID, CourseID, Grade
FROM FinalGrades
ORDER BY StudentID, CourseID;
查询特定学生的成绩:
SELECT StudentID, CourseID, Grade
FROM FinalGrades
WHERE StudentID = 1;
查询特定课程的成绩:
SELECT StudentID, CourseID, Grade
FROM FinalGrades
WHERE CourseID = 101;
计算每个学生的平均成绩:
SELECT StudentID, AVG(Grade) AS AverageGrade
FROM FinalGrades
GROUP BY StudentID;
计算每个课程的平均成绩:
SELECT CourseID, AVG(Grade) AS AverageGrade
FROM FinalGrades
GROUP BY CourseID;
阅读全文