|
|
 |
| SQL Server 2008 General discussion of SQL Server *2008* version only - not related to a specific book. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server 2008 section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

October 28th, 2009, 09:34 AM
|
|
Authorized User
|
|
Join Date: Sep 2005
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Question about a Many To Many Relationship
I apologize if this is not the correct forum to ask this question. I can't seem to get a straight answer to this question.
Given the following tables:
Customer
Primary Key CustomerID
Addresses
Primary Key AddressID
CustomerAddressesXref
Primary Key ID
Unique Key CustomerID + AddressID
There is a many to many relationship between Customers and Addresses, so I created the CustomerAddressesXref table.
Orders
Primary Key OrderID
The orders table contains a field called BillingAddressID.
Here is the question. Should the BillingAddressID on the orders table be constrained against the AddressID field on the Addresses table or the ID field in the CustomerAddressesXref table?
Can you explain the reasons behind the decision?
Thanks so much.
Last edited by mikener : October 28th, 2009 at 09:37 AM.
|

October 28th, 2009, 06:52 PM
|
|
Friend of Wrox
|
|
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,666
Thanks: 6
Thanked 9 Times in 9 Posts
|
|
How would I do this:
First I need to check, you say many to many because:
> a customer can have multiple addreses?
> and at any given address there can be multiple customers?
If the answer to both questions above is yes then I would call this a many to many relationship. if one of the answers is no then you dont have a many to many relationship situation.
NOTE - PK = Primary Key and FK = Foriegn Key (All my PK's are calle ID there is really no need to have such long field names.)
Customer tble
ID (PK)
Addresses tble
ID (PK)
customerAddresses tble
ID (PK)
customerID (FK related to customer.ID)
AddressID (FK related to addresses.ID)
Orders tble
ID (PK)
billingAddress (FK related to customerAddresse.AddressID)
Why - Becasue you need to know what address to send it to. When you think about it all the address ID's are linked...
|

October 28th, 2009, 07:23 PM
|
|
Authorized User
|
|
Join Date: Sep 2005
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by mat41
How would I do this:
First I need to check, you say many to many because:
> a customer can have multiple addreses?
> and at any given address there can be multiple customers?
If the answer to both questions above is yes then I would call this a many to many relationship. if one of the answers is no then you dont have a many to many relationship situation.
NOTE - PK = Primary Key and FK = Foriegn Key (All my PK's are calle ID there is really no need to have such long field names.)
Customer tble
ID (PK)
Addresses tble
ID (PK)
customerAddresses tble
ID (PK)
customerID (FK related to customer.ID)
AddressID (FK related to addresses.ID)
Orders tble
ID (PK)
billingAddress (FK related to customerAddresse.AddressID)
Why - Becasue you need to know what address to send it to. When you think about it all the address ID's are linked...
|
Thanks for responding. I am however confused about one thing. Based on the statement "Why - Becasue you need to know what address to send it to. " I would have thought that you would have said the billingAddress on the Orders table would relate to the Addresses table, not the CustomerAddresses table. Why is it better to relate to the link table?
Thanks
ps. Yes it is a Many to Many relationship - Each customer can have many addresses and each address can point to many customers.
|

October 28th, 2009, 07:41 PM
|
|
Friend of Wrox
|
|
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,666
Thanks: 6
Thanked 9 Times in 9 Posts
|
|
I probably didnt explain myself all that well. Yes you will get the physical address from the addresses table. Of course this is the only table where the text string of the address sits. The customerAddresses table only stores the ID. As I said all three table are related and have relationships. When customer places an order you will I assume need to ask them which one of thier multiple addresses they wish to be billed? therefore get the address from the address table. I would link orders to both tables and I would get the address like so:
SELECT addresses.physicalAddress FROM (((addresses
LEFT OUTER JOIN customerAddresses ON addresses.ID = customerAddresses.addressID)
LEFT OUTER JOIN orders ON customerAddresses.addressID = orders.billingAddress)
WHERE Orders.ID = " & ThecustomerAddressesID & ";"
The ThecustomerAddressesID variable would be identified by the customerID and the billingAddress they instructed the bill to be sent to
|

October 29th, 2009, 09:54 AM
|
|
Authorized User
|
|
Join Date: Sep 2005
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by mat41
I probably didnt explain myself all that well. Yes you will get the physical address from the addresses table. Of course this is the only table where the text string of the address sits. The customerAddresses table only stores the ID. As I said all three table are related and have relationships. When customer places an order you will I assume need to ask them which one of thier multiple addresses they wish to be billed? therefore get the address from the address table. I would link orders to both tables and I would get the address like so:
SELECT addresses.physicalAddress FROM (((addresses
LEFT OUTER JOIN customerAddresses ON addresses.ID = customerAddresses.addressID)
LEFT OUTER JOIN orders ON customerAddresses.addressID = orders.billingAddress)
WHERE Orders.ID = " & ThecustomerAddressesID & ";"
The ThecustomerAddressesID variable would be identified by the customerID and the billingAddress they instructed the bill to be sent to
|
If I understand you correctly you are saying that the BillingAddressID field on the orders table should store the ID of the CustomerAddressesXref table (which points to the Addresses table) instead of storing the AddressID on the addresses table.
Is this the standard approach DBA's use when setting up constraints against a many to many relationship?
Thanks so much for the help.
|

October 29th, 2009, 06:51 PM
|
|
Friend of Wrox
|
|
Join Date: Jan 2004
Location: Sydney, NSW, Australia.
Posts: 1,666
Thanks: 6
Thanked 9 Times in 9 Posts
|
|
No but I can see why you thought that, my mistake Above I said:
billingAddress (FK related to customerAddresse.AddressID)
it should have said:
billingAddress (FK related to Addresses.ID)
NOTE - You have figured out my 'CustomerAddresses' table is what you call 'CustomerAddressesXref' sorry I should have kept with your table name. I tend to name my tables simply which relect the core data. After all the table holds customer addresses....
I would relate orders.billingAddress to addresses.ID which means it stores the ID fro the addresses table
|

October 29th, 2009, 07:43 PM
|
|
Authorized User
|
|
Join Date: Sep 2005
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by mat41
No but I can see why you thought that, my mistake Above I said:
billingAddress (FK related to customerAddresse.AddressID)
it should have said:
billingAddress (FK related to Addresses.ID)
NOTE - You have figured out my 'CustomerAddresses' table is what you call 'CustomerAddressesXref' sorry I should have kept with your table name. I tend to name my tables simply which relect the core data. After all the table holds customer addresses....
I would relate orders.billingAddress to addresses.ID which means it stores the ID fro the addresses table
|
No need to apologize. I am just glad that we cleared that up. I asked the original question because my boss believes that the dependency should be against the cross reference table (CustomerAddressesXref). In fact, he believes that whenever there is a many to many relationship, tables like orders (or whatever) should always be constrained against a cross reference table. I don't have enough database design experience to argue with him. To make matters worse, he has less experience then I do.
Thanks
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |