gpt4 book ai didi

function - Postgres ON DELETE CASCADE 会在后续表上触发 ON DELETE 触发器吗?

转载 作者:行者123 更新时间:2023-11-29 12:14:21 26 4
gpt4 key购买 nike

ON DELETE CASCADE 会在后续表上触发 ON DELETE 触发器吗?

我有三个表,结果、m_int 和行计数。 M_int 与 FK 的结果相关。结果设置了 ON DELETE CASCADE,m_int 有一个 BEFORE DELETE 触发器。 Rowcount 跟踪 m_int 中使用了多少行以及由哪个用户使用。

我的问题是,当我从结果中删除一行时,我希望 DELETE CASCADE 在 m_int 上触发触发器。有任何想法吗?我整个上午都在阅读文档,但找不到答案。

peri=> \d results
Table "public.results"
Column | Type | Modifiers
-----------+---------+-----------------------------------------------------
result_id | integer | not null default nextval('result_id_seq'::regclass)
trial__id | integer | not null
title | text |
Indexes:
"results_pkey" PRIMARY KEY, btree (result_id)
Referenced by:
TABLE "m_int" CONSTRAINT "m_int_result__id_fkey" FOREIGN KEY (result__id) REFERENCES results(result_id) ON DELETE CASCADE

peri=> \d m_int
Table "public.m_int"
Column | Type | Modifiers
------------+---------+------------------------------------------------------------
metric__id | integer | not null
result__id | integer | not null
value | integer | not null
m_value_id | bigint | not null default nextval('m_int_m_value_id_seq'::regclass)
Indexes:
"m_int_pkey" PRIMARY KEY, btree (m_value_id)
Foreign-key constraints:
"m_int_metric__id_fkey" FOREIGN KEY (metric__id) REFERENCES metrics(metric_id) ON DELETE CASCADE
"m_int_result__id_fkey" FOREIGN KEY (result__id) REFERENCES results(result_id) ON DELETE CASCADE
Triggers:
addrows_m_int AFTER INSERT ON m_int FOR EACH ROW EXECUTE PROCEDURE add_rows_m_int()
remrows_m_int BEFORE DELETE ON m_int FOR EACH ROW EXECUTE PROCEDURE rem_rows_m_int()

peri=> \d rowcount
Table "public.rowcount"
Column | Type | Modifiers
------------+---------+-----------
user__id | integer | not null
table_name | text | not null
total_rows | bigint |
Indexes:
"rowcount_pkey" PRIMARY KEY, btree (user__id, table_name)

最佳答案

手册 CREATE TABLE会告诉你:

In addition, when the data in the referenced columns is changed, certain actions are performed on the data in this table's columns. The ON DELETE clause specifies the action to perform when a referenced row in the referenced table is being deleted. [...]

是的,在 results 中删除也会在 m_int 中删除。当然,这并不意味着在 m_int 中删除会删除 results 中的任何内容。以及您的额外触发器 add_rows_m_intrem_rows_m_int 做什么或阻止什么 - 我们不知道。

关于function - Postgres ON DELETE CASCADE 会在后续表上触发 ON DELETE 触发器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9931600/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com