相关疑难解决方法(0)

对于每个语句触发器示例

我一直在查看postgresql触发器文档,但它似乎只显示行级触发器的示例,但我找不到语句级触发器的示例.

特别是,如何在单个语句中迭代更新/插入行并不是很清楚,因为它NEW是针对单个记录的.

postgresql triggers plpgsql

16
推荐指数
2
解决办法
1万
查看次数

插入整数列时不要静默舍入浮点输入

我有一张像:

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 postgresql-12

6
推荐指数
1
解决办法
70
查看次数

如何编写返回文本或整数值的函数?

我正在使用 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)

sqlfiddle 链接

我的查询安全地执行插入。我需要的是我的函数应该返回除 void 数据类型之外的内容。像 text("inserted into table") 或 integer(0-false,1-true) 之类的东西,验证是否插入对我有用?

我需要一个在插入完成时返回 aninteger或 a的函数的语法text。用于验证目的。有没有办法解决这个问题?

postgresql types plpgsql postgresql-9.2

5
推荐指数
1
解决办法
2万
查看次数

如何找到通过外键引用特定行的表?

给定这样的结构:

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)

sql postgresql foreign-keys

4
推荐指数
1
解决办法
2561
查看次数