学生信息表(Student):学生号(StudentID)、姓名(Name)、性别(Gender)、出生日期(BirthDate)、联系电话(Phone)、家庭住址(Address)、班级编号(ClassID) 班级信息表(Class):班级号(ClassID)、班级名称(ClassName)、所属学院(Academy) 教师信息表(Teacher):教师号(TeacherID)、姓名(Name)、性别(Gender)、出生日期(BirthDate)、联系电话(Phone)、所属学院(Academy) 课程信息表(Course):课程号(CourseID)、课程名称(CourseName)、学分(Credit)、授课教师(teacher_name) 成绩信息表(Score):成绩号(ScoreID)、学生号(StudentID)、课程号(CourseID)、成绩(Grade)建表代码和查询语句
时间: 2023-07-24 21:50:43 浏览: 345
建表代码:
```
-- 学生信息表
CREATE TABLE Student (
StudentID INT PRIMARY KEY,
Name VARCHAR(50),
Gender CHAR(1),
BirthDate DATE,
Phone VARCHAR(20),
Address VARCHAR(100),
ClassID INT
);
-- 班级信息表
CREATE TABLE Class (
ClassID INT PRIMARY KEY,
ClassName VARCHAR(50),
Academy VARCHAR(50)
);
-- 教师信息表
CREATE TABLE Teacher (
TeacherID INT PRIMARY KEY,
Name VARCHAR(50),
Gender CHAR(1),
BirthDate DATE,
Phone VARCHAR(20),
Academy VARCHAR(50)
);
-- 课程信息表
CREATE TABLE Course (
CourseID INT PRIMARY KEY,
CourseName VARCHAR(50),
Credit INT,
teacher_name VARCHAR(50)
);
-- 成绩信息表
CREATE TABLE Score (
ScoreID INT PRIMARY KEY,
StudentID INT,
CourseID INT,
Grade FLOAT,
FOREIGN KEY (StudentID) REFERENCES Student(StudentID),
FOREIGN KEY (CourseID) REFERENCES Course(CourseID)
);
```
查询语句:
1. 查询学生信息表中所有学生的姓名、性别和班级名称
```
SELECT Student.Name, Student.Gender, Class.ClassName
FROM Student
INNER JOIN Class
ON Student.ClassID = Class.ClassID;
```
2. 查询所有学生的课程名称、课程学分和成绩
```
SELECT Student.Name, Course.CourseName, Course.Credit, Score.Grade
FROM Student
INNER JOIN Score
ON Student.StudentID = Score.StudentID
INNER JOIN Course
ON Score.CourseID = Course.CourseID;
```
3. 查询所有学生的平均成绩
```
SELECT Student.Name, AVG(Score.Grade) AS AverageGrade
FROM Student
INNER JOIN Score
ON Student.StudentID = Score.StudentID
GROUP BY Student.Name;
```
4. 查询某个学生的所有成绩
```
SELECT Course.CourseName, Score.Grade
FROM Score
INNER JOIN Course
ON Score.CourseID = Course.CourseID
WHERE Score.StudentID = [学生ID];
```
5. 查询某个教师的所有课程和授课班级
```
SELECT Course.CourseName, Class.ClassName
FROM Course
INNER JOIN Class
ON Course.teacher_name = [教师姓名] AND Class.ClassID = Course.ClassID;
```
阅读全文