创建数据库tb_school 2. 创建数据表t_institution、t_class、t_student、t_teacher、t_course查询包含班级名称的学生信息、查询包含学院名称的班级信息、教师信息、
时间: 2024-10-21 09:05:41 浏览: 49
创建数据库tb_school的SQL命令通常是这样的:
```sql
CREATE DATABASE tb_school;
```
这将创建一个新的名为tb_school的数据库。
接下来,我会分别为你创建数据表t_institution、t_class、t_student、t_teacher、t_course:
```sql
USE tb_school; -- 如果不在tb_school数据库下,先切换到这个数据库
CREATE TABLE t_institution (
id INT PRIMARY KEY,
name VARCHAR(100)
);
CREATE TABLE t_class (
class_id INT PRIMARY KEY,
institution_id INT,
name VARCHAR(100),
FOREIGN KEY (institution_id) REFERENCES t_institution(id)
);
CREATE TABLE t_student (
student_id INT PRIMARY KEY,
class_id INT,
name VARCHAR(100),
FOREIGN KEY (class_id) REFERENCES t_class(class_id)
);
CREATE TABLE t_teacher (
teacher_id INT PRIMARY KEY,
class_id INT,
name VARCHAR(100),
FOREIGN KEY (class_id) REFERENCES t_class(class_id)
);
CREATE TABLE t_course (
course_id INT PRIMARY KEY,
class_id INT,
course_name VARCHAR(100),
FOREIGN KEY (class_id) REFERENCES t_class(class_id)
);
```
要查询包含班级名称的学生信息、包含学院名称的班级信息以及教师信息,可以编写如下的SQL语句:
1. 学生信息查询(包含班级名称):
```sql
SELECT s.name AS student_name, c.name AS class_name
FROM t_student s
JOIN t_class c ON s.class_id = c.class_id;
```
2. 班级信息查询(包含学院名称):
```sql
SELECT c.name AS class_name, i.name AS institution_name
FROM t_class c
JOIN t_institution i ON c.institution_id = i.id;
```
3. 教师信息查询:
```sql
SELECT t.name AS teacher_name, c.name AS class_name
FROM t_teacher t
JOIN t_class c ON t.class_id = c.class_id;
```
阅读全文