Обсуждение: NULL Foreign Key
Greetings, I'm about to conclude that I can't do this, but I thought I'd ask first: Can I make a foreign key that is allowed to be NULL? I have several situations where I want a field to reference another table, but not be required. thanks, Dylan Kuhn New Directions Technologies, Inc.
On Tue, 16 Jul 2002 17:10:32 -0700, "Kuhn, Dylan K (NDTI)" <KuhnDK@navair.navy.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