我正在使用PostgreSQL,我正在尝试这样做:
我有一张地图和一些特定的地标。地标可以是3种不同的东西,比如X、Y或Z。
如果只有X,我会创建一个关联表,如下所示
create table maps_points
(
map_id int NOT NULL FOREIGN KEY to maps.map_id,
x_id int NOT NULL FOREIGN KEY to xs.x_id
);
但由于地标可以指向3个不同的表,我不知道如何实现它。我心目中的解决方案是创建3列,将其中一列设置为空,其他列设置为空,如下所示:
create table maps_points
(
map_id int NOT NULL FOREIGN KEY to maps.map_id,
x_id int FOREIGN KEY to xs.x_id,
y_id int FOREIGN KEY to ys.y_id,
z_id int FOREIGN KEY to zs.z_id,
);
select * from maps_points mp
left join xs on mp.x_id = xs.x_id
left join ys on mp.y_id = ys.y_id
left join zs on mp.z_id = zs.z_id
where map_id = $1
有没有更好的方法
one of