我一直在查看postgresql触发器的文档,但它似乎只显示行级触发器的示例,但我找不到语句级触发器的示例.
特别是,如何在单个语句中迭代更新/插入行并不是很清楚,因为它NEW
是针对单个记录的.
我有一张像:
CREATE TABLE foo(bar int)
Run Code Online (Sandbox Code Playgroud)
我有一个将值插入该表的脚本:
INSERT INTO foo(bar)
VALUES (1), (2), (3.2)
Run Code Online (Sandbox Code Playgroud)
浮点值被静默四舍五入以适合数据类型:
> SELECT * FROM foo;
bar
-----
1
2
3
(3 rows)
Run Code Online (Sandbox Code Playgroud)
Postgres 是否有任何内置功能可以防止这种情况发生,而是引发错误?(甚至是警告?)
我正在使用 PostgreSQL 9.2.4。
postgres=# 选择版本();
version
-------------------------------------------------------------
PostgreSQL 9.2.4, compiled by Visual C++ build 1600, 64-bit
(1 row)
Run Code Online (Sandbox Code Playgroud)
我的查询安全地执行插入。我需要的是我的函数应该返回除 void 数据类型之外的内容。像 text("inserted into table") 或 integer(0-false,1-true) 之类的东西,验证是否插入对我有用?
我需要一个在插入完成时返回 aninteger
或 a的函数的语法text
。用于验证目的。有没有办法解决这个问题?
给定这样的结构:
CREATE TABLE reference_table (
reference_table_key numeric NOT NULL,
reference_value numeric,
CONSTRAINT reference_table_pk PRIMARY KEY (reference_table_key)
);
CREATE TABLE other_table (
other_table_key numeric NOT NULL,
reference_table_key numeric,
CONSTRAINT other_table_pk PRIMARY KEY (other_table_key),
ONSTRAINT other_table_reference_fk FOREIGN KEY (reference_table_key)
REFERENCES reference_table (reference_table_key) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE SET NULL
);
CREATE TABLE another_table (
another_table_key numeric NOT NULL,
do_stuff_key numeric,
CONSTRAINT another_table_pk PRIMARY KEY (another_table_key),
ONSTRAINT another_table_reference_fk FOREIGN KEY (do_stuff_key)
REFERENCES reference_table (reference_table_key) MATCH SIMPLE
ON UPDATE NO ACTION …
Run Code Online (Sandbox Code Playgroud)