gpt4 book ai didi

sql - 触发创建父元素并在 postgresql 中检索 id

转载 作者:行者123 更新时间:2023-11-29 13:41:12 25 4
gpt4 key购买 nike

我创建了以下表格:

CREATE TABLE IF NOT EXISTS public.teams (
id SERIAL PRIMARY KEY,
name VARCHAR(64) NOT NULL UNIQUE
) WITH (OIDS = FALSE);

CREATE TABLE IF NOT EXISTS public.submissions (
id SERIAL PRIMARY KEY,
team_id INTEGER REFERENCES public.teams NOT NULL,
records_num INTEGER NOT NULL,
timestamp TIMESTAMP NOT NULL
) WITH (OIDS = FALSE);

CREATE TABLE IF NOT EXISTS public.predictions (
id SERIAL PRIMARY KEY,
submission_id INTEGER REFERENCES public.submissions NOT NULL,
customer INTEGER REFERENCES public.real NOT NULL,
date DATE NOT NULL,
billing NUMERIC(20, 2) NOT NULL
) WITH (OIDS = FALSE);

CREATE TABLE IF NOT EXISTS public.real (
customer INTEGER PRIMARY KEY,
date DATE NOT NULL,
billing NUMERIC(20, 2) NOT NULL
) WITH (OIDS = FALSE);

提交-预测的关系是一对多的;用户将以 1000 行的数据包的形式提交预测,这些预测应获得相同的提交 ID。

我正在尝试创建一个触发器,该触发器在创建提交行的预测上运行之前插入。这是我目前所拥有的:

CREATE OR REPLACE FUNCTION insert_submission() RETURNS TRIGGER AS
$$
BEGIN
INSERT INTO submissions(team_id, records_num, timestamp)
VALUES (1, 1, '2018-04-21 00:00:00'); /*example values, need to fill with dynamically assigned ones, specially for records_num and team_id*/
RETURN NULL;
END
$$ LANGUAGE plpgsql;
DROP TRIGGER trigger_submission ON public.predictions;
CREATE TRIGGER trigger_submission BEFORE INSERT ON predictions
EXECUTE PROCEDURE insert_submission();

所以,我的问题是:

  1. 如何为触发器插入的行检索新创建的 submissions.id,以便将其添加到用户在预测中插入的所有行?为此,我是否必须在插入后运行另一个触发器?

编辑:为了澄清@bignose 的回答,事件的顺序是这样的:

用户向 public.predictions 中插入 1000 行:

INSERT INTO predictions(customer, date, billing)
VALUES
(1, '2018-01-05', 543.42),
(4, '2018-04-02', 553.21),
...
(423, '2019-11-18', 38.87) /* 1000th row */

他不知道要在这些行中插入哪个 submission_id,事实上,这个预测数据包的提交行尚不存在,因此在提交之前运行触发器以在提交中创建一行,该行将执行如下操作:

INSERT INTO public.submisssions(team_id, records_num, timestamp)
VALUES (
4, /* I will need something to retrieve team_id here */
1000, /* I will need something to count the rows of the insert that triggered this */
NOW() /* convert to timestamp */
)

最后一个查询应该将它刚刚创建的 public.submission.id 值返回给用户请求的插入,这样它最终就像这样:

INSERT INTO predictions(customer, date, billing)
VALUES
(@submission_id, 1, '2018-01-05', 543.42),
(@submission_id, 4, '2018-04-02', 553.21),
...
(@submission_id, 423, '2019-11-18', 38.87) /* 1000th row */

@submission_id 应该是从触发器中检索到的值(以及所有 1000 行的一些值)

  1. 我如何计算用户插入的行以将它们用作 submissions.records_num 的值?

  2. 假设我事先知道 team.name,我如何检索 team.id 以在触发器执行期间插入?

谢谢!亲切的问候

最佳答案

触发器函数,当用于行级触发器时,has access to the old and new state of the table .

CREATE OR REPLACE FUNCTION insert_submission() RETURNS TRIGGER AS
$$
BEGIN
INSERT INTO submissions(team_id, records_num, timestamp)
VALUES (NEW.foo, NEW.bar, '2018-04-21 00:00:00');
RETURN NULL;
END
$$ LANGUAGE plpgsql;

从描述中不清楚,您希望从触发此函数的行中检索哪些字段。因此,您需要将 NEW.fooNEW.bar 替换为 NEW 行状态中的字段引用。

关于sql - 触发创建父元素并在 postgresql 中检索 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55111821/

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