使用SQL语句自定义创建两张数据表,并向数据表中添加测试数据(五条)
时间: 2024-10-21 12:17:46 浏览: 53
当然可以,这里有一个简单的例子,我们将使用SQL语句来创建两个表格并添加一些测试数据。假设我们要创建一个名为`Employees`的员工信息表和一个名为`Departments`的部门信息表。
首先,创建`Departments`表:
```sql
CREATE TABLE Departments (
id INT PRIMARY KEY,
name VARCHAR(50)
);
```
然后,我们添加部门信息作为测试数据(例如,ID 1的"Sales"部门,ID 2的"Engineering"部门):
```sql
INSERT INTO Departments (id, name) VALUES (1, 'Sales');
INSERT INTO Departments (id, name) VALUES (2, 'Engineering');
```
接下来,创建`Employees`表,包含员工ID、姓名以及所属部门ID:
```sql
CREATE TABLE Employees (
employee_id INT PRIMARY KEY,
name VARCHAR(50),
department_id INT,
FOREIGN KEY (department_id) REFERENCES Departments(id)
);
```
最后,我们插入五条测试员工数据,关联到上面的部门:
```sql
INSERT INTO Employees (employee_id, name, department_id) VALUES (1, 'John Doe', 1);
INSERT INTO Employees (employee_id, name, department_id) VALUES (2, 'Jane Smith', 1);
INSERT INTO Employees (employee_id, name, department_id) VALUES (3, 'Bob Johnson', 2);
INSERT INTO Employees (employee_id, name, department_id) VALUES (4, 'Alice Williams', 2);
INSERT INTO Employees (employee_id, name, department_id) VALUES (5, 'Charlie Brown', 2);
```
这就是如何使用SQL语句创建表并添加测试数据的基本示例。记得根据实际需求调整字段类型和长度。
阅读全文