1. 创建数据库tb_school 2. 创建数据表t_institution、t_class、t_student、t_teacher、t_course 3. 向各个表中插入对应的数据,并能够通过select * 查询表中所有数据
时间: 2024-09-26 14:02:50 浏览: 59
首先,我们来创建名为`tb_school`的数据库,如果它不存在的话:
```sql
CREATE DATABASE IF NOT EXISTS tb_school;
USE tb_school; -- 切换到新创建的数据库
```
接下来,我们将分别创建五个数据表:
1. `t_institution`
```sql
CREATE TABLE t_institution (
id INT PRIMARY KEY,
name VARCHAR(100),
address VARCHAR(255)
);
```
2. `t_class`
```sql
CREATE TABLE t_class (
id INT PRIMARY KEY,
institution_id INT,
name VARCHAR(50),
FOREIGN KEY (institution_id) REFERENCES t_institution(id)
);
```
3. `t_student`
```sql
CREATE TABLE t_student (
id INT PRIMARY KEY,
class_id INT,
name VARCHAR(50),
FOREIGN KEY (class_id) REFERENCES t_class(id)
);
```
4. `t_teacher`
```sql
CREATE TABLE t_teacher (
id INT PRIMARY KEY,
institution_id INT,
name VARCHAR(50),
FOREIGN KEY (institution_id) REFERENCES t_institution(id)
);
```
5. `t_course`
```sql
CREATE TABLE t_course (
id INT PRIMARY KEY,
teacher_id INT,
name VARCHAR(50),
FOREIGN KEY (teacher_id) REFERENCES t_teacher(id)
);
```
现在我们可以向这些表中插入一些示例数据。这里展示部分插入操作,实际操作需要更多数据:
```sql
INSERT INTO t_institution (id, name, address) VALUES (1, '学校A', '地址A');
INSERT INTO t_class (id, institution_id, name) VALUES (1, 1, '班级1');
... // 同理插入其他表的数据
-- 为了演示查询所有数据,我们可以只查询一条记录,但在实际应用中会查询全部
SELECT * FROM t_institution LIMIT 1;
```
阅读全文