pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Oliver Elphick <olly@lfix.co.uk>
To: Pausas Fuentes, Jaume <JPausas@alehop.com>
Cc: pgsql-sql@postgreSQL.org
Cc: olly@linda.lfix.co.uk
Subject: Re: [SQL] foreign key implementation
Date: Thu, 13 Jan 2000 21:21:16 +0000
Message-ID: <200001132121.VAA23887@linda.lfix.co.uk> (raw)
In-Reply-To: <JPausas@alehop.com> <84f54420.442084f5@alehop.com>
References: <84f54420.442084f5@alehop.com>

"Pausas Fuentes, Jaume" wrote:
  >Hi
  >
  >Anybody have an implementation of the sql foreign key?
  >
  >Reading the manual it says that I must use the create trigger command 
  >but how to implement a procedure to do what foreing key does?

You need the refint package out of contrib.

Then use commands like these:

create database bray;
\connect bray

-- refint functions (from postgresql-contrib package).  These are
-- needed until foreign keys are implemented in PostgreSQL
\i contrib/spi/refint.sql

create table area 
(
        id              char(2)         primary key,
        name            text            not null,
        vat_class       char(1)         default 'S'
);

create trigger area_fref
        before DELETE or UPDATE on area
        for each row execute procedure
        check_foreign_key (1, 'restrict', 'id',
                'customer', 'area'
                );

create table customer
(
        acs_code        char(8),
...
        area            char(2)         references area (id),
...
);

create trigger customer_pkref2
        before INSERT or UPDATE on customer
        for each row execute procedure
        check_primary_key ('area', 'area', 'id');


-- 
Oliver Elphick                                Oliver.Elphick@lfix.co.uk
Isle of Wight                              http://www.lfix.co.uk/oliver
               PGP key from public servers; key ID 32B8FAA1
                 ========================================
     "For the LORD is good; his mercy is everlasting; and 
      his truth endureth to all generations."         
                                     Psalms 100:5 





view thread (9+ messages)  latest in thread

Message-ID: <200001132121.VAA23887@linda.lfix.co.uk>
Permalink:  ../200001132121.VAA23887@linda.lfix.co.uk/
Also on:    postgresql.org/message-id/200001132121.VAA23887@linda.lfix.co.uk

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: olly@lfix.co.uk, JPausas@alehop.com, olly@linda.lfix.co.uk
  Subject: Re: [SQL] foreign key implementation
  In-Reply-To: <200001132121.VAA23887@linda.lfix.co.uk>
  References: <84f54420.442084f5@alehop.com> <200001132121.VAA23887@linda.lfix.co.uk>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox