我有以下表格
临时用户表
内容来自API
-------------------------------------
| UserId | Location | Department |
-------------------------------------
| 1 | Loc1 | Dep1 |
| 2 | Loc2 | Dep40 |
| 3 | Loc100 | Dep30 |
-------------------------------------
此处的目标是插入这些数据,但它们属于3个不同的表:
用户表
---------------------------------------
| UserId | LocationID | DepartmentID |
---------------------------------------
| 1 | 1 | 1 |
| 2 | 2 | 2 |
--------------------------------------
系
---------------------------------
| DepId | DepName |
---------------------------------
| 1 | Dep1
| 2 | Dep40
....
| 30 (new)| Dep40 --> New value
----------------------------------
地点
---------------------------------
| LocId | LocName |
---------------------------------
| 1 | Loc1
| 2 | Loc240
....
| 10 (new)| Loc100 --> New value
----------------------------------
基于这个temp_user表,我应该能够添加一个新用户并插入缺少的部门和位置以具有类似这样的内容 考虑到例如:
我的用户表应如下所示
---------------------------------------
| UserId | LocationID | DepartmentID |
---------------------------------------
| 1 | 1 | Dep1 |
| 2 | 2 | Dep40 |
| 3 | 10 | 30 |
--------------------------------------
问题
进行此插入的最佳方法是什么?我可以使用某些触发器来执行此操作,还是应该创建存储过程?什么是正确的算法呢?
答案 0 :(得分:1)
假设部门名称和位置名称是唯一的,您可以执行以下操作:
-- insert missing locations
insert into location (loc_name)
select location
from temp_users
on conflict (loc_name) do nothing;
-- insert missing departments
insert into department (dep_name)
select department
from temp_users
on conflict (dep_name) do nothing;
-- insert new users
insert into users (id, location_id, department_id)
select tu.user_id, l.id, d.id
from temp_users tu
join location l on l.loc_name = tu.location
join department d on d.dep_name = tu.department
on conflict (id) do nothing; -- ignore existing users with the same ID
以上假设所有id
列都定义为主键,location
和department
的ID是使用序列生成的(例如,将它们定义为{{1}并且serial
和location.loc_name
上定义了唯一约束。