我有一个简单的数据库:
CREATE TABLE workpiece
(
id serial primary key
-- More columns, not important here
);
CREATE TABLE workequipment
(
id serial primary key
-- More columns, not important here
);
CREATE TABLE workpiece_workequipment
(
workpiece_id integer not null references workpiece(id),
workequipment_id integer not null references workequipment(id),
primary key(workpiece_id, workequipment_id)
);
但现在需求发生变化:我必须包含类型 workequipment(如工具,测量设备等)也是如此 并强制我们不能在每个工件上多次使用相同类型的工件设备。
所以我想出了以下内容:
CREATE TABLE workpiece
(
id serial primary key
-- More columns, not important here
);
CREATE TABLE workequipment
(
id serial primary key,
equipment_type integer, -- An enum in real world
-- More columns, not important here
unique(id, equipment_type)
);
CREATE TABLE workpiece_workequipment
(
workpiece_id integer not null references workpiece(id),
workequipment_id integer not null,
workequipment_type integer not null,
primary key(workpiece_id, workequipment_id),
foreign key(workequipment_id, workequipment_type) references workequipment(id, equipment_type),
unique(workpiece_id, workequipment_type)
);
使用这种冗余来强制执行约束是否可以? 如果是,我应该在表workequipment中删除唯一(id,equipment_type) 而使(id,equipment_type)成为主键? 或者是否有更好的解决方案?
答案 0 :(得分:2)
您需要功能唯一索引:
CREATE TABLE workpiece
(
id serial primary key
-- More columns, not important here
);
CREATE TABLE workequipment
(
id serial primary key,
equipment_type integer
-- More columns, not important here
);
CREATE TABLE workpiece_workequipment
(
workpiece_id integer not null references workpiece(id),
workequipment_id integer not null references workequipment(id),
primary key(workpiece_id, workequipment_id)
);
-- Magic starts here :)
create function get_workequipment_type(int) returns int immutable language sql as $$
select equipment_type from workequipment where id = $1
$$;
create unique index idx_check_wetype_unique
on workpiece_workequipment(workpiece_id, get_workequipment_type(workequipment_id));
测试:
insert into workpiece values(default);
insert into workequipment(equipment_type) values(1),(1),(2);
insert into workpiece_workequipment values(1,1),(1,3); -- Works
--insert into workpiece_workequipment values(1,1),(1,2); -- Fails
答案 1 :(得分:1)
这非常难看,但我也无法提出更好的解决方案。
您必须同时保留workequipment
上的主键和唯一约束,否则您最终可能会遇到重复的id
。