I'm starting with MySQL database relashionships and I have a question.
I'm going to put in an exemple:
Table A = customer
Table B = products
Table C = sales
I wanna know how can I make a relationship with these tables where "sales" has only one customer and multiple produts.
The "sales" table can't have a Primary Key with both foreign keys, cuz it will have multiple products.
I could create a ´idsale´, but how I would structure the table to receive multiple products?
Thank you.
Costumers and Sales have a ONE-TO-MANY relation. You could add a customer reference on the Sales table
Table sales
- id PK
- customers_id FK
Sales and Product have a MANY-TO-MANY relation. You will need a new table to map this relation
Table sales_has_products
- sales_id FK
- products_id FK
I would recommend creating a fourth table, let's call it productSale, that has its own primary key, as well as foreign keys the sale ID and the product ID. Sale, then, will just have an ID and the foreign key of the customer ID (as well as any additional info you want, of course.)
This way, a sale can include multiple items of the same product (because all productSale rows have a unique ID), and all of the items included in a sale can be found by querying productSale by the specific sale ID.
Related
I have a CUSTOMERS table and a CONTACTS table the relation between them is one to many obviously.
also I have PROJECTS table and PROJECT_CUSTOMERS table with relation one to many and with relation one to one between CUSTOMERS and PROJECT_CUSTOMERS.
my problem is that I have a fifth table PROJECT_CONTACTS ....I can't figure which tables shall I refer to in this tables, currently I am refering to PROJECT_CUSTOMERS and CONTACTS table, is this correct or there is something better ?
Your title refers to "foreign keys" but your question just seems to be about what columns should go in what tables.
First, decide what situations can arise and what you want/need to say about them. (This will your tables, columns, candidate keys, foreign keys and constraints.)
Every table holds rows that make some predicate (statement template parameterized by column names) true. Your design seems to have:
CUSTOMERS(id, ...) -- ID identifies a customer and ...
CONTACTS(id, ...) -- ID identifies a contact and ...
PROJECTS(id, ...) -- ID identifies a project and ...
PROJECT_CUSTOMERS(pid, cust_id, ...) -- project PID has customer CUST_ID and ...
PROJECT_CONTACTS(pid, cont_id, cust_id)...)
-- project PID has contact CONT_ID and project pid has customer CUST_ID and ...
A foreign key has a table & column list referencing a table and column list that forms a candidate key. It says that lists of values in the first table appear as lists of values in the second table. Where that is so, declare a foreign key.
i am building an invoicing application consisting of following business logic.
a) Place a new order for customer. (an order is a group of three
related components, estimate, invoice and purchaseorder)
b) After placing an order a new estimate can be generated. an order
will have only one estimate.(An estimate consist of item details)
c) With reference to an estimate of the order. an invoice can be
generated. an invoice qualify for the discount of price. apart from
item details, an invoice consist of some expenses.an order can contain
only one invoice
d) with reference to an invoice of the order, PurchaseOrder can be
generated. PurchaseOrder consist of item information about vendor
purchase. an order can contain multiple PurchaseOrder.
here is the database table design i have come up with.
while all look good, i am having difficulty deciding where to store the items list that belongs to particular estimate, invoice or purchaseorder of the order.
i had thought of several solution.
Approach A : create different tables for item list for each. (estimate, invoice and purchase order)
table : estimate_item , invoice_item , purchaseorder_item.(this tables contains columns similar to that of
order_item in above image).
problem: problem with this approach is all the three tables consist of identical columns storing identical information, the only
difference is foreign key that will be stored.
Approach B: create one item list table order_item
tablename: order_item
problem: not sure what to store as foreign key in this table since the foreign key can be from three different table. i thought of few
way of handling foreign keys in this table as follows.
1)foreignKey table reference column: type (example values: estimate, invoice, purchaseorder) foreignKey column: type_id(consist
of foreignKey of any of three tables) problem: i am using naming
convention for column names for example column name ending with tablename_id defines the foreign key. and this method violate the rules.
2) foreignKeyColumn: order_id , estimate_id , invoice_id ,
purchaseorder_id. problem: unnecessary foreign key columns
defined.
i want to know how should i store the foreign key in order_item table so that it identifies the order and estimate/invoice/purchaseorder it belongs too.
the relationship for tables are:
id is the primary key for all the tables
table name: order relates to (contact, estimate, invoice, shipment) tables.
column name: contact_id (foreign key(referring to id column of the contact table)).
column name: estimate_id (foreign key(referring to id column of the estimate table)).
column name: invoice_id (foreign key(referring to id column of the invoice table)).
column name: shipment_id (foreign key(referring to id column of the shipment table)).
tablename: purchaseorder (this have one to many relationship with order table)
column name: order_id (foreign key(referring to id column of the order table)).
column name: contact_id (foreign key(referring to id column of the contact table)).
the question is about how to go with the storing of foreign keys in order_item table.
Thank you.
Update 1:
Please note that each table estimate , invoice and purchaseorder will have item of it's own and having no relation to each other.
Hi I'm not sure how the relations happen. for instance, you have 'estimate' pointing to 'order item' but I don't see what key you have to make that join (or look-up). as another 'order' points to 'estimate' but how are those two joined? I dont see any shared attributes that both those entities have.
I'm assuming 'id' is just something to make rows in each particular table unique, but are not id's that have value to the application. so, I would think you' need to carry estimate.reference number into the 'order item' table. This is just a cursory comment.
also, would hep for clarity if keys were listed first. so in 'order item' you have attribute 'order id' (which appears to be an FK) buried in the end of the list of other attributes. makes this hard to read.
If I understand you correctly, each document associated with an order (i.e. an estimate, purchaseorder and/or invoice) might contain a different list of items.
If that is the case, I would probably create a Documents table along the following lines:
CREATE TABLE Documents (
DocumentId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
OrderId INT NOT NULL,
-- you can move any fields common to all document types here
-- e.g. date created, reference #, etc.
FOREIGN KEY (OrderId) REFERENCES order (id)
);
And then your order_item, estimate, purchaseorder and invoice tables all reference their associated entry in this table:
ALTER TABLE [tablename]
ADD COLUMN DocumentId INT NOT NULL,
ADD FOREIGN KEY (DocumentId) REFERENCES Documents (DocumentId)
);
Is this what you're after?
I'm designing a database for an online checkout as a project. This is the first database I've made so go easy on me.
Anyways, I have tables "account", "order", "shopping cart" and "sku". Account holds the shipping info, name, phone, email, etc. The primary key is accountNumber. Order holds the billing information. It's foreign key is accountNumber and it's primary key is OrderID. Shopping Cart only has 2 columns-orderID and skuNumber. OrderID is the foreign key and skuNumber is the primary key. SKU has 4 columns-skuNumber, quanity, price, unit, and totalPrice. skuNumber is foreign and primary key.
My question is- the way I currently have this setup only 1 item sku can be ordered at a time. How would you design this differently so that wasn't necessary? I know I could do sku1, sku2, etc., but that seems like it would be against best practices.
Thanks!
Your database isn't normaized by this design, instead of the SKU table you will need two extra tables, becouse you simply need to separate the prodcts details(Name, price, etc...) from the Order details, so you will have to add the following tables:
Products:
Id.
Name.
Price.
...
Another one OrderItems or SKUItems that contains the following columns:
OrderID foreign key to the orders table.
ProductId a foreign key to the products table.
Quantity.
...
I didn't understand what SKU is but if it is some kind of product type, you can add an extra table ProductTypes: Id, name that holds product types, this table contains for example a product type called SKU then you can add a ProductTypeId to your Products, so you could have something like:
ProductTypeId | Name
------------------------
1 SKU
For the Products table:
ID | Name | ProductTypeId
-----------------------------
1 SKU1 1
2 SKU2 1
3 SKU3 1
I'm implementing a price database where multiple sellers sell the same product at different prices. Each product is unique, and is identified by its model_number. Right now I have 1 seller in my price table. I am looking for the best way to build a table that can list multiple prices with each price being a field for a row whose primary key is model_number
The table looks like this:
model_number | seller | price
abc Store1 99.99
This structure works well when there is only 1 seller, as the primary key is model_number and my query to update the price uses on duplicate key update, which only updates the product if the price changes. Furthermore, the primary key is the model_number, but if I have multiple sellers that have the same model_number but with a different price, I believe when updating the table, the database will get confused as there will be duplicates of model_number.
Should I give each seller a unique key for their product? for instance, should seller_1 have a primary key of seller_1 and keep that value the same so the database knows to update if the price field of seller_1 changes.
Thanks
You can make the model_number and the seller primary key. That way you can have several tuples with the same model_number as long as the sellers differ.
model_number | seller | price
abc Store1 99.99
abc Store2 9.99
Is this what you want?
Three tables:
Seller, Seller_Product, Product
Each row of the linking table Seller_Product contains two Foreign Keys to corresponding Seller and Product row. The price is an attribute of the linking between a Seller and a Product. So it becomes a field in the linking table.
Seller: ID, Name, ...
Seller_Product: Seller_ID, Product_ID, price, availability, ...
Product: ID, Name, model_number, manufacturer, ...
This is a common table design for N:M Relationship.
So far your table structure looks good, all you would need to do is add the seller column as part of the primary key of the table. What you have here is a Many-to-Many relationship between products and sellers. That many to many relationship has some additional data, which is the price.
I think what you mean by "give each seller a unique key for their product" is actually what they call a surrogate key. You could give the table a primary key column so it looks like this
id | model_number | seller | price
Where id is the primary key of the table, and model_number and seller is are secondary keys and foreign keys, but that is really up to you, either way will work just fine.
Many to many relationships are what you are dealing with here.
You want three tables for this solution:
Products:
A table with products information, including a unique auto-incrementing id.
Sellers:
A table with sellers information, including a unique auto-incrementing id.
Products_To_Sellers:
A table with product_id, and seller_id ( INDEX these columns individually )
With this approach, you can JOIN the tables to view sellers products, but you ultimately have a SCHEMA that scales infinitely
[seller] [product_seller] [product]
-id seller.id -id -price
product.id
if you need products to be unique, you'll need another table:
[seller] [product_seller] [product] [seller_price]
-id seller.id -id product.id
product.id seller.id
-price
I have two tables and I need to establish a 1-many relation among them, as an example:
1 Customer can have many Order(s).
What is a good way to create keys on the Order table such that there can be many rows in Orders, relating to one/same Customer details? i.e can I have cases when there are 2 rows with same CustomerID inserted into Order (1-many relation on CustomerID foreign key)
Assume
Customer table has columns:
CustomerID (key)
Name
OtherColumns
Order:
<IsaKeyNeeded>
customerID (foreign key)
OrderName
Another question I have is does 'Order' need to have it's own key?
You have it set up correctly ... the Order table should have a foreign key to the Customer table. This establishes the relationship of one customer to many orders. Just do not make the CustomerID a unique key.
To answer your other question ... yes, the Order table should have it's own primary key.