Re: constraint/restrict - Mailing list pgsql-sql

From Richard Huxton
Subject Re: constraint/restrict
Date
Msg-id 00a801c09679$c57a26e0$1001a8c0@archonet.com
Whole thread Raw
In response to constraint/restrict  (Olaf Marc Zanger <olaf.zanger@soli-con.com>)
List pgsql-sql
From: "Olaf Marc Zanger" <olaf.zanger@soli-con.com>

> hi there,
>
> with two tables i want to make some constraint-restrictions
>
> to make sure that now country-row is deleted if there is still a
country_id
> in address table.
>
> e.g.
>
> address: 1, 2, ...
> country: 2, ...
>
> now country wouldn't be allowed to be deleted.
>
> how to do that?

You want a foreign-key (only in version 7) - check the reference manual for
CREATE TABLE - and look for the keyword REFERENCES

Basically, it's like:

create table foo (fooid serial unique, footxt text);

create table bar (barid serial, barfoo int4 references foo (fooid), bartxt text);

Then after a few inserts...

delete from foo where fooid=1;
ERROR:  <unnamed> referential integrity violation - key in foo still
referenced from bar

- Richard Huxton



pgsql-sql by date:

Previous
From: Olaf Marc Zanger
Date:
Subject: constraint/restrict
Next
From: Ines.Klimann@liafa.jussieu.fr
Date:
Subject: Oracle8 / PostgreSQL