Re: NULL Foreign Key

From: Manfred Koizar <mkoi-pg(at)aon(dot)at>
To: "Kuhn, Dylan K (NDTI)" <KuhnDK(at)navair(dot)navy(dot)mil>
Cc: "'pgsql-general(at)postgresql(dot)org'" <pgsql-general(at)postgresql(dot)org>
Subject: Re: NULL Foreign Key
Date: 2002-07-17 12:42:50
Message-ID: fdpajuog4ra0cuuntlfa2b05350js74ct1@4ax.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, 16 Jul 2002 17:10:32 -0700, "Kuhn, Dylan K (NDTI)"
<KuhnDK(at)navair(dot)navy(dot)mil> wrote:
>Can I make a foreign key that is allowed to be NULL?

Yes:

fred=# CREATE TABLE father (i INT PRIMARY KEY);
NOTICE: CREATE TABLE/PRIMARY KEY will create implicit index
'father_pkey' for table 'father'
CREATE
fred=# CREATE TABLE son (i INT REFERENCES father);
NOTICE: CREATE TABLE will create implicit trigger(s) for FOREIGN KEY
check(s)
CREATE
fred=# INSERT INTO father VALUES (1);
INSERT 183317 1
fred=# INSERT INTO son VALUES (1);
INSERT 183318 1
fred=# INSERT INTO son VALUES (2);
ERROR: <unnamed> referential integrity violation - key referenced
from son not found in father
fred=# INSERT INTO son VALUES (NULL);
INSERT 183320 1

Servus
Manfred

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Henrik Steffen 2002-07-17 12:55:11 Re: max() not using index
Previous Message Peter Gibbs 2002-07-17 12:40:27 Re: max() not using index