agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedFrom: Chris Ryan <chris@greatbridge.com>
To: craig.may@s2.enthdimension.com.au
To: pgsql-sql@postgresql.org
Subject: Re: Trigger
Date: Wed, 06 Sep 2000 08:40:20 -0400
Message-ID: <39B63B34.116C525F@greatbridge.com> (raw)
References: <20000906202707.A4767@s2.enthdimension.com.au>
<39B6360A.8FEE705@greatbridge.com>
Chris Ryan wrote:
>
> Craig May wrote:
> >
> > Could someone send me a quick example of a trigger.
>
> Hope this helps.
>
> Chris Ryan
>
-- snipped code --
I am so sorry but you may have noticed my email client wrapped lines it
shouldn't have. I have attached the file this time.
Chris Ryan
--
-- FILE: trigger_example.sql
--
-- DESCRIPTION:
-- This file shows the basics of creating a table with a trigger
--
-- Chris Ryan <chris@greatbridge.com> 09/06/2000
--
-- GENERAL DISCLAIMER:
-- Please feel free to use this in any way you see fit to copy, modify,
-- redistribute, etc.. I provide not warranty of the code nor may I be held
-- responsible for it's use/misuse should something bad happen including
-- intentional or acts of god.
--
--
-- FUNCTION: trigger_last_updated
--
-- DESCRIPTION:
-- This is a function called by the table triggers to update the last_updated
-- field on insert and updates.
--
create function trigger_last_updated()
returns opaque
as 'begin
new.last_updated := now();
return new;
end;'
language 'plpgsql';
--
-- TABLE: test_tbl
--
-- DESCRIPTION:
-- A simple table to test my trigger
--
create table test_tbl (
some_field varchar(10),
last_updated timestamp not null default now()
);
--
-- TRIGGER: trigger_insert_update_test_tbl
--
-- DESCRIPTION:
-- This is the trigger called on insert and updates of all the table that
-- has the last_updated field. It will use the function trigger_last_updated
-- The cool thing here is the function doesn't make specific reference to the
-- table so you could create a different trigger for each table with the field
-- last_updated and use the same function.
--
create trigger trigger_insert_update_test_tbl
before insert or update on test_tbl
for each row execute procedure trigger_last_updated();
Attachments:
[text/plain] trigger_example.sql (1.5K, ../39B63B34.116C525F@greatbridge.com/2-trigger_example.sql)
download | inline:
--
-- FILE: trigger_example.sql
--
-- DESCRIPTION:
-- This file shows the basics of creating a table with a trigger
--
-- Chris Ryan <chris@greatbridge.com> 09/06/2000
--
-- GENERAL DISCLAIMER:
-- Please feel free to use this in any way you see fit to copy, modify,
-- redistribute, etc.. I provide not warranty of the code nor may I be held
-- responsible for it's use/misuse should something bad happen including
-- intentional or acts of god.
--
--
-- FUNCTION: trigger_last_updated
--
-- DESCRIPTION:
-- This is a function called by the table triggers to update the last_updated
-- field on insert and updates.
--
create function trigger_last_updated()
returns opaque
as 'begin
new.last_updated := now();
return new;
end;'
language 'plpgsql';
--
-- TABLE: test_tbl
--
-- DESCRIPTION:
-- A simple table to test my trigger
--
create table test_tbl (
some_field varchar(10),
last_updated timestamp not null default now()
);
--
-- TRIGGER: trigger_insert_update_test_tbl
--
-- DESCRIPTION:
-- This is the trigger called on insert and updates of all the table that
-- has the last_updated field. It will use the function trigger_last_updated
-- The cool thing here is the function doesn't make specific reference to the
-- table so you could create a different trigger for each table with the field
-- last_updated and use the same function.
--
create trigger trigger_insert_update_test_tbl
before insert or update on test_tbl
for each row execute procedure trigger_last_updated();
view thread (14+ messages) latest in thread
Message-ID: <39B63B34.116C525F@greatbridge.com>
Permalink: ../39B63B34.116C525F@greatbridge.com/
Also on: postgresql.org/message-id/39B63B34.116C525F@greatbridge.com
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: chris@greatbridge.com, craig.may@s2.enthdimension.com.au
Subject: Re: Trigger
In-Reply-To: <39B63B34.116C525F@greatbridge.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox