Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

NULL Foreign Keys

1 view
Skip to first unread message

Aidy

unread,
Jan 24, 2008, 8:12:02 AM1/24/08
to
I have a table, the entry of which may or not relate to a row in another
table. Is it ok to make that field a nullable foreign key?

Example;

Car_ID, Car
1, Ford
2, Nissan

User_ID, Car_ID
1, 2
2, NULL
3, 1

So user 1 has a Nissan, but user 2 has no car at all. I know an alternative
is to have a seperate linking table that dictates who has a car, and I know
that approach has its pros and cons - but basically, for the sake of
simplicity in data representation, joining in queries, number of tables to
manage etc is it accepted as "ok" to have nullable FKs? I've found a few
articles that pretty much sum up what I feel the answer to be - ie use the
right tools for the job at hand, and also some that say "yes it's fine" and
some folk that so "no it's horrid".

Just wondered if people had thoughts on this.


Jack Vamvas

unread,
Jan 28, 2008, 4:16:26 AM1/28/08
to
This is quite a common scenario ; for example in a Parent - Child scenario ,
you enter the child data without knowing what the Parent data is at present.
This is usually a reflection of bad data. The best thing to do is to add a
CONSTRINT that checks and handles this type of data.


--

Jack Vamvas
___________________________________
Search IT jobs from multiple sources- http://www.ITjobfeed.com


"Aidy" <ai...@xxnoemailxx.com> wrote in message
news:99SdnW4Le-s...@bt.com...

Aidy

unread,
Jan 28, 2008, 8:00:34 AM1/28/08
to
> you enter the child data without knowing what the Parent data is at
> present

It's not that, it's more like a "may have" relationship. Ie a user "may
have" a car, but if they don't their Car_ID is NULL. Obviously the User
table contains more info, not just the Car_ID.


David Portas

unread,
Feb 4, 2008, 1:04:11 AM2/4/08
to
"Aidy" <ai...@xxnoemailxx.com> wrote in message
news:_JadnVsW4O1...@bt.com...

Assuming User_ID is the primary key of User, you don't need a nullable
foreign key at all:

CREATE TABLE User
(User_ID INT NOT NULL PRIMARY KEY,
/* ... other columns */);

CREATE TABLE Car
(Car_ID INT NOT NULL PRIMARY KEY,
/* ... other columns */);

CREATE TABLE UserCar
(User_ID INT NOT NULL PRIMARY KEY,
Car_ID INT NOT NULL REFERENCES Car(Car_ID));

This is called non-loss decomposition and it results in a schema in 5th
Normal Form, something that ought to be your goal when designing any SQL
database.

--
David Portas


David Portas

unread,
Feb 4, 2008, 1:07:52 AM2/4/08
to
"David Portas" <REMOVE_BEFORE_R...@acm.org> wrote in message
news:TuWdnRkMHuRBNzva...@giganews.com...

>
> CREATE TABLE UserCar
> (User_ID INT NOT NULL PRIMARY KEY,
> Car_ID INT NOT NULL REFERENCES Car(Car_ID));
>

Oops. I missed the other FK:

CREATE TABLE UserCar
(User_ID INT NOT NULL PRIMARY KEY REFERENCES User (User_ID),


Car_ID INT NOT NULL REFERENCES Car(Car_ID));

--
David Portas


Aidy

unread,
Feb 4, 2008, 5:23:45 AM2/4/08
to
Yeah I get that, but it requires the addition of an extra table.

"David Portas" <REMOVE_BEFORE_R...@acm.org> wrote in message
news:TuWdnRkMHuRBNzva...@giganews.com...

Ed Murphy

unread,
Feb 4, 2008, 9:44:43 AM2/4/08
to
Aidy wrote:

> Yeah I get that, but it requires the addition of an extra table.

Let me show you this SYCDEFIL table that I've been working with
lately. Extra tables are not always a bad thing.

David Portas

unread,
Feb 4, 2008, 3:04:06 PM2/4/08
to
"Aidy" <ai...@xxnoemailxx.com> wrote in message
news:B4SdnXstl8Qpejva...@bt.com...

> Yeah I get that, but it requires the addition of an extra table.
>

And why would that be a problem?

--
David Portas


Aidy

unread,
Feb 5, 2008, 4:21:32 AM2/5/08
to
Usually more admin required (CMS I mean) mainly.

"David Portas" <REMOVE_BEFORE_R...@acm.org> wrote in message

news:PfydnZolv9gl8jra...@giganews.com...

Jack Vamvas

unread,
Feb 16, 2008, 11:04:17 AM2/16/08
to
In a CMS scenario , the only things you would need to change (once you've
set up the tables) is your sql statements

--

Jack Vamvas
___________________________________
Search IT jobs from multiple sources- http://www.ITjobfeed.com

"Aidy" <ai...@xxnoemailxx.com> wrote in message

news:GIidnWkuUKUBtzXa...@bt.com...

0 new messages