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
___________________________________
Search IT jobs from multiple sources- http://www.ITjobfeed.com
"Aidy" <ai...@xxnoemailxx.com> wrote in message
news:99SdnW4Le-s...@bt.com...
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.
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
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
"David Portas" <REMOVE_BEFORE_R...@acm.org> wrote in message
news:TuWdnRkMHuRBNzva...@giganews.com...
> 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.
And why would that be a problem?
--
David Portas
"David Portas" <REMOVE_BEFORE_R...@acm.org> wrote in message
news:PfydnZolv9gl8jra...@giganews.com...
--
Jack Vamvas
___________________________________
Search IT jobs from multiple sources- http://www.ITjobfeed.com
"Aidy" <ai...@xxnoemailxx.com> wrote in message
news:GIidnWkuUKUBtzXa...@bt.com...