加入三张表postgres?

时间:2014-06-02 13:21:36

标签: sql database postgresql

我有三张桌子(PostgreSQL):

PUBBLICATIONS

(pub_id            SERIAL                                   ,
 creation          DATE NOT NULL DEFAULT ('now'::text)::date,
 pub_uuid          CHARACTER VARYING PRIMARY KEY    NOT NULL,
 inv_uuid          CHARACTER VARYING                        )

INVESTIGATIONS

(inv_id            SERIAL                                   ,
 creation          DATE NOT NULL DEFAULT ('now'::text)::date,
 inv_uuid          CHARACTER VARYING PRIMARY KEY    NOT NULL,
 pub_uid           CHARACTER VARYING                        )

RUNDETAILS

(run_id            SERIAL                                   ,
 creation          DATE NOT NULL DEFAULT ('now'::text)::date,
 run_uuid          CHARACTER VARYING PRIMARY KEY    NOT NULL,
 inv_uuid          CHARACTER VARYING REFERENCES investigations(inv_uuid) )

我想创建(并在每次数据库中有新条目时自动更新)包含*_uuid的表。类似的东西:

RUN_INV_PUB

(run_id            SERIAL                                   ,
 creation          DATE NOT NULL DEFAULT ('now'::text)::date,
 run_uuid          CHARACTER VARYING                        ,
 inv_uuid          CHARACTER VARYING                        ,   
 pub_uuid          CHARACTER VARYING                        )   

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

PostgreSQL支持触发器。

触发器为数据库开发人员提供了一种机制,可以在修改特定表的内容时运行代码,例如在插入时。

您可以编写一个触发器,它会注意到表中插入的新数据(或旧日期已删除),然后将适当的数据插入另一个表中。

您可以在PostreSQL网站上找到此示例:PostgreSQL docs - section 36.4 - A Complete Trigger Example