问题描述:
大家好,我有这张桌子:
CREATE TABLE COMPORDINE (
CodProdotto CHAR(5) NOT NULL CHECK(CodProdotto ~* '^[0-9]+$'),
CodOrdine CHAR(5) CHECK(CodOrdine ~* '^[0-9]+$'),
Prezzo REAL NOT NULL, CHECK(Prezzo >= 0.0)
CONSTRAINT OrdineFK FOREIGN KEY(CodOrdine) REFERENCES ORDINE(CodOrdine)
CONSTRAINT ProdottoFK FOREIGN KEY(CodProdotto) REFERENCES PRODOTTO(CodProdotto)
);
我在上面声明了一个TRIGGER
:
CREATE TRIGGER updatePrezzoOrdine
AFTER INSERT ON COMPORDINE
FOR EACH ROW
EXECUTE PROCEDURE updatePrezzoOrdine();
和一个TRIGGER FUNCTION
:
CREATE OR REPLACE FUNCTION updatePrezzoOrdine()
RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
DECLARE
Totale REAL;
BEGIN
SELECT SUM(Prezzo) INTO Totale
FROM COMPORDINE AS CO
WHERE CO.CodOrdine = NEW.CodOrdine
GROUP BY CodOrdine;
UPDATE ORDINE SET PrezzoTotale = PrezzoTotale + Totale
WHERE CodOrdine = NEW.CodOrdine;
RETURN NEW;
END
$$
一切正常,触发器正常触发,但没有UPDATE
在桌子上完成ORDINE
(第 12 行)。
我的经历(并尝试过):
我尝试放置一些打印说明以查看查询是否有效:
RAISE NOTICE 'NEW.CodOrdine = %', NEW.CodOrdine;
RAISE NOTICE 'NEW.CodProdotto = %', NEW.CodProdotto;
RAISE NOTICE 'NEW.Prezzo = %', NEW.Prezzo;
但输出返回插入的新行:
INSERT INTO COMPORDINE VALUES ('12345', '11111', 1.80)
有空值:
NOTICE: NEW.CodOrdine = <NULL>
NOTICE: NEW.CodProdotto = <NULL>
NOTICE: NEW.Prezzo = <NULL>
所以UPDATE
“失败”是因为没有ORDINE
匹配NULL
值的行:
鳕鱼 | PrezzoTotale | 数据采集器 | 代码客户端 | 依赖鳕鱼 |
---|---|---|---|---|
12345 | 默认 (0.0) | 2020-12-12 | 00000 | 99999 |
显然问题可能与插入的行有关,可能吗?
我的期望:
在INSERT
我用这种方式匹配ORDINE.PrezzoTotale
的每个项目的正确总价更新列之后:COMPORDINE
COMPORDINE.CodOrdine = ORDINE.CodOrdine
INSERT INTO COMPORDINE VALUES ('12345', '11111', 1.80)
鳕鱼 | PrezzoTotale | 数据采集器 | 代码客户端 | 依赖鳕鱼 |
---|---|---|---|---|
12345 | 1.80 | 2020-12-12 | 00000 | 99999 |
非常感谢任何人提前
[编辑#1]:
如所问,这是输出\d compordine
:
**"public.compordine" table:**
| Colonna | Tipo | Ordinamento | Pu‗ essere null | Default|
|------------|--------------|-------------|-----------------|--------|
|codprodotto | character(5) | | not null | |
|codordine | character(5) | | not null | |
|prezzo | real | | not null | |
**Indexes**:
"compordine_codprodotto_key" UNIQUE CONSTRAINT, btree (codprodotto)
**Check constraints**:
"compordine_codordine_check" CHECK (codordine ~* '^[0-9]+$'::text)
"compordine_codprodotto_check" CHECK (codprodotto ~* '^[0-9]+$'::text)
"compordine_prezzo_check" CHECK (prezzo > 0::double precision)
**Referential integrity constraints**:
"ordinefk" FOREIGN KEY (codordine) REFERENCES ordine(codordine) ON UPDATE CASCADE
"prodottofk" FOREIGN KEY (codprodotto) REFERENCES prodotto(codprodotto) ON UPDATE CASCADE
**Triggers**:
updateprezzoordine AFTER INSERT ON compordine FOR EACH STATEMENT EXECUTE FUNCTION updateprezzoordine()
**Disabled triggers**:
updatescorte BEFORE INSERT ON compordine FOR EACH ROW EXECUTE FUNCTION updatescorte()
[编辑#2]:
所以,我做了其他几次尝试来寻找解决方案(剧透警报:未找到解决方案);我所做的是创建另一个函数和触发器,它只是在另一个名为的表上NEW.<column>
的语句之后打印值(我怀疑所有这些问题都只与表有关,但我错了),这是代码:INSERT
CLIENTE
COMPORDINE
CREATE TABLE CLIENTE (
CodCliente CHAR(5) PRIMARY KEY, CHECK(CodCliente ~* '^[0-9]+$'),
Nome VARCHAR(255) NOT NULL, CHECK(Nome ~* '^[A-Za-z ]+$'),
Cognome VARCHAR(255) NOT NULL, CHECK(Cognome ~* '^[A-Za-z ]+$'),
CodiceFiscale CHAR(16) NOT NULL, CHECK(CodiceFiscale ~* '^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$'),
Indirizzo VARCHAR(300) NOT NULL,
Email VARCHAR(255) NOT NULL, CHECK(Email ~* '^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+[.][A-za-z]+$'),
UNIQUE(CodiceFiscale, Email)
);
CREATE TRIGGER print
BEFORE INSERT ON Cliente
EXECUTE PROCEDURE print();
CREATE FUNCTION print()
RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
BEGIN
RAISE NOTICE 'CodCliente = %', NEW.CodCliente;
RETURN NEW;
END
$$
INSERT INTO CLIENTE VALUES ('34567', 'Gianfranco', 'Rana', 'GNFRNA87F24E123A', 'Via Delle Rane 23', 'franco.rane@ravioli.it')
输出返回:
NEW.CodCliente = <NULL>
虽然它应该是:
NEW.CodCliente = <34567>
我根本不明白为什么,不应该NEW
以AFTER INSERT TRIGGER
这种方式返回值?:
事件 | OLD |
NEW |
---|---|---|
INSERT |
NULL |
新纪录 |
DELETE |
旧唱片 | NULL |
UPDATE |
原始记录 | 更新记录 |