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 > Relationships

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-12-04, 15:26
kram66 kram66 is offline
Registered User
 
Join Date: Feb 2004
Posts: 86
Relationships

What is the easiest way to workout a one-to-one, one-to-many, mandatory-one-to-many and optional-one-to-many etc from a database?
Reply With Quote
  #2 (permalink)  
Old 03-15-04, 08:48
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: Relationships

If the database was well designed, the constraints will tell all. Consider 2 tables A and B:
  • optional one-to-one: B has a foreign key F to A and there is a unique constraint on the columns of F and the columns of F allow NULLs.
  • mandatory one-to-one: B has a foreign key F to A and there is a unique constraint on the columns of F and the columns of F do not allow NULLs.
  • optional one-to-many: B has a foreign key F to A and there is no unique constraint on the columns of F and the columns of F allow NULLs.
  • mandatory one-to-many: B has a foreign key F to A and there is no unique constraint on the columns of F and the columns of F do not allow NULLs.
A good CASE tool could reverse engineer the entity model from such a database.

If it was not well designed, you will have to infer the relationships from the data, and verify them with a knowledgeable user (e.g. "it appears that each FOO may have 0 or many BARs - is that correct?")
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 03-18-04, 03:57
kram66 kram66 is offline
Registered User
 
Join Date: Feb 2004
Posts: 86
So if the table Customer has a primary key of cusno
and the Order table has a primary key of orderno

The Order table has a foriegn key of customer.cusno what does it make the relationship? There are no constraints supplied?
Reply With Quote
  #4 (permalink)  
Old 03-18-04, 05:03
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Clearly one customer may have many orders, and each order is for exactly one customer.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #5 (permalink)  
Old 03-18-04, 08:07
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Quote:
Originally posted by andrewst
Clearly one customer may have many orders, and each order is for exactly one customer.
Clearly ?!?!

-PatP
Reply With Quote
  #6 (permalink)  
Old 03-18-04, 08:20
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Quote:
Originally posted by Pat Phelan
Clearly ?!?!

-PatP
Well, I thought so!
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #7 (permalink)  
Old 03-18-04, 09:05
Matt_T_hat Matt_T_hat is offline
Registered User
 
Join Date: Sep 2003
Location: UK
Posts: 122
It is clearly only if you are sad like me and spend all day doing databases.

For normal people it might be an idea to toy with a good diagram method (ERD-Object Modling methods for example) that let you "see" the relationships.
__________________
Matt the Hat says: "what!?"
A child of five could understand this! Fetch me a child of five!
SCARY HARD CHALLENGE: http://www.dbforums.com/database-concepts-design/988682-better-relational-design.html
Reply With Quote
  #8 (permalink)  
Old 03-18-04, 09:42
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Quote:
Originally posted by Matt_T_hat
It is clearly only if you are sad like me and spend all day doing databases.

For normal people it might be an idea to toy with a good diagram method (ERD-Object Modling methods for example) that let you "see" the relationships.
What about joint-purchase covenants, which prohibit a single legal entity (person, corporation, etc) from owning property? That would require multiple customers for a single invoice.

I guess the point that I was trying to make is that we as data modelers can't assume anything until it is verified by the problem-domain experts (the end users).

-PatP
Reply With Quote
  #9 (permalink)  
Old 03-18-04, 09:45
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
My assessment was based on the information given that "The Order table has a foriegn key of customer.cusno what does it make the relationship?"

You can only put one customer in a row in the Order table, which means the order hasonly 1 customer.

Remember, this question was about working backwards from the database to the ERD...
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #10 (permalink)  
Old 03-18-04, 11:14
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Ah, good point. I should have re-read the whole thread. Sorry.

-PatP
Reply With Quote
  #11 (permalink)  
Old 03-19-04, 15:34
kram66 kram66 is offline
Registered User
 
Join Date: Feb 2004
Posts: 86
Thanks for the great answers, but what about a many-to-many relationship, how do you calculate this type?
Reply With Quote
  #12 (permalink)  
Old 03-19-04, 15:58
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
A many-to-many relationship would be implemented by an "intersection table" like this one (between employee and project):

create table emp_project
( emp_id references employee
, proj_id references project
, ...
, constraint emp_project_pk primary key (emp_id, proj_id)
);
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #13 (permalink)  
Old 03-22-04, 04:40
Matt_T_hat Matt_T_hat is offline
Registered User
 
Join Date: Sep 2003
Location: UK
Posts: 122
What he said...

but with a picture

This is a many to many relationship worked out in a database

This is simply a picture of what andrewst is saying.
Attached Images
File Type: jpg many-to-many.jpg (7.5 KB, 125 views)
__________________
Matt the Hat says: "what!?"
A child of five could understand this! Fetch me a child of five!
SCARY HARD CHALLENGE: http://www.dbforums.com/showthread.php?threadid=988682
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