the*_*aps 2 postgresql triggers stored-procedures function
如何创建一个trigger function之前adding/updating,function应检查具有相同的记录id(即与具有相同属性的id现有进行比较)如果找到具有id的记录,则将该条目设置为,然后添加包含在该记录中找到的属性的相应值的条目(除了为新记录设置的属性除外),当为空时,则新时间等于当前时刻的那个时间.因此,新记录就像祖先一样.如果找到具有该id的记录,则将其添加到具有当前时间的企业的数据库中.objectstemporary_objecttime_deadtime_deadtime_createtime_createtime_dead'stime_create
例如这里是一个简单的解释(仅用于解释目的)
id time_create time-dead student amount
1 06.12 07.12 henry 500
1 07.12 henry 1000
Run Code Online (Sandbox Code Playgroud)
因此,如果一名叫id 1的亨利在06.12进入一个房间并在07.12离开他下次再次进入另一个房间时time_dead将等于time_create(因此旧条目的time_dead和新条目的time_create将相等)这些是我的表格以sql格式显示
CREATE TABLE temporary_object
(
id integer NOT NULL,
time_create timestamp without time zone NOT NULL,
time_dead timestamp without time zone,
CONSTRAINT temporary_object_pkey PRIMARY KEY (id, time_create)
)
CREATE TABLE persons
(
fname text,
fsurname text,
)
INHERITS (temporary_object)
CREATE TABLE rooms
(
roomnum integer,
course integer,
passport text,
students_number text
)
INHERITS (temporary_object)
Run Code Online (Sandbox Code Playgroud)
这是我想要做的但我害怕我不知道如何完成它但我100%不对可能有些帮助
CREATE TRIGGER trigger2
BEFORE INSERT OR UPDATE
ON persons
FOR EACH ROW
EXECUTE PROCEDURE func1();
Run Code Online (Sandbox Code Playgroud)
这就是功能
CREATE OR REPLACE FUNCTION func1() RETURNS TRIGGER AS $persons$
DECLARE
time_create integer;
time_dead timestamp;
id timestamp;
BEGIN
IF (TG_OP = 'INSERT') THEN
time_create=
Run Code Online (Sandbox Code Playgroud)
我无法告诉你我在你的问题中遗漏了什么,但我试着回答我认为我理解的内容.
行级触发器可以访问受NEW和OLD变量影响的行的版本(取决于TG_OP).在这种情况下,您可以使用NEW:
CREATE OR REPLACE FUNCTION func1()
RETURNS TRIGGER AS
$persons$
DECLARE
i integer;
BEGIN
IF TG_OP = 'INSERT'
THEN
UPDATE persons
SET time_dead = NEW.time_create
WHERE
id = NEW.id -- we are looking for the same ID
AND time_dead IS NULL
;
ELSE -- UPDATE
-- do here something
END IF;
END;
$persons$
LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
这只是一个启动器,根据您的需要进行修改.
| 归档时间: |
|
| 查看次数: |
3894 次 |
| 最近记录: |