创建触发器以将数据从一个表复制到另一个表

时间:2018-07-18 11:30:47

标签: sql postgresql plpgsql database-trigger

我正在尝试创建一个触发器,该触发器将在表1中放一个新条目时复制表1中的数据并将其粘贴到表2中。

OpenFileService

我正在使用的代码是这样:

Table 1  

id | first_name | last_name | email | uid | pwd 
----+------------+-----------+-------+-----+-----


Table 2 

user_id | user_first_name | user_last_name | user_uid 
---------+-----------------+----------------+---------

但是当我将数据插入表1时,我收到此错误消息:

DROP TRIGGER IF EXISTS usersetup_identifier ON users;

CREATE OR REPLACE FUNCTION usersetup_identifier_insert_update() RETURNS trigger AS $BODY$
BEGIN
    if NEW.identifier is null then
        NEW.identifier := "INSERT INTO users_setup (user_id, user_first_name, user_last_name, user_uid)
                          SELECT id, first_name, last_name, uid 
                          FROM users";
    end if;
RETURN NEW;
end
$BODY$
 LANGUAGE plpgsql;

CREATE TRIGGER usersetup_identifier
    AFTER INSERT OR UPDATE ON users FOR EACH ROW
    EXECUTE PROCEDURE usersetup_identifier_insert_update();

表描述为:

NOTICE:  identifier "INSERT INTO users_setup (user_id, user_first_name, user_last_name, user_uid)
                          SELECT id, first_name, last_name, uid 
                          FROM users" will be truncated to "INSERT INTO users_setup (user_id, user_first_name, user_last_na"
ERROR:  record "new" has no field "identifier"
CONTEXT:  SQL statement "SELECT NEW.identifier is null"
PL/pgSQL function usersetup_identifier_insert_update() line 3 at IF

所有列都匹配相应的列 谁能帮我告诉我我要去哪里错了?

                                     Table "public.users"
       Column   |     Type      | Collation | Nullable |              Default              
    ------------+---------------+-----------+----------+-----------------------------------
     id         | integer       |           | not null | nextval('users_id_seq'::regclass)
     first_name | character(20) |           | not null | 
     last_name  | character(20) |           | not null | 
     email      | character(60) |           | not null | 
     uid        | character(20) |           | not null | 
     pwd        | character(20) |           | not null | 
    Indexes:
        "users_pkey" PRIMARY KEY, btree (id)
        "users_email_key" UNIQUE CONSTRAINT, btree (email)
        "users_pwd_key" UNIQUE CONSTRAINT, btree (pwd)
        "users_uid_key" UNIQUE CONSTRAINT, btree (uid)
    Triggers:
        usersetup_identifier AFTER INSERT OR UPDATE ON users FOR EACH ROW EXECUTE PROCEDURE usersetup_identifier_insert_update()

有人可以帮助我解决我的问题吗?

1 个答案:

答案 0 :(得分:1)

您的代码中存在多个错误

  1. users的列名为identifier,因此表达式NEW.identifier无效
  2. 您要使用表达式new.identifier := ...向(不存在的)列分配一个值-但您想运行INSERT语句,而不是分配一个值。
  3. 字符串值需要用单引号引起来,例如'Arthur'中,双引号表示标识符(例如,表或列名)。但是没有名为"INSERT INTO use ..."的列

要访问要插入的行的值,您需要使用new记录和列名。无需从表中选择

据我所知,这就是您想要的:

CREATE OR REPLACE FUNCTION usersetup_identifier_insert_update() 
   RETURNS trigger 
AS 
$BODY$
BEGIN
    INSERT INTO users_setup (user_id, user_first_name, user_last_name, user_uid)
    values (new.id, new.first_name, new.last_name, new.uid);
    RETURN NEW;
end
$BODY$
 LANGUAGE plpgsql;

无关,但是:

像这样复制数据是不好的数据库设计。如果更改用户名会怎样?然后,您还需要UPDATE user_setup表。最好只在引用user_setup表的users表中存储(外键)引用。