views:

123

answers:

5

Hi. Sorry if the title is hard to understand.. :S

I have a table with an order, and this order should be able to have an unknown number of pizzas. How do I implement this?

I have a table for pizzas, and a row for PizzaID. The order table should have an unkown number of pizzaIDs.. :S

Thanks

+1  A: 

You should link them the other way around. The pizza should indicate which order it's associated with, as each pizza will only come from one order.

For example, your pizza record might look something like this:

PizzaId, **OrderId**, BaseType, StuffedCrust, PizzaType

Then you can simply query the database for all pizzas related to order n.

Dave
+4  A: 

You need a table for Orders, a table for Pizzas and a junction table associating Pizzas with Orders

Pizza                 
-----  
Id
Name             

Order
-----
Id
Date_Of_Order

Pizza_Order
-----------
Order_Id
Pizza_Id
Quantity
Russ Cam
Agreed, although my preference would be to keep the same column names for the ID's across all tables: PizzaId, OrderId
Yoav
Surely introducing a third table just adds extra unnecessary complication? Why not just link the pizza back to the order by an OrderId? You could still include a quantity if you wanted to indicate a number of identical pizzas without having to insert one record for each.
Dave
@Dave - if I've understood the OP's question correctly, a record in the Pizza table represents a particular `Pizza` (toppings, base type, etc). Your suggestion would mean that each particular pizza could only be included in one particular `Order`. the junction table is necessary to be able to map many `Pizzas` to many `Orders`.
Russ Cam
@Yoav- Each to their own preference :) I prefer to keep the Id column in a table representing separate entities named as `Id` and prefix with the entity name in junction tables / foreign key fields. Specifying `EntityId` feels superfluous to me - where do you draw the line? Do you name other fields `EntityFieldName` e.g. `PizzaName`, `PizzaBaseType`, etc?
Russ Cam
@Dave - Continuing with that line of thinking, it would seem that the structure given in your answer would be a junction table too, where `PizzaId` is a foreign key to a `Pizza` table and `OrderId` a foreign key to an `Order` table? Is that correct?
Russ Cam
@Russ Not really, the PizzaId would be the primary key for that table, rather than looking up to another table. The PizzaType in the example was more for brevity than functionality, I'd actually expect something along the lines of BaseType, CrustType, a number of toppings fields (or perhaps a third table here for variable # of toppings), etc. The pizza record in my mind would be a composite allowing for full customisation. Many customized pizzas to an order, one order containing many pizzas.
Dave
@Dave - That would work too, but there will come a point where there will be duplicate "customised" Pizzas in the table, only with a different PizzaId and OrderId. This duplication could be *normalized out* by having a `Pizza` Entity table that holds all of the different "customised" pizzas
Russ Cam
A: 

Add an 'OrderID' column to the Pizzas table, and make it a foreign key to the 'ID' column in the Orders table.

This is fairly basic relational database design stuff, you might want to look at some basic tutorials.

belugabob
A: 

You need three tables:

  • Pizzas (with PizzaID + other columns that define a particular pizza)
  • Orders (with OrderID + additional columns that you need like customer, order datetime etc...
  • PizzaOrders (with OrderID, PizzaID and Quantity) - several rows will have the same OrderID, but different PizzaID values + number of these pizzas.

So you have a many-to-many relationship between Pizzas and Orders tables.

Robert Koritnik
A: 

I would create 3 tables here

One for orders

One table orderItems that is 1-n linked to te orders table by storing the orderID and n-1 to Pizzas by Storing the PizzaID. You should also store the price information in this table, since the price of a pizza might change over time, and that could affect your old orders

and another table Pizzas that will contain all available Pizza Items (Call it MenueItems if you plan to expand to pasta and beverages later on ;) )

Heiko Hatzfeld