SlideShare une entreprise Scribd logo
1  sur  12
Introduction to Triggers Robert Haas PostgreSQL East 2010
What Is a Trigger? ,[object Object]
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.
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;
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)
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 );
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;
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();
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) );
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) );

Contenu connexe

Tendances (20)

Triggers in SQL | Edureka
Triggers in SQL | EdurekaTriggers in SQL | Edureka
Triggers in SQL | Edureka
 
SQL JOIN
SQL JOINSQL JOIN
SQL JOIN
 
Lab2 ddl commands
Lab2 ddl commandsLab2 ddl commands
Lab2 ddl commands
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
 
Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Including Constraints -Oracle Data base
Including Constraints -Oracle Data base
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
 
SQL Server Stored procedures
SQL Server Stored proceduresSQL Server Stored procedures
SQL Server Stored procedures
 
Procedure and Functions in pl/sql
Procedure and Functions in pl/sqlProcedure and Functions in pl/sql
Procedure and Functions in pl/sql
 
Index in sql server
Index in sql serverIndex in sql server
Index in sql server
 
Joins in SQL
Joins in SQLJoins in SQL
Joins in SQL
 
SQL(DDL & DML)
SQL(DDL & DML)SQL(DDL & DML)
SQL(DDL & DML)
 
Packages in PL/SQL
Packages in PL/SQLPackages in PL/SQL
Packages in PL/SQL
 
SQL Joins.pptx
SQL Joins.pptxSQL Joins.pptx
SQL Joins.pptx
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Oracle Database Trigger
Oracle Database TriggerOracle Database Trigger
Oracle Database Trigger
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
View & index in SQL
View & index in SQLView & index in SQL
View & index in SQL
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
SQL Joins With Examples | Edureka
SQL Joins With Examples | EdurekaSQL Joins With Examples | Edureka
SQL Joins With Examples | Edureka
 

En vedette

Presentatie Marshall Goldsmith
Presentatie Marshall GoldsmithPresentatie Marshall Goldsmith
Presentatie Marshall GoldsmithHans Janssen
 
Using triggers in my sql database
Using triggers in my sql databaseUsing triggers in my sql database
Using triggers in my sql databaseKimera Richard
 
Trigger and cursor program using sql
Trigger and cursor program using sqlTrigger and cursor program using sql
Trigger and cursor program using sqlSushil Mishra
 
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...Beat Signer
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, proceduresVaibhav Kathuria
 
Database backup and recovery
Database backup and recoveryDatabase backup and recovery
Database backup and recoveryAnne Lee
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands1keydata
 
Introduction to databases
Introduction to databasesIntroduction to databases
Introduction to databasesakanksha007
 
Veri̇tabani ve Kullanici Yöneti̇mi̇
Veri̇tabani ve Kullanici Yöneti̇mi̇Veri̇tabani ve Kullanici Yöneti̇mi̇
Veri̇tabani ve Kullanici Yöneti̇mi̇Anar Godjaev
 

En vedette (20)

Trigger
TriggerTrigger
Trigger
 
TRIGGERS
TRIGGERSTRIGGERS
TRIGGERS
 
Database Triggers
Database TriggersDatabase Triggers
Database Triggers
 
Triggers ppt
Triggers pptTriggers ppt
Triggers ppt
 
Presentatie Marshall Goldsmith
Presentatie Marshall GoldsmithPresentatie Marshall Goldsmith
Presentatie Marshall Goldsmith
 
Using triggers in my sql database
Using triggers in my sql databaseUsing triggers in my sql database
Using triggers in my sql database
 
Trigger and cursor program using sql
Trigger and cursor program using sqlTrigger and cursor program using sql
Trigger and cursor program using sql
 
Sql ppt
Sql pptSql ppt
Sql ppt
 
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
 
Trigger Data Base
Trigger Data BaseTrigger Data Base
Trigger Data Base
 
View of data DBMS
View of data DBMSView of data DBMS
View of data DBMS
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, procedures
 
Database backup and recovery
Database backup and recoveryDatabase backup and recovery
Database backup and recovery
 
MYSQL.ppt
MYSQL.pptMYSQL.ppt
MYSQL.ppt
 
Backup And Recovery
Backup And RecoveryBackup And Recovery
Backup And Recovery
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
 
Introduction to databases
Introduction to databasesIntroduction to databases
Introduction to databases
 
Introduction to triggers
Introduction to triggersIntroduction to triggers
Introduction to triggers
 
Database Security
Database SecurityDatabase Security
Database Security
 
Veri̇tabani ve Kullanici Yöneti̇mi̇
Veri̇tabani ve Kullanici Yöneti̇mi̇Veri̇tabani ve Kullanici Yöneti̇mi̇
Veri̇tabani ve Kullanici Yöneti̇mi̇
 

Similaire à Introduction to triggers

Function Procedure Trigger Partition.pdf
Function Procedure Trigger Partition.pdfFunction Procedure Trigger Partition.pdf
Function Procedure Trigger Partition.pdfSanam Maharjan
 
Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Thuan Nguyen
 
perl usage at database applications
perl usage at database applicationsperl usage at database applications
perl usage at database applicationsJoe Jiang
 
Mysqlppt
MysqlpptMysqlppt
MysqlpptReka
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 05
Oracle - Program with PL/SQL - Lession 05Oracle - Program with PL/SQL - Lession 05
Oracle - Program with PL/SQL - Lession 05Thuan Nguyen
 
My sql presentation
My sql presentationMy sql presentation
My sql presentationNikhil Jain
 
Getting Started with PL/Proxy
Getting Started with PL/ProxyGetting Started with PL/Proxy
Getting Started with PL/ProxyPeter Eisentraut
 
Mca ii-dbms-u-v-transaction management
Mca ii-dbms-u-v-transaction managementMca ii-dbms-u-v-transaction management
Mca ii-dbms-u-v-transaction managementRai University
 
Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16Thuan Nguyen
 
I am trying to change this code from STRUCTS to CLASSES, the members.pdf
I am trying to change this code from STRUCTS to CLASSES, the members.pdfI am trying to change this code from STRUCTS to CLASSES, the members.pdf
I am trying to change this code from STRUCTS to CLASSES, the members.pdfpetercoiffeur18
 
Oracle trigger
Oracle triggerOracle trigger
Oracle triggernasrul28
 
MYSQL
MYSQLMYSQL
MYSQLARJUN
 
CGI With Object Oriented Perl
CGI With Object Oriented PerlCGI With Object Oriented Perl
CGI With Object Oriented PerlBunty Ray
 
Valeriy Prokopchuk: Validators in Migrations.
Valeriy Prokopchuk: Validators in Migrations.Valeriy Prokopchuk: Validators in Migrations.
Valeriy Prokopchuk: Validators in Migrations.Sphere Consulting Inc
 
Sql Objects And PL/SQL
Sql Objects And PL/SQLSql Objects And PL/SQL
Sql Objects And PL/SQLGary Myers
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricksYanli Liu
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to PerlDave Cross
 

Similaire à Introduction to triggers (20)

Function Procedure Trigger Partition.pdf
Function Procedure Trigger Partition.pdfFunction Procedure Trigger Partition.pdf
Function Procedure Trigger Partition.pdf
 
SQL -PHP Tutorial
SQL -PHP TutorialSQL -PHP Tutorial
SQL -PHP Tutorial
 
Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17
 
perl usage at database applications
perl usage at database applicationsperl usage at database applications
perl usage at database applications
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09
 
Oracle - Program with PL/SQL - Lession 05
Oracle - Program with PL/SQL - Lession 05Oracle - Program with PL/SQL - Lession 05
Oracle - Program with PL/SQL - Lession 05
 
My sql presentation
My sql presentationMy sql presentation
My sql presentation
 
Getting Started with PL/Proxy
Getting Started with PL/ProxyGetting Started with PL/Proxy
Getting Started with PL/Proxy
 
Mca ii-dbms-u-v-transaction management
Mca ii-dbms-u-v-transaction managementMca ii-dbms-u-v-transaction management
Mca ii-dbms-u-v-transaction management
 
Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16
 
I am trying to change this code from STRUCTS to CLASSES, the members.pdf
I am trying to change this code from STRUCTS to CLASSES, the members.pdfI am trying to change this code from STRUCTS to CLASSES, the members.pdf
I am trying to change this code from STRUCTS to CLASSES, the members.pdf
 
DOODB_LAB.pptx
DOODB_LAB.pptxDOODB_LAB.pptx
DOODB_LAB.pptx
 
Oracle trigger
Oracle triggerOracle trigger
Oracle trigger
 
MYSQL
MYSQLMYSQL
MYSQL
 
CGI With Object Oriented Perl
CGI With Object Oriented PerlCGI With Object Oriented Perl
CGI With Object Oriented Perl
 
Valeriy Prokopchuk: Validators in Migrations.
Valeriy Prokopchuk: Validators in Migrations.Valeriy Prokopchuk: Validators in Migrations.
Valeriy Prokopchuk: Validators in Migrations.
 
Sql Objects And PL/SQL
Sql Objects And PL/SQLSql Objects And PL/SQL
Sql Objects And PL/SQL
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricks
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 

Plus de Command Prompt., Inc

Howdah - An Application using Pylons, PostgreSQL, Simpycity and Exceptable
Howdah - An Application using Pylons, PostgreSQL, Simpycity and ExceptableHowdah - An Application using Pylons, PostgreSQL, Simpycity and Exceptable
Howdah - An Application using Pylons, PostgreSQL, Simpycity and ExceptableCommand Prompt., Inc
 
Mastering PostgreSQL Administration
Mastering PostgreSQL AdministrationMastering PostgreSQL Administration
Mastering PostgreSQL AdministrationCommand Prompt., Inc
 
Replication using PostgreSQL Replicator
Replication using PostgreSQL ReplicatorReplication using PostgreSQL Replicator
Replication using PostgreSQL ReplicatorCommand Prompt., Inc
 
Python utilities for data presentation
Python utilities for data presentationPython utilities for data presentation
Python utilities for data presentationCommand Prompt., Inc
 
PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...
PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...
PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...Command Prompt., Inc
 
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQLpg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQLCommand Prompt., Inc
 
Not Just UNIQUE: Generalized Index Constraints
Not Just UNIQUE: Generalized Index ConstraintsNot Just UNIQUE: Generalized Index Constraints
Not Just UNIQUE: Generalized Index ConstraintsCommand Prompt., Inc
 
Implementing the Future of PostgreSQL Clustering with Tungsten
Implementing the Future of PostgreSQL Clustering with TungstenImplementing the Future of PostgreSQL Clustering with Tungsten
Implementing the Future of PostgreSQL Clustering with TungstenCommand Prompt., Inc
 
Elephant Roads: a tour of Postgres forks
Elephant Roads: a tour of Postgres forksElephant Roads: a tour of Postgres forks
Elephant Roads: a tour of Postgres forksCommand Prompt., Inc
 
configuring a warm standby, the easy way
configuring a warm standby, the easy wayconfiguring a warm standby, the easy way
configuring a warm standby, the easy wayCommand Prompt., Inc
 
Normalization: A Workshop for Everybody Pt. 2
Normalization: A Workshop for Everybody Pt. 2Normalization: A Workshop for Everybody Pt. 2
Normalization: A Workshop for Everybody Pt. 2Command Prompt., Inc
 
Normalization: A Workshop for Everybody Pt. 1
Normalization: A Workshop for Everybody Pt. 1Normalization: A Workshop for Everybody Pt. 1
Normalization: A Workshop for Everybody Pt. 1Command Prompt., Inc
 

Plus de Command Prompt., Inc (20)

Howdah - An Application using Pylons, PostgreSQL, Simpycity and Exceptable
Howdah - An Application using Pylons, PostgreSQL, Simpycity and ExceptableHowdah - An Application using Pylons, PostgreSQL, Simpycity and Exceptable
Howdah - An Application using Pylons, PostgreSQL, Simpycity and Exceptable
 
Backup and-recovery2
Backup and-recovery2Backup and-recovery2
Backup and-recovery2
 
Mastering PostgreSQL Administration
Mastering PostgreSQL AdministrationMastering PostgreSQL Administration
Mastering PostgreSQL Administration
 
Temporal Data
Temporal DataTemporal Data
Temporal Data
 
Replication using PostgreSQL Replicator
Replication using PostgreSQL ReplicatorReplication using PostgreSQL Replicator
Replication using PostgreSQL Replicator
 
Go replicator
Go replicatorGo replicator
Go replicator
 
Pg migrator
Pg migratorPg migrator
Pg migrator
 
Python utilities for data presentation
Python utilities for data presentationPython utilities for data presentation
Python utilities for data presentation
 
PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...
PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...
PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...
 
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQLpg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQL
 
Not Just UNIQUE: Generalized Index Constraints
Not Just UNIQUE: Generalized Index ConstraintsNot Just UNIQUE: Generalized Index Constraints
Not Just UNIQUE: Generalized Index Constraints
 
Implementing the Future of PostgreSQL Clustering with Tungsten
Implementing the Future of PostgreSQL Clustering with TungstenImplementing the Future of PostgreSQL Clustering with Tungsten
Implementing the Future of PostgreSQL Clustering with Tungsten
 
Elephant Roads: a tour of Postgres forks
Elephant Roads: a tour of Postgres forksElephant Roads: a tour of Postgres forks
Elephant Roads: a tour of Postgres forks
 
configuring a warm standby, the easy way
configuring a warm standby, the easy wayconfiguring a warm standby, the easy way
configuring a warm standby, the easy way
 
Bucardo
BucardoBucardo
Bucardo
 
Basic Query Tuning Primer
Basic Query Tuning PrimerBasic Query Tuning Primer
Basic Query Tuning Primer
 
A Practical Multi-Tenant Cluster
A Practical Multi-Tenant ClusterA Practical Multi-Tenant Cluster
A Practical Multi-Tenant Cluster
 
5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance
 
Normalization: A Workshop for Everybody Pt. 2
Normalization: A Workshop for Everybody Pt. 2Normalization: A Workshop for Everybody Pt. 2
Normalization: A Workshop for Everybody Pt. 2
 
Normalization: A Workshop for Everybody Pt. 1
Normalization: A Workshop for Everybody Pt. 1Normalization: A Workshop for Everybody Pt. 1
Normalization: A Workshop for Everybody Pt. 1
 

Introduction to triggers

  • 1. Introduction to Triggers Robert Haas PostgreSQL East 2010
  • 2.
  • 3. Can fire on INSERT or UPDATE or DELETE or any combination.
  • 4. Can fire once per statement or for every row.
  • 5. Can fire BEFORE or AFTER the main operation.
  • 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. 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. 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. 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. 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. 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. 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. 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. 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.