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
Related
i am doing a simple shopping cart for my assignment. currently i am doing the planning for relational databases. here are the part of the requirements:
multiple seller with multiple products
multiple customer
cust can add many items in a cart
a cart (having many items) is saved after a transaction so cust can view them in transaction history
so the cart has cart_id tied to cust_id
if there are 10 transaction, 10 carts will be saved
each cart has many items.
this is the flow (my attempt) -
a cart with cart_id=10001 is create when cust=11 adds 1st item
lets say cust added 5 items in cart
transaction completed. 2 things happen:
cart_id is saved to master_cart table with cust_id
(on my part) a table is created with the name 10001 having prod_id and quantity
my prob is about the list how do i create a table having the name of the cart_id? also, is this the right way?
this is my draft for db.
is this the right way or how do i create table with the name based on other table row values?
You should not create a table for each cart. Dynamic information should be in table contents, not names.
You should have a single cart_items table with cart_id as a column.
CREATE TABLE cart_items (
cart_id INT,
prod_id INT,
quantity INT,
UNIQUE KEY (cart_id, prod_id),
FOREIGN KEY (cart_id) REFERENCES cart (cart_id),
FOREIGN KEY (prod_id) REFERENCES product (prod_id)
);
My database is called 'shopping' and it contains the following tables:
products (product_id, product_name, price, category_id)
product_category (category_id, cagtegory_name)
customers (customer_id, username, pass, fname, lname, email)
orders (order_id, order_date, product_id, customer_id)
Now I want to consider keeping records of each products stock levels.
i.e
product_id = 1
product_name = MICROSOFT Limited Edition Gears 5 Xbox One X, Tekken 7 & Project Cars 2 Bundle
price = 449.99
category_id = 2
stock _id = 1 ( references stock_id in product_stock table ) :-/ ?
I have considered THREE approaches to storing 'stock info' on products.
Approach a) Add a new Column in products table called 'stock_level' simple, and valid since stock level is entirely dependent on its primary key in this situation.
Approach b) Create a new table called 'product_stock' , with the columns 'stock_id, and stock_level'.
The confusion is arising in the logic here.
For some reason, having gone with approach b, I had made the primary key of product_stock table (stock_id) ALSO a foreign key, referencing back to the products table as:
product_stock.stock_id = products.product_id
i.e
product_stock.stock_id 1 REFERENCES BACK TO products.product_id 1
product_stock.stock_id 2 REFERENCES BACK TO products.product_id 2
etc etc
THE THIRD approach I thought about was keep the stock_id key as a Primary Key only, and sever the link back to Products table. Then in products, create a column called stock_id with is a FK referencing stock_id in product_stock table.
Which of the three approaches seems the best ? I for some reason, regret deleting the columns and relations set up which was giving me behavior as discussed in approach b.
The reason I liked approach b was because, if you hover over each stock_id key in product_stock, you can see instantly what product it relates to in the products table ( phpmyadmin ).
thoughts on this folks ?
Refer to question and read.
no need, its all on phpmyadmin and not required in this case.
refer to question.
As far as you explained, each product has only and only one stock value. The simplest way to proceed is to to store that information directly in a products table: option a) in your question.
Using a separated table would be useful if, for example, you were in a situation where you have several stores and you need to keep track of the stock level of each product in each store. You would then create a separated table, with a column to store the id of the product, another to store the id of the store, and a third column to store the level of this particular product in this particular stock.
I'm trying design a model which allows a user to be a buyer and seller with a single account, but some teachers told me that this diagram is wrong because it has redundancy.
I had reviewed the diagram but I haven't found a way to solve this redundancy. In the table orders I need to know who is a buyer, so for this reason I didn't delete this from the table. Some ideas?
The only thing that are "redundant" (not normalized to be exact) in your scheme is this :
You don't need to make a special ID, a composite PK is enough.
-------------------
| ORDERPRODUCT |
-------------------
| PK | PRODUCT_ID |
| PK | ORDER_ID |
-------------------
ADD CONSTRAINT pk
PRIMARY KEY (PRODUCT_ID, ORDER_ID);
On top of what #Blag has said, for Categories, you have 2 fields that might do the same thing: categoryname and description. You already have an identifier with PK_IdCategory, so one of those might be unnecessary
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.
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