Database relationship design
Enverex
Worcester, UK Icrontian
Right, some of my coursework is creating a database and linking the tables acordingly, but I just cant work this one thing out.
The company wants to keep a person or companies records for the future, and some other details, so I make that an entity with the appropriate fields (customer ID, name, dob, phone, etc).
Next they need the order information stored, such as date of the order, progress, date of payment, and customer ID for linking)
But they also want to keep on record, roughly 10 different types of products, so you think, fine, have a table called products, with the fields product ID, name, availability, price, etc. But the point is, how would I link it there?
See, Order is linked from Customer, by putting another Customer ID field in the order table, then giving it a "One customer, to many orders" relationship.
Now what I need to set-up is a "One order to Many Products" relationship.... but what fields could I link there? As I couldn't put a product id field in the order table as they would only allow for one product, and I couldn't put an Order ID in the product table as that would permaently associate that product with that order....
The thing I am thinking of at the moment is making a fourth table which will be unique to each order (thus containing the Order ID) and linking that to both the Order (through the Order ID) and linking it to the table of products through the ID, so that when the ID is entered in the orders product table, the rest will fill itself in.
Does that sound right?
EX
The company wants to keep a person or companies records for the future, and some other details, so I make that an entity with the appropriate fields (customer ID, name, dob, phone, etc).
Next they need the order information stored, such as date of the order, progress, date of payment, and customer ID for linking)
But they also want to keep on record, roughly 10 different types of products, so you think, fine, have a table called products, with the fields product ID, name, availability, price, etc. But the point is, how would I link it there?
See, Order is linked from Customer, by putting another Customer ID field in the order table, then giving it a "One customer, to many orders" relationship.
Now what I need to set-up is a "One order to Many Products" relationship.... but what fields could I link there? As I couldn't put a product id field in the order table as they would only allow for one product, and I couldn't put an Order ID in the product table as that would permaently associate that product with that order....
The thing I am thinking of at the moment is making a fourth table which will be unique to each order (thus containing the Order ID) and linking that to both the Order (through the Order ID) and linking it to the table of products through the ID, so that when the ID is entered in the orders product table, the rest will fill itself in.
Does that sound right?
EX
0
Comments
In your case, you'd have an Order ID foreign key linked to a Product ID foreign key. Each Order/Product combo essentially makes its own unique ID. The only drawback, now that I think about it, is it only allows for the customer to purchase one of each item. I guess you'd have to add a quantity column.
So you'd have something like:
And so on.
EX
Modern docs are as much query results as they are DB structure, and the more you use queries the simpler your base data set can be. Use field to field relations when possible, one record per order not per item, unless you want each query to gen one order form line. Simplify the one-to-manys when possible and reserve to use for where you have to, like aggregate reports of sales by customer, or "who buys what" queries--invoice gen does not need to be that way. With one to manys, you get into multiple queries in certain orders, or you get utter hash out. Technically, you get into itterative loops which are each multiple or many queries per loop, with every one-to-many you try to manage.
John.
Below is a pic of it working, and I think that is pretty much what I need. The hard part is explaining how I did it all in about 2000 words. Maybe you could write it for me, eh Ageek?
EX