创建数据库学校,数据表,学院,班级,学生,教师,课程,查询包含班级名称的学生信息,查询包含学院名称的班级信息
时间: 2024-10-21 08:05:39 浏览: 29
创建数据库学校(School)主要包括以下几个步骤:
1. 数据库创建:
```sql
CREATE DATABASE SchoolDB;
```
2. 使用数据库:
```sql
USE SchoolDB;
```
接下来,我们需要创建数据表:
- 学院 (College)
```sql
CREATE TABLE College (
ID INT PRIMARY KEY,
Name VARCHAR(50),
SchoolID INT FOREIGN KEY REFERENCES School(ID)
);
```
- 班级 (Class)
```sql
CREATE TABLE Class (
ID INT PRIMARY KEY,
ClassName VARCHAR(50),
CollegeID INT FOREIGN KEY REFERENCES College(ID),
TeacherID INT FOREIGN KEY REFERENCES Teacher(ID)
);
```
- 教师 (Teacher)
```sql
CREATE TABLE Teacher (
ID INT PRIMARY KEY,
Name VARCHAR(50),
SchoolID INT FOREIGN KEY REFERENCES School(ID)
);
```
- 学生 (Student)
```sql
CREATE TABLE Student (
ID INT PRIMARY KEY,
Name VARCHAR(50),
ClassID INT FOREIGN KEY REFERENCES Class(ID),
SchoolID INT FOREIGN KEY REFERENCES School(ID)
);
```
- 课程 (Course)
```sql
CREATE TABLE Course (
ID INT PRIMARY KEY,
ClassName VARCHAR(50),
TeacherID INT FOREIGN KEY REFERENCES Teacher(ID)
);
```
查询包含班级名称的学生信息:
```sql
SELECT * FROM Student WHERE ClassID IN (SELECT ID FROM Class WHERE ClassName = '指定班级名称');
```
查询包含学院名称的班级信息:
```sql
SELECT Class.ClassName FROM Class JOIN College ON Class.CollegeID = College.ID WHERE College.Name = '指定学院名称';
```
阅读全文