自动更新数据库中的字段
我想用另一列的聚合自动更新数据库列。 涉及三个表:
T_RIDER
RIDER_ID
TMP_PONYLIST
...
T_RIDER_PONY
RIDER_ID
PONY_ID
T_PONY
PONY_ID
PONY_NAME
...
T_RIDER
和 T_PONY
通过 T_RIDER_PONY
具有 n:m 关系。 T_RIDER
和 T_PONY
还有一些列,但只有 TMP_PONYLIST
和 PONY_NAME
与此处相关。
TMP_PONYLIST
是一个以分号分隔的 PONY_NAMES
列表,想象一下类似 “Twisty Tail;Candy Cane;Lucky Leaf”
的内容。 无论 T_RIDER_PONY
或 T_PONY
发生什么情况,我都希望使该字段保持最新状态。
我的第一个想法是在 T_RIDER_PONY
和 T_PONY
上设置触发器。 问题是似乎无法在触发器内读取 T_RIDER_PONY
,我总是得到 ORA-04091
。我发现了一些有关使用三个触发器和包变量的提示,但这听起来太复杂了。
也许您认为我最好更改架构或完全摆脱 TMP_PONYLIST
。 这些是选项,但不是这个问题的主题。 目前我只对不需要对我的应用程序进行任何更改的答案感兴趣(没有应用程序直接使用表,只能使用视图,因此允许使用视图进行欺骗)。
那么,如何让 TMP_PONYLIST
自动保持最新状态? 如何连接字符串是一个有趣的子问题,我还没有找到一个优雅的解决方案。
我正在使用 Oracle 数据库 10g 企业版版本 10.2.0.4.0 - 64bi。
更新
我喜欢使用物化视图的想法。 我所拥有的是:
CREATE MATERIALIZED VIEW
V_TMP_PONYLIST
BUILD IMMEDIATE
REFRESH COMPLETE ON COMMIT
AS SELECT
R.RIDER_ID, string_agg(P.PONY_NAME) AS TMP_PONYLIST
FROM
T_PONY P, T_RIDER R, T_RIDER_PONY RP
WHERE
P.PLOTGROUP_ID=RP.PLOTGROUP_ID AND
R.QUEUE_ID=RP.QUEUE_ID
GROUP BY R.RIDER_ID;
string_agg
未显示,因为它很长并且我认为它不相关。
它不会使用 ON COMMIT
进行编译,我得到 ORA-12054
。 据我了解,文档聚合仅被禁止 刷新速度快
,那么这里有什么问题呢?
更新 文森特和托尼的答案不同,但都很有帮助。 我接受了托尼的,但也请务必阅读文森特的回答。
I'd like to automatically update a database column with an aggregate of another column.
There are three tables involved:
T_RIDER
RIDER_ID
TMP_PONYLIST
...
T_RIDER_PONY
RIDER_ID
PONY_ID
T_PONY
PONY_ID
PONY_NAME
...
T_RIDER
and T_PONY
have an n:m relationship via T_RIDER_PONY
.T_RIDER
and T_PONY
have some more columns but only TMP_PONYLIST
and PONY_NAME
are relevant here.
TMP_PONYLIST
is a semicolon spararated list of PONY_NAMES
, imagine something like "Twisty Tail;Candy Cane;Lucky Leaf"
.
I'd like to keep this field up to date no matter what happens to T_RIDER_PONY
or T_PONY
.
My first idea was to have triggers on T_RIDER_PONY
and T_PONY
.
The problem is that it seems to be impossible to read T_RIDER_PONY
within the trigger, I always get ORA-04091
. I found some hints about working with three triggers and package variables, but this sounds way too complicated.
Maybe you think I'd better change the schema or get rid of TMP_PONYLIST
completely.
These are options but not the topic of this question.
For the moment I'm only interested in answers that don't require any change to my applications (no application works with the tables directly, only views, so trickery with views is allowed).
So, how can I keep TMP_PONYLIST
up to date automatically?
How to concatenate the string is an interesting subproblem I also have not yet found an elegant solution for.
I'm using Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi.
UPDATE
I like the idea of using a materialized view.
What I have is:
CREATE MATERIALIZED VIEW
V_TMP_PONYLIST
BUILD IMMEDIATE
REFRESH COMPLETE ON COMMIT
AS SELECT
R.RIDER_ID, string_agg(P.PONY_NAME) AS TMP_PONYLIST
FROM
T_PONY P, T_RIDER R, T_RIDER_PONY RP
WHERE
P.PLOTGROUP_ID=RP.PLOTGROUP_ID AND
R.QUEUE_ID=RP.QUEUE_ID
GROUP BY R.RIDER_ID;
string_agg
is not shown because it is long and the I think it is not relevant.
It won't compile with ON COMMIT
, I get ORA-12054
.
As I understand the documentation aggregates are only forbidden withREFRESH FAST
, so what's the problem here?
UPDATE
Vincents and Tonys answers were different but both helpful.
I accepted Tonys but be sure to read Vincents answer also.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TMP_PONYLIST
列上的信息是多余的(它存在于其他地方)。您将遇到维护它的各种问题(除非有某种锁定机制,否则任何解决方案都无法在多用户环境中正常工作)。在标准化模型中,您只需从物理模型中删除此列即可。如果您需要这些信息,可以使用视图,例如 Oracle 11gR2:
请参阅 此 SO 例如 11gR2 之前的字符串聚合。
the information on the column
TMP_PONYLIST
is redundant (it exists somewhere else). You will get into all sorts of problems to maintain it (No solution will work correctly in a multi-user environment unless there is some sort of locking mechanism).In a normalized model you would simply drop this column from the physical model. If you need the information, you could use a view, for example with Oracle 11gR2:
See this SO for example of string aggregation before 11gR2.
为什么需要在触发器中读取T_RIDER_PONY表?您将插入或删除一行,因此触发器需要做的就是在 T_PONY 中查找小马名称并更新表 T_RIDER,将名称附加到 TMP_PONYLIST 或从 TMP_PONYLIST 中删除它,如下所示
删除部分中的更新是相当的我承认这令人不愉快;最好使用 apex_util.string_to_table 和 apex_util.table_to_string 等实用程序来处理它!有关详细信息,请参阅此答案。
Why do you need to read the T_RIDER_PONY table in the trigger? You will have either inserted or deleted a row, so all the trigger needs to do is look up the pony name in T_PONY and update table T_RIDER, either appending the name to or removing it from TMP_PONYLIST like this
That update in the delete section is rather unpleasant I admit; it might be preferable to use utilities like apex_util.string_to_table and apex_util.table_to_string to deal with it! See this SO answer for details.