将新集对传播到表中的所有子对(PostgreSQL) [英] Propagate new set pairs to all children pairs in a table (PostgreSQL)

查看:133
本文介绍了将新集对传播到表中的所有子对(PostgreSQL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于以下的表:

I have a table which is similar to the following:

serial,integer1,integer2

integer1!= integer2 的约束,(1,2) code>和(2,1)不能共存(因为它们是相同的集合,以不同的顺序)。

There is constraint that integer1 != integer2, and the sets (1, 2) and (2, 1) cannot coexist (because they are the same set, in a different order).

有一个简单的方法,所以我可以做以下:

Is there an easy way so that I can do the following:

当我插入一个标识符对时,它将传播关系到所有每个标识符的其他对。例如:

When ever I insert an identifier pair, it will propagate the relationship down to all of each identifier's other pairs. For instance:

开头为:

(1, 2)
(3, 4)



如果我插入 3);自动使(1,4) (2,4)(2,3) 。会很好,如果有一些选择欺骗可以执行,但我认为它可能需要一个触发器实际上物理创建这些行。对此最好的方法有什么建议?

If I insert (1, 3); automatically make (1, 4) (2, 4) and (2, 3). Would be nice if there is some select trickery that can be performed, but I think it will probably need to be a trigger to actually physically create those rows. Any suggestions for the best way to do this?

我想要超级递归。

推荐答案

就我个人而言,最好的选择是创建一个表和一个表序列。组表可以是您说的整数1和2是组A的一部分 整数2,3和4是组B 的一部分。序列表将是一个组和相关序列中所有坐标的列表。

Personally, I'd say the best option is to create a table group, and a table serial. The group table would be where you say integer 1 and 2 are part of the group A and integer 2, 3, and 4 are part of the group B. The serial table would be a list of all "coordinates" within a group and the associated serial.

从这里,您可以在插入时在组表上触发,删除和更新,将更改传播到序列表(禁用现在已删除的坐标或删除它们,然后使用 null 或默认序列创建新条目,甚至可以通过存储过程或序列表上的触发器生成)。

From there, you can make a trigger on your group table when inserting, deleting and updating, where you propagate the changes to your serial table (either disable the now extinct coordinates or delete them, and then create new entries with a null or default serial, could even be generated through a stored procedure or a trigger on the serial table).

我将使用组的原因是允许您更轻松地管理删除。如果你不拆分表,那么从A组中删除1是很粗糙的。

The reason that I would work with groups is because it allows you to manage deletion more easily. If you don't split the table, it's gonna be rough to remove 1 from the group A.

基本上:


  • 表格整数(如果适用):整数 +其他有价值的数据

  • 表格组整数和整数):整数,组

  • 表序列(整数和整数之间的实际连接表): Integer,Integer Serial +其他有价值的数据。所有插入和删除操作都应通过表格组上的触发器自动完成。

  • Table integer (if appropriate) : Integer + other valuable data
  • Table group (a pseudo-junction table between integer and integer) : Integer, Group
  • Table Serial (the actual junction table between integer and integer) : Integer, Integer, Serial + other valuable data. All inserts and removes should be done automatically by triggers on the table group.

这篇关于将新集对传播到表中的所有子对(PostgreSQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆