Can't think right ... quick database normalization question

Status
Not open for further replies.

FormerlyDZone

Ars Praefectus
4,581
Subscriptor
I have an order table that is an order for a customer. I need to reference as a foreign key an unknown number of products that could make up the order (product_id) how do I do this? What am I missing. I set up an intermediary table between ORDER and PRODUCTS but I can't wrap my head around what the composite key should be since I can't use Order_id as part of it (cause it would still be many to many) right now I have order_id, quantity, and price... what am I missing so that I only have known quantity in the database?

Or should I just have a column "product_ids" and do a comma separate list of product_id (array style)

thanks

-zone
 

Ardax

Ars Legatus Legionis
19,795
Subscriptor
Your intermediary table (I'll call it ORDER_ITEMS) should reference both order_id and product_id. You'd have a 1:n relationship between ORDERS and ORDER_ITEMS on order_id. You'd ultimately have a m:n relationship between ORDER_ITEMS and PRODCUTS, but I don't see why that's bad.

If the same product could show up multiple times on the same order, then you'll need some kind of artificial PK.
 

Ardax

Ars Legatus Legionis
19,795
Subscriptor
Of course. :) It depends on why the same product shows up more than once. If it's something like "I want 500 business cards with this name on them, 500 with that name, and 500 with this other name" then I'm not sure I'd want to make a varchar field part of my PK.

Oh, and this:
Or should I just have a column "product_ids" and do a comma separate list of product_id (array style)
D:
 

FormerlyDZone

Ars Praefectus
4,581
Subscriptor
newwb":1aqgrk4t said:
ShuggyCoUk":1aqgrk4t said:
Or should I just have a column "product_ids" and do a comma separate list of product_id (array style)

wash your mind out with soap!
Listen to Ardax and The Dan.
Yeah, that really is a terrible way to represent a many-to-many…



Pipe-delimited is far better.

Yea I know but I dont' do this stuff often (it's for a class .. I don't do it at all in my primary field so I was just throwing out ideas)
 

FormerlyDZone

Ars Praefectus
4,581
Subscriptor
Ardax":2mp08y4p said:
Your intermediary table (I'll call it ORDER_ITEMS) should reference both order_id and product_id. You'd have a 1:n relationship between ORDERS and ORDER_ITEMS on order_id. You'd ultimately have a m:n relationship between ORDER_ITEMS and PRODCUTS, but I don't see why that's bad.

If the same product could show up multiple times on the same order, then you'll need some kind of artificial PK.

But even then I would have order number appear multiple times. Would that be ok if my primary key was a composite?

example
ORDER
1
2
3
4

PRODUCTS
A
B
C
D

ORDER_ITEMS
1 : A
1 : B
2 : B
2 : C
3 : D
4 : A
4 : D


I thought you couldn't have the same data in multiple rows if it was a primary key.

If this is the case would the primary key in ORDER_ITEMS be Orderid+productID(+QUANTITY potentially)

then ORDER would contain (among others) 2 columns
order_id and the foreign key orderid+productid

Thanks for putting up with this, it's tough to wrap my head around!
 

HitScan

Ars Tribunus Militum
2,523
Subscriptor++
Mrdzone":1cpdoqw7 said:
I thought you couldn't have the same data in multiple rows if it was a primary key.

That's true, if you're talking about the primary key for that table. When you reference a primary key from one table in another table, it's a foreign key, and you can reference the same foreign key as many times as you like. So, in your example, ORDER has 1 field which is also the primary key, PRODUCTS has 1 field which is it's primary key, and ORDER_ITEMS has 2 fields which are both foreign keys. There isn't any field currently in ORDER_ITEMS that could be a primary key, though you could add one if you wanted (or it is required for the assignment, etc.).
 

Ardax

Ars Legatus Legionis
19,795
Subscriptor
I thought you couldn't have the same data in multiple rows if it was a primary key.
The combination of order_id + product_id (+ whatever_else) is your primary key, so that composition is what must be unique.

Think about it this way: It wouldn't make much sense to be able to create a composite PK if each component of the PK also had to be unique. It also wouldn't be very useful.
 

FormerlyDZone

Ars Praefectus
4,581
Subscriptor
HitScan":208pim41 said:
Mrdzone":208pim41 said:
I thought you couldn't have the same data in multiple rows if it was a primary key.

That's true, if you're talking about the primary key for that table. When you reference a primary key from one table in another table, it's a foreign key, and you can reference the same foreign key as many times as you like. So, in your example, ORDER has 1 field which is also the primary key, PRODUCTS has 1 field which is it's primary key, and ORDER_ITEMS has 2 fields which are both foreign keys. There isn't any field currently in ORDER_ITEMS that could be a primary key, though you could add one if you wanted (or it is required for the assignment, etc.).

Yea no the assignment is actually dead simple and I've already done it, he just wanted a database and said he didn't care about normalization (rough draft for a later project I assume) but I figured I'd get a head start as I do know a little bit about this stuff but not enough to be proficient.

Thanks for the help guys!
 

Xavin

Ars Legatus Legionis
30,828
Subscriptor++
Mrdzone":3si3mwiy said:
Yea I know but I dont' do this stuff often (it's for a class .. I don't do it at all in my primary field so I was just throwing out ideas)
Just for reference, a nasty hack like delimited text in a database field is only acceptable when it's 5:00am, there's a meeting at 8:00am where it needs to work, and you're completely at a loss as to how to do it the right way.
 
Status
Not open for further replies.