如何建立三张表并插入数据
时间: 2023-06-06 15:07:41 浏览: 108
利用三层进行插入数据操作
可以使用SQL语句来建立三张表并插入数据,具体如下:
建立表1:employee,包含字段id、name、age、gender、salary。
CREATE TABLE employee (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
gender VARCHAR(10),
salary FLOAT
);
向表1中插入数据:
INSERT INTO employee (id, name, age, gender, salary)
VALUES (1, '张三', 25, '男', 10000),
(2, '李四', 30, '男', 12000),
(3, '王五', 28, '女', 9000);
建立表2:department,包含字段id、name、location。
CREATE TABLE department (
id INT PRIMARY KEY,
name VARCHAR(50),
location VARCHAR(50)
);
向表2中插入数据:
INSERT INTO department (id, name, location)
VALUES (1, '人事部', '北京'),
(2, '财务部', '上海'),
(3, '技术部', '深圳');
建立表3:employee_department,包含字段id、employee_id、department_id。
CREATE TABLE employee_department (
id INT PRIMARY KEY,
employee_id INT,
department_id INT,
FOREIGN KEY (employee_id) REFERENCES employee(id),
FOREIGN KEY (department_id) REFERENCES department(id)
);
向表3中插入数据:
INSERT INTO employee_department (id, employee_id, department_id)
VALUES (1, 1, 1),
(2, 2, 2),
(3, 3, 3);
阅读全文