If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > Neatest way of modelling zero-or-one to many rels?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-12-10, 06:43
Anna-J Anna-J is offline
Registered User
 
Join Date: Jan 2010
Posts: 19
Neatest way of modelling zero-or-one to many rels?

What do you think it the neatest way of modelling 'zero-or-one to many' relationships, using a nullable foreign key or an association table?

Example: a project can have one manager assigned to it, or be unassigned.

[Project Mgr] 0..1 <------ 0..n [Project]

An association table is a bit of an overkill, but a nullable FK 'smells' a bit wrong, so I'd like to hear your opinions.
Reply With Quote
  #2 (permalink)  
Old 07-12-10, 06:57
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Nullable FK
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #3 (permalink)  
Old 07-12-10, 09:53
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
Nullable FK.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #4 (permalink)  
Old 07-12-10, 11:49
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Nullable FK.

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #5 (permalink)  
Old 07-12-10, 11:56
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
oh, all right... nullable FK
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 07-12-10, 14:58
dportas dportas is offline
Registered User
 
Join Date: Dec 2007
Location: London, UK
Posts: 732
Quote:
Originally Posted by Anna-J View Post
What do you think it the neatest way of modelling 'zero-or-one to many' relationships, using a nullable foreign key or an association table?

Example: a project can have one manager assigned to it, or be unassigned.

[Project Mgr] 0..1 <------ 0..n [Project]

An association table is a bit of an overkill, but a nullable FK 'smells' a bit wrong, so I'd like to hear your opinions.
I would do it like this:

Code:
CREATE TABLE ProjectProjectMgr
(ProjectId INT NOT NULL PRIMARY KEY REFERENCES Project (ProjectId),
 ProjectMgr INT NOT NULL REFERENCES ProjectMgr (ProjectMgrId));
(which may be what you mean by an "association table").

If a project doesn't have a project manager then it doesn't need a project manager Id - much less a null Id. Only add nulls into a schema where there is some good reason for doing so. And it pays to be doubly cautious when considering nullable foreign keys because they have significant disadvantages.
Reply With Quote
  #7 (permalink)  
Old 07-12-10, 16:14
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by dportas View Post
Only add nulls into a schema where there is some good reason for doing so.
okay, provided that i get to decide what good means

Quote:
Originally Posted by dportas View Post
And it pays to be doubly cautious when considering nullable foreign keys because they have significant disadvantages.
o rly?

significant how, if i may ask?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 07-13-10, 07:37
Anna-J Anna-J is offline
Registered User
 
Join Date: Jan 2010
Posts: 19
Nice to see that there's a consensus that nullable FKs are ok to use. A nullable FK it is, then!
Reply With Quote
  #9 (permalink)  
Old 07-13-10, 07:49
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
I could have told you before people started responding that everyone bar one would have recommended that.

Also, I was tempted to say so initially but left it, but smell is not ordinarily a recognised stage in database design. Make whatever decisions you believe you can justify but justify them with reason. I am sure it is a small olfactory step to thinking that shoving an autonumber on to a table smells like normalisation.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #10 (permalink)  
Old 07-13-10, 15:17
dportas dportas is offline
Registered User
 
Join Date: Dec 2007
Location: London, UK
Posts: 732
Quote:
Originally Posted by Anna-J View Post
Nice to see that there's a consensus that nullable FKs are ok to use. A nullable FK it is, then!
Certainly if you want to make design decisions based on acclaimation on DBFORUMS. Even though I disagreed I wouldn't have wanted you to follow my example just because you read it here. Even less so if it happened to be the majority view. A million people are always wrong.

What I would suggest is that you study some decent books on database design and make sure you understand what are the potential advantages and disadvantages of nulls. Then you'll have more information with which to make up your own mind.
Reply With Quote
  #11 (permalink)  
Old 07-15-10, 17:04
rdjabarov rdjabarov is offline
Registered User
 
Join Date: Jul 2003
Location: San Antonio, TX
Posts: 3,611
Association table ONLY, very rarely you'd use a "nullable" FK. I usually use them after the fourth Heineken.
__________________
"The data in a record depends on the Key to the record, the Whole Key, and
nothing but the Key, so help me Codd."
Reply With Quote
  #12 (permalink)  
Old 07-16-10, 03:33
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Quote:
Originally Posted by dportas View Post
it pays to be doubly cautious when considering nullable foreign keys because they have significant disadvantages.
Quote:
Originally Posted by dportas View Post
make sure you understand what are the potential advantages and disadvantages of nulls.
Dave - I know most of the disadvantages of NULLs. With the exception of RDBMSs that stick to the ISO SQL standard where it comes to NULLable composite foreign keys (courtesy of you) I don't know of any NULL issues that are specific to foreign keys.
Care to enlighten?
In case it is relevant, the longer I work in the field the more and more dissatisfied I am becoming with NULLs. I'm not yet at the point that I would eliminate them from my every schema but I am moving in that direction.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #13 (permalink)  
Old 07-16-10, 10:28
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
Quote:
Originally Posted by rdjabarov View Post
Association table ONLY, very rarely you'd use a "nullable" FK. I usually use them after the fourth Heineken.
Quote:
Originally Posted by dportas View Post
Only add nulls into a schema where there is some good reason for doing so.
Still waiting for a good reason for NOT using a NFK (outside of Happy Hour).
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #14 (permalink)  
Old 07-16-10, 13:42
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by blindman View Post
Still waiting for a good reason for NOT using a NFK (outside of Happy Hour).
so am i, and i asked before you did
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #15 (permalink)  
Old 07-16-10, 16:13
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
One reason for not using NULL-able Foreign Keys is that they force you to use outer joins in order to get correct answers, and to cope with the results of unmatched keys (many more NULL values) that those joins imply. This is a complete "straw man" in my mind, it is a factually correct but practically irrelevant point to me.

A better reason in my mind is that Foreign Keys imply a relationship. I usually track many details about a relationship like when it began, when it ended, etc. When I model things that have relationships, I normally treat the relationship itself as a "thing" to be modeled because that fits well with my view of my environment.

I see my environment as a rich and varied place. I'm rarely willing to compromise and represent things very simply in order to make the code very simple because that has never worked to my benefit. I always end up coming back and implementing most if not not more than I originally imagined, and then I have to deal with the "lost data" and the complexity of a system/schema upgrade on what is often a critically important system... This just is bad business to me.

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On