How to store product quantity in database - mysql

I have database with three tables
product (productID,productName,productDescription)
size (sizeID , sizeName) eg. s ,m ,l ,8,10,12,37,35..etc
ProductDetails (ProductDetailsID, productID(FK) , sizeID (FK))
I want to store the quantity of each product and it size for example there are 5 in stock for product 1 of size s, 10 in stock for product 4 size 37 ..etc
In which table should I place the quantity ?

You can store it in ProductDetails only as the quantity is dependent upon the product and its size both.

You should use another table.
It could be called ProductSizeQuantity(id, ProductId, sizeID, Quantity)

you have
product (productID,productName,productDescription)
size (sizeID , sizeName)
ProductDetails (ProductDetailsID, productID(FK) , sizeID (FK))
in my opinion you need:
product (productID[PK], productName, productDescription)
size (size[PK])
ProductDetails (productID[PK] , size[PK], quantity)
notes:
for size it is not required sizeid + sizeName, but only one column with size name
quantity is in product details table, in this table id your PK is productId+Size you'll have one tuple for each for each size of the product

You can do something like this.
Product table :
id
name
description
Size table :
id
product_id
size
Quantity table :
id
size_id
stock
Fetch product first, fetch sizes based on product and fetch its Quantity based on size id.

Related

discount entity for ERD- onine shopping

I'm currently making an ER diagram for my product model. But I'm confused that the DISCOUNT needs an entity to connect with the product module or it can just be an attributes of the product?
my current product module:
the
PRODUCT
PRODUCT_ID(PK)
PRODUCT_NAME
PRODUCT_PRICE
PRODUCT_TYPE
PRODUCT_COLOUR
PRODUCT_SIZE
STOCK_UNIT (FK)
this is the entity i try to make for DISCOUNT
PRODUCT_ID (PK,FK)
ADMIN_ID(FK)
DISCOUNT_DATE
DISCOUNTED_PRICE
In my scenario, some of the specific product prices can be discounted with value/ per centage. But I am confused that do i need to put the discount value/percentage as an attribute or an entity in the product module because there are only some products that have the discounted price.
I hope that my explanation is clear enough ><
Based on question and comments, I suggest:
Discount Table
--------------
DISCOUNT_ID (PrimaryKey)
PRODUCT_ID (ForeignKey)
IsPERCENT (Bool)
DISCOUNT (Float|Double)
FROM_DATE (DateTime)
TILL_DATE (DateTime)
IsPercent should be True if discount is to calculated using Percentage.
Discount should be value (treated as percent or Currency depending on IsPercent)
Also in your program, validate that Discount value doesn't go above 100 if IsPercent is TRUE

SubTotal Query based on condition of other column

I am creating a spare part management database in Microsoft Access. I have two table which are ItemTable and EntryTable. ItemTable holds information about each item with unique ItemID and EntryTable holds information of each items usage. I need to calculate the total stock left for each items based on the usage.
So as you can see, for the ItemID with 2, i need to calculate the total stock left based on the usage of In or Out of Status field.
If status is In then plus elseif status is Out then minus. Then total the stock of ItemID 2. Thus the total stock for ItemID 2 will be 3. I have figured out by total and group by for the ItemID but i cannot figure out the way to subtotal based on condition from other column. Thank you.
You can do it with conditional aggregation:
select itemid,
sum(iif(status = 'In', 1, -1) * quantity) as total
from entrytable
group by itemid

What is the best DB schema to handle exceptions and prevent redundancy?

There are some products that can be sold in multiple units and the base unit is kilogram. The unit of each product has different weight, for example a Bundle of product A is 20kg but a Bundle of product B is 30kg
The following is my initial design.
product:
id name
product_variation:
id product_id name price
product_variation_unit:
id product_variation_id unit_id
product_variation_unit_weight:
product_variation_unit_id weight
The problem is than most of the variation of a product have the same unit and weight. So here is my solution but I'm not sure it's a good idea:
selectable_unit:
id selectable_id selectable_type unit_id
1 10 product 100
2 20 product_variation 101
selectable_unit_weight:
selectable_unit_id weight
And when I want to get the unit and weight of a variation, first I will check the variation and if there isn't, I will check it's product.
I't a little complicated, can you help me to find a better solution or improve it?
Have a look at the following diagram (Oracle Data Modeler notation). Usually, a price is depending on the SKU (stock keeping unit) and the period; a weight is depending on the SKU (packed product).
An SKU weight is limited by units allowed for its product (foreign key contains 2 columns).
SKU.value is the value in the unit to sell, i.e. kg, pcs, liters etc.

Complete table with another table which has same id

i have two table "product" and "sales". product have "id,Name,price" and sales have "id,pro_id" .
in product table save all products with unique id, and in sales table save all sales product with id of product in "pro_id".
i want a sql which can report a table sales with full data of product info.
for example report have "pri_id,Name,Price" columns.
sample:
product:
id--------Name------price 1---------RAM-------300
2---------CPU-------400
sales:
id---------pro_id 1---------2 2---------1 3---------2
4---------2 5---------1
result of report :
pro_id----Name------price 2---------CPU-------400
1---------RAM-------300 2---------CPU-------400
2---------CPU-------400 1---------RAM-------300
SELECT pro_id,name,price FROM product JOIN sales ON sales.pro_id =
product.id

Database structure for product attributes on orders

I'm making a POS and currently kind of stuck on the order proccess due to the product attributes.
As of now the DB looks something like this:
tbl_products
tbl_attributes (attr id, name, extra price)
tbl_sales ( where i store the time, total price etc)
tbl_salelines ( where i store what items have being order for a certain sale).
Problem is storing the attributes for an item order.
There isn't a fix amount of attributes one item can have. It could go from 1 to 10-15
therefor i don't think making some columns in the tbl_salelines with attr_1 attr_2 etc etc would solve this.
How should i approach this?
Can't you use a foreign key in tbl_attributes to youre tbl_salelines table. So you can have 1 saleline and for that saleline you can add multiple attributes which have a relation to the saleline?
Example:
tbl_saleline 1
Id -> 1
tbl_attributes
**Attribute 1**
Id -> 1
name -> 'attribute 1'
**SaleLine_Id -> 1 (FK->tbl_saleline)**
**Attribute 2**
Id -> 2
name -> 'attribute 2'
**SaleLine_Id -> 1 (FK->tbl_saleline)**