某工厂的物资管理系统涉及的实体有: 仓库:属性有仓库号、面积、电话号码 零件:属性有零件号、名称、单价 供应商:属性有供应商号、姓名、地址、电话 项目:属性有项目号、预算、开工日期 职工:属性有职工号、姓名、年龄 其中,一个仓库可以存放多种零件,一种零件可以存放在多个仓库中,用库存量来表示某种零件在某个仓库中的数量。一个仓库有多个职工当仓库保管员,一个职工只能在一个仓库工作。 供应商、项目和零件三者之间具有多对多的联系。即一个供应商可以供给若干项目多种零件,每个项目可以使用不同供应商供应的零件,写出sql语句
时间: 2023-07-14 13:12:16 浏览: 117
以下是涉及到的实体及其属性:
- 仓库:仓库号(warehouse_id)、面积(area)、电话号码(phone)
- 零件:零件号(part_id)、名称(name)、单价(price)
- 供应商:供应商号(supplier_id)、姓名(name)、地址(address)、电话(phone)
- 项目:项目号(project_id)、预算(budget)、开工日期(start_date)
- 职工:职工号(employee_id)、姓名(name)、年龄(age)
根据题目要求,可以得到以下表格:
- 仓库表(warehouse):warehouse_id、area、phone
- 零件表(part):part_id、name、price
- 供应商表(supplier):supplier_id、name、address、phone
- 项目表(project):project_id、budget、start_date
- 职工表(employee):employee_id、name、age、warehouse_id
此外,还需要一张关联表来表示供应商、项目和零件之间的联系:
- 供应商-项目-零件关联表(supplier_project_part):supplier_id、project_id、part_id
以下是针对题目要求的 SQL 语句:
1. 创建表格
```
CREATE TABLE warehouse (
warehouse_id INT PRIMARY KEY,
area FLOAT,
phone VARCHAR(20)
);
CREATE TABLE part (
part_id INT PRIMARY KEY,
name VARCHAR(50),
price FLOAT
);
CREATE TABLE supplier (
supplier_id INT PRIMARY KEY,
name VARCHAR(50),
address VARCHAR(100),
phone VARCHAR(20)
);
CREATE TABLE project (
project_id INT PRIMARY KEY,
budget FLOAT,
start_date DATE
);
CREATE TABLE employee (
employee_id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
warehouse_id INT,
FOREIGN KEY (warehouse_id) REFERENCES warehouse (warehouse_id)
);
CREATE TABLE supplier_project_part (
supplier_id INT,
project_id INT,
part_id INT,
PRIMARY KEY (supplier_id, project_id, part_id),
FOREIGN KEY (supplier_id) REFERENCES supplier (supplier_id),
FOREIGN KEY (project_id) REFERENCES project (project_id),
FOREIGN KEY (part_id) REFERENCES part (part_id)
);
```
2. 插入数据
```
INSERT INTO warehouse (warehouse_id, area, phone) VALUES (1, 1000, '123456789');
INSERT INTO warehouse (warehouse_id, area, phone) VALUES (2, 2000, '987654321');
INSERT INTO part (part_id, name, price) VALUES (1, '零件A', 10);
INSERT INTO part (part_id, name, price) VALUES (2, '零件B', 20);
INSERT INTO supplier (supplier_id, name, address, phone) VALUES (1, '供应商A', '地址A', '111111111');
INSERT INTO supplier (supplier_id, name, address, phone) VALUES (2, '供应商B', '地址B', '222222222');
INSERT INTO project (project_id, budget, start_date) VALUES (1, 1000000, '2022-01-01');
INSERT INTO project (project_id, budget, start_date) VALUES (2, 2000000, '2022-02-01');
INSERT INTO employee (employee_id, name, age, warehouse_id) VALUES (1, '职工A', 30, 1);
INSERT INTO employee (employee_id, name, age, warehouse_id) VALUES (2, '职工B', 40, 2);
INSERT INTO supplier_project_part (supplier_id, project_id, part_id) VALUES (1, 1, 1);
INSERT INTO supplier_project_part (supplier_id, project_id, part_id) VALUES (1, 1, 2);
INSERT INTO supplier_project_part (supplier_id, project_id, part_id) VALUES (2, 2, 2);
```
3. 查询某个仓库中所有零件及其库存量
```
SELECT part.name, COALESCE(SUM(inventory.quantity), 0) as inventory_count
FROM part
LEFT JOIN inventory ON part.part_id = inventory.part_id
WHERE inventory.warehouse_id = 1
GROUP BY part.part_id;
```
4. 查询某个零件在哪些仓库中存放,以及其库存量
```
SELECT warehouse.warehouse_id, warehouse.area, COALESCE(inventory.quantity, 0) as inventory_count
FROM warehouse
LEFT JOIN inventory ON warehouse.warehouse_id = inventory.warehouse_id AND inventory.part_id = 1;
```
5. 查询项目预算超过 100 万元的供应商姓名和地址
```
SELECT supplier.name, supplier.address
FROM supplier
INNER JOIN supplier_project_part ON supplier.supplier_id = supplier_project_part.supplier_id
INNER JOIN project ON supplier_project_part.project_id = project.project_id
WHERE project.budget > 1000000;
```
6. 查询所有职工的姓名和所在仓库的电话号码
```
SELECT employee.name, warehouse.phone
FROM employee
INNER JOIN warehouse ON employee.warehouse_id = warehouse.warehouse_id;
```
阅读全文