Introduction to Triggers Robert Haas PostgreSQL East 2010
What Is a Trigger? <ul><li>Way to execute a database function automatically.
Can fire on INSERT or UPDATE or DELETE or any combination.
Can fire once per statement or for every row.
Can fire BEFORE or AFTER the main operation. </li></ul>
Example #1: Compute full name CREATE TABLE person ( id serial PRIMARY KEY, first_name varchar not null, middle_name varcha...
Example #1: Compute full name CREATE TRIGGER compute_person_full_name BEFORE INSERT OR UPDATE ON person FOR EACH ROW EXECU...
Example #2: Counts and sums CREATE TABLE orders ( id serial primary key, customer_name varchar not null, number_of_items i...
Example #2: Counts and sums CREATE OR REPLACE FUNCTION update_order_stats() RETURNS trigger AS $$ BEGIN IF (TG_OP != 'DELE...
Example #2: Counts and sums CREATE TRIGGER update_order_stats AFTER INSERT OR UPDATE OR DELETE ON order_items FOR EACH ROW...
Example #3: Denormalization CREATE TABLE person ( id serial primary key, full_name varchar not null, is_project_manager bo...
Example #3: Denormalization CREATE TABLE person ( id serial primary key, full_name varchar not null, is_project_manager bo...
Upcoming SlideShare
Loading in …5
×

Introduction to triggers

1,469
-1

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,469
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
49
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Introduction to triggers

  1. 1. Introduction to Triggers Robert Haas PostgreSQL East 2010
  2. 2. What Is a Trigger? <ul><li>Way to execute a database function automatically.
  3. 3. Can fire on INSERT or UPDATE or DELETE or any combination.
  4. 4. Can fire once per statement or for every row.
  5. 5. Can fire BEFORE or AFTER the main operation. </li></ul>
  6. 6. Example #1: Compute full name CREATE TABLE person ( id serial PRIMARY KEY, first_name varchar not null, middle_name varchar not null, last_name varchar not null, full_name varchar not null ); CREATE OR REPLACE FUNCTION compute_person_full_name() RETURNS trigger AS $$ BEGIN NEW.full_name := NEW.first_name || CASE WHEN NEW.middle_name = '' THEN '' ELSE ' ' || NEW.middle_name END || ' ' || NEW.last_name; RETURN NEW; END $$ LANGUAGE plpgsql;
  7. 7. Example #1: Compute full name CREATE TRIGGER compute_person_full_name BEFORE INSERT OR UPDATE ON person FOR EACH ROW EXECUTE PROCEDURE compute_person_full_name(); INSERT INTO person (first_name, middle_name, last_name) VALUES ('Robert', 'M.', 'Haas'), ('Tom', '', 'Lane'); rhaas=# select full_name from person; full_name ---------------- Robert M. Haas Tom Lane (2 rows)
  8. 8. Example #2: Counts and sums CREATE TABLE orders ( id serial primary key, customer_name varchar not null, number_of_items integer not null default 0, total_price numeric(12,2) not null default 0 ); CREATE TABLE order_items ( order_id integer not null references orders (id), item_name varchar not null, price numeric(12,2) not null default 0 );
  9. 9. Example #2: Counts and sums CREATE OR REPLACE FUNCTION update_order_stats() RETURNS trigger AS $$ BEGIN IF (TG_OP != 'DELETE') THEN UPDATE orders SET number_of_items = number_of_items + 1, total_price = total_price + NEW.price WHERE id = NEW.order_id; END IF; IF (TG_OP != 'INSERT') THEN UPDATE orders SET number_of_items = number_of_items - 1, total_price = total_price - OLD.price WHERE id = OLD.order_id; END IF; RETURN NULL; END $$ LANGUAGE plpgsql;
  10. 10. Example #2: Counts and sums CREATE TRIGGER update_order_stats AFTER INSERT OR UPDATE OR DELETE ON order_items FOR EACH ROW EXECUTE PROCEDURE update_order_stats();
  11. 11. Example #3: Denormalization CREATE TABLE person ( id serial primary key, full_name varchar not null, is_project_manager boolean not null ); CREATE TABLE project ( id serial primary key, name varchar not null, project_manager_id integer not null references person (id) );
  12. 12. Example #3: Denormalization CREATE TABLE person ( id serial primary key, full_name varchar not null, is_project_manager boolean not null ); CREATE TABLE project ( id serial primary key, name varchar not null, project_manager_id integer not null references person (id), is_project_manager boolean not null, CONSTRAINT pm_is_legal CHECK (is_project_manager) );
  13. 13. Example #3: Denormalization CREATE OR REPLACE FUNCTION project_insert_or_update() RETURNS trigger AS $$ BEGIN SELECT pp.is_project_manager FROM person pp WHERE pp.id = NEW.project_manager_id FOR UPDATE INTO NEW.is_project_manager; RETURN NEW; END $$ LANGUAGE plpgsql; CREATE TRIGGER project_insert_or_update BEFORE INSERT OR UPDATE ON project FOR EACH ROW EXECUTE PROCEDURE project_insert_or_update();
  14. 14. Example #3: Denormalization CREATE OR REPLACE FUNCTION person_update() RETURNS trigger AS $$ BEGIN IF (OLD.is_project_manager IS DISTINCT FROM NEW.is_project_manager) THEN UPDATE project SET is_project_manager = NEW.is_project_manager WHERE project_manager_id = NEW.id; END IF; RETURN NULL; END $$ LANGUAGE plpgsql; CREATE TRIGGER person_update AFTER UPDATE ON person FOR EACH ROW EXECUTE PROCEDURE person_update();
  15. 15. Questions? <ul>Any questions? </ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×