简体   繁体   中英

Trigger - postgres

I have created the following tables

professor
------------------------------------------------------
ProfNr | Name | Room | Bonus
------------------------------------------------------

lecture
--------------------------------------------------
Lecture No | Title | semester hours | held by (Prof Reference)
--------------------------------------------------

I am looking for a trigger under the postgres using the weekly lesson the professor gives a bonus (salary)

Maybe I can help someone would, really great!

Or even an idea for a possible trigger other application

First, I think your bonuses should be another table so you can aggregate on demand. So this leaves three tables:

CREATE TABLE professor (
   profid serial not null unique,
   ...
);

CREATE TABLE lecture (
   lecture_id serial not null unique,
   professor int not null references professor(profid),
   ...
);

CREATE TABLE prof_bonus (
   lecture_id int references lecture(id),
   profid int references professor (profid),
   bonus_amt numeric not null,
   primary key (lecture_id, profid)
);

CREATE FUNCTION add_bonus() RETURNS TRIGGER LANGUAGE PLPGSQL AS
$$
BEGIN
INSERT INTO prof_bonus (lecture_id, profid, bonus_amt)
VALUES (new.lecture_id, new.profid, 100);
RETURN NEW;
END;
$$;

CREATE TRIGGER add_bonus AFTER INSERT TO lecture FOR EACH ROW 
EXECUTE PROCEDURE add_bonus();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM