sql >> Database >  >> RDS >> PostgreSQL

Postgresql-trigger invoegen om samen te voegen

Hier is een werkende oplossing:

CREATE TEMPORARY TABLE t_employees (
    first_name TEXT,
    mid_name TEXT,
    last_name TEXT,
    fullname TEXT
);

CREATE OR REPLACE FUNCTION set_fullname()
    RETURNS TRIGGER AS $$
    BEGIN
        NEW.fullname = NEW.first_name || ' ' || NEW.mid_name || ' ' || NEW.last_name;
        RETURN NEW;
    END;
    $$ language 'plpgsql';


CREATE TRIGGER set_fullname_trigger
    BEFORE INSERT OR UPDATE ON t_employees
    FOR EACH ROW
    EXECUTE PROCEDURE set_fullname();


SET client_min_messages TO 'debug';
INSERT INTO t_employees VALUES ('fname1', 'mname1', 'lname1');
SELECT * FROM t_employees;
UPDATE t_employees SET first_name = 'updated-first-name';
SELECT * FROM t_employees;


  1. MySQL ALTER TABLE op tabellen die te groot zijn om te dupliceren

  2. Database online maken vanuit de herstelmodus in SQL Server

  3. PHP-MySQL-Hoe kan ik het MySQL integer-veld veilig verhogen?

  4. glasvis 4 &MySQL &JSTL