E-Comeerce Database ERD Help - database

This is frying my brain, I really need help!
Here is the thing i want to achieve.
I have a Table name Product.
The product may or may not have up to two Optional field. Example Color and Size.
If the product does not have the optional field, it will have only one row of Price and Quantity, else for each row of optional field, there will be one price and Quantity.
I know this sound Confusing, pardon me. I'm confused too. ):
But i can give you guys fews example below.
So the one million dollar question is, what are the tables and it's field i should create?
[ Product Without Optional Field ]
Price | Quantity
$1.00 | 2
[ Product With One Optional Field ]
Price | Quantity | Size
$1.00 | 2 | Large
$2.00 | 1 | Small
[ Product With Two Optional Field ]
Price | Quantity | Size | Color
$1.00 | 2 | Large | Green
$2.00 | 1 | Small | Blue
I come up with an idea of having Two entity named Product and Optional to have many to many relationship with the Optional Entity to store the field name, example Size and the junction-entity name Product_Optional will store the value, example Large.
However I'm still stuck with the issue of how to bind the Two Optional Field of one product to the same price and quantity! SORRY to be confusing :(

Edit:
Since your options are unknown, you could do something like this
Products
id
product_name
product_description
...
ProductOptions
id
option (size, color, whatever)
value (large, blue, anything)
ProductInventory
id
product_id
product_option_id
quantity
price
Then your records in ProductInventory would look like:
1 | 1 | 1 | 5 | 2.00
1 | 1 | 2 | 3 | 3.00
etc etc
More detailed example, using the table structure above:
Products
1 | Product 1 | Prod 1 Description
2 | Product 2 | Prod 2 Description
3 | Product 3 | Prod 3 Description
ProductOptions
1 | Size | Small
2 | Size | Medium
3 | Size | Large
4 | Color | Blue
5 | Color | Red
6 | Color | Green
7 | Width | 10 Inches
8 | ... (as many as you want)
ProductInventory
1 | 1 | 1 | 5 | 2.00
(says for product 1, size small, there are 5 quantity, and cost is 2.00
2 | 1 | 2 | 17 | 3.00
(says for product 1, size medium, there are 17 quantity, and cost is 3.00
etc

I have run into the same problem, and I think you need more than one table as well. try this:
products
id
product_name
product_price
.....
Product_options
id
product_option_name
product_options_values
link your product_options_values to your product_options using product_options_id as foriegn key. example: product_option 'size' has the product_option_values of 'small', 'medium', 'large'*
id
product_options_id (fk)
options_value
options_value_price
product_options_to_products
this table links your products to your options. here you'd assign different options to different products.
id
product_id
product_options_id

Related

Query or Scan DynamoDB for all rows which have an attribute that matches a list of options

I have a DynamoDB table that has the following format
{
id: "1234" (Primary Key, String)
billNumber: "01" (Sort Key, String)
month: 1 (Number)
product: "Apple" (String)
itemLocation": "Aisle 1" (String)
}
Each product is written to the table separately, so the products can't be written to the same row and they can't update the existing entry to append to the value in the product field.
I want to know how to query or scan this DDB table to find all itemLocations that id "1234" has purchased in the month 1 where the value within the Product field matches list of given products.
I also have a global secondary index on id-month which I can use to find all rows purchased in a month by a user.
Meaning if the table looked like
id | billNumber | month | product | itemLocation
1234 | 01 | 1 | Apple | Aisle 1
1234 | 02 | 1 | Banana | Aisle 2
1234 | 03 | 1 | Cherry | Aisle 3
1234 | 04 | 1 | Coke | Aisle 4
and I wanted to get the itemLocations that id "1234" bought in month "1" where the products were one of {"Apple", "Banana", "Cherry"} I would be returned every row but the Coke row as seen below.
id | billNumber | month | product | itemLocation
1234 | 01 | 1 | Apple | Aisle 1
1234 | 02 | 1 | Banana | Aisle 2
1234 | 03 | 1 | Cherry | Aisle 3
Is this possible in a single query or scan without needing to query for each Product separately? I believe I could solve the problem in my example by doing 3 queries. The same id and month in all 3 queries and a different Product in each query.
The closest I've gotten to seeing something that could work is DDB Condition Expression for this but that doesn't seem to be used for query, instead for CRUD operations.
You can't have a DDB table that looks like that...
With DDB, when using a composite primary key (hash + sort) the combination must be unique.
So you can't have
id | month | product | itemLocation
1234 | 1 | Apple | Aisle 1
1234 | 1 | Banana | Aisle 2
1234 | 1 | Cherry | Aisle 3
1234 | 1 | Coke | Aisle 4
in which 4 records have the same hash key (ID) and sort key(month)
EDIT
Ok so now you've got a valid DDB table...
But you can't query() it for
that id "1234" bought in month "1" where the products were one of {"Apple", "Banana", "Cherry"}
Scan() would work, but very inefficiently...
In order to do this efficiently, you'll want an local secondary index with a sort key of month#product
Now, depending on how many products, you'll need to query 3 times
Query(table, hk='1234', sk='1#Apple')
Query(table, hk='1234', sk='1#Banana')
Query(table, hk='1234', sk='1#Cherry')
Or only once and filter the products server side or client side (note that filtering server side doesn't save any Read Capacity Units)
Query(table, hk='1234', sk BEGINS WITH '1#')
Adding a newer answer here for anyone else who comes across this problem later:
You can transform your list of products into a String Set (type SS) and query your DDB using your id and contains(StringSet, product) as a filter expression.

is this a good practice for a database for products with multiple units,cost price & expiry dates

I a working on a database that is going to have a product with multiple expiration date, multiple cost prices and therefore there will multiple stock entries for the same product, I have made an initial database design for this and I wanted to ask you guys if this is a good practice or not. If not please advise me on how to do it the right way.
This is what I have thought about so far.
Creating 3 tables (1. Product_info - 2.Product_Stock - 3.units)
and below is the detailed structure:
Units Table
--------------------------
id Name
|------|
1 |Piece |
2 |Pack |
3 |Kilos |
Here I will list all the units that I will use as the base product unit.
Product Information Table
-----------------------------------------------------------------------------------------------------------------------
id Name AvgCostPrice AvgPrice AvgPackCostPrice AvgPackPrice totalQuantity BaseUnitID multiplier PackBarcode Barcode
|------|------------|--------|----------------|------------|-------------|----------|----------|------------|--------|
1 |Soda | | | | | 108 | 1 | 12 | 111111 | 111222 |
2 |Water | | | | | 50 | 1 | 6 | 222222 | 222111 |
in the above table the average cost price and selling price for the packs and piece will be calculated from the different stocks I have for the said product.
The multiplier column will be for how much pieces does a product pack hold.
The Total Quantity will hold the sum of different stock quantities I have in the (Product Stock Table) ,Also it will only sum the quantity for base unit of the product.
for example: if the base unit of soda is pack, then it will sum the (PackQTY) Column in (Product Stock Table). and if else it will sum (Quantity) in that table.
Product Stock Table
---------------------------------------------------------------------------------------------------------------------------
id ProdID UnitID CustomBarcode Quantity PackQTY CostPrice Price PackCostPrice PackPrice expDate Enabled
|------|-------|--------------|-------------|-------|----------|-------|--------------|---------|--------------|---------
1 |1 | 1 | | 84 | 7 | 2.0 | 2.4 | 24.0 | 29 | 20/may/2019 | 1
2 |1 | 1 | | 24 | 2 | 1.5 | 1.9 | 18.0 | 23 | 10/aug/2019 | 1
2 |2 | 3 | | 50 | 0 | 3.0 | 5.0 | 0.0 | 0 | 10/Feb/2019 | 1
1.The enabled column will work as a (Boolean) to determine whether to use this stock while selling.
for example: if I wanted to sell a soda Can and I have two Stocks for it. if stock number one is 0 then enable column will be false and therefore it will only subtract the quantity sold from stock number two and use its price and cost price in the (SalesDetails Table)
Custom Barcode Column will be used to separate stocks when having a discount on almost expired stock.
And I also thought of separating the different units for each product stock in (Stock Table)
So, when I want to sell 24 pieces of soda and 3 packs of soda it will choose the oldest stock depending on its (Enabled Column Value = True)and subtract that quantity from it and if it reaches zero then (Enabled column) Value will change to false.
after that it will go again and do the same but this time it will change the value of PackQtY from 7 to 4 and the Quantity Column Value will be calculated through this [ Product_Stock.Quantity= Product_Stock.Quantity - (QtySold * Prodcut_info.Multiplier Column Value) ] which will be 84-(3*12)= 48
And the sales details structure output will be like this:
Sale Details Table
----------------------------------------------------------
id ProdID UnitID Quantity CostPrice Price total CostTotal
|------|-------|-----------|-------------|-------|------|---------|
1 |1 | 1 | 24 | 2.0 | 2.4 | 57.6 | 48.0 |
2 |1 | 2 | 3 | 18.0 | 23.0 | 69.0 | 54.0 |
Product Stock Table (After Selling 24 pieces of Soda and 3 packs of Soda)
---------------------------------------------------------------------------------------------------------------------------
id ProdID UnitID CustomBarcode Quantity PackQTY CostPrice Price PackCostPrice PackPrice expDate Enabled
|------|-------|--------------|-------------|-------|----------|-------|--------------|---------|--------------|---------
1 |1 | 1 | | 48 | 4 | 2.0 | 2.4 | 24.0 | 29 | 20/may/2019 | 1
2 |1 | 1 | | 0 | 0 | 1.5 | 1.9 | 18.0 | 23 | 10/aug/2019 | 0
Sorry if I didn't explain it very well.
Thank you very much in advance.
Firstly, you need to be careful about how you use nouns.
For example: "Price" does not mean the same as "Cost" and "CostPrice" sounds like an oxymoron. I suggest that you restrict your yourself to using either Cost or Price.
Van Ng asks if you have done an Entity Relationship diagram. Well, at the stage that you seem to be at, it is probably unwise to start with an ER diagram because an ER diagram is helpful as a summary of a model that you have already defined - and you are not yet at that stage.
Averages: If you design your database schema correctly then you can calculate data such as averages. You don't need averages as base tables.
I recommend that you consider using the fact-based modeling method called "object-role modeling"(ORM) because you can start with "the facts" before thinking about drawing ER diagrams.
Example:
I used the NORMA ORM tool to create the following example:
First, I read your text, extracted facts and then used the facts to design an object-role model.
Then I used the NORMA tool to generate a "logical view" of the object-role model. (happens in milliseconds)
I did not add everything that you mention but I hope that this will be enough to help you to make progress.
The example contains two artefacts:
1: The logical model that was generated by the NORMA tool.
2: The facts from which the logical model was generated.
[

Geographically display item with highest score by country - Tableau

I have data in the following format:
Type | Country | Item | Value
Category A | Afghanistan | Item 1 | 5
Category A | Afghanistan | Item 2 | 3
Category A | Afghanistan | Item 3 | 1
Category B | Afghanistan | Item 1 | 2
Category B | Afghanistan | Item 2 | 5
Category B | Afghanistan | Item 3 | 1
I'm trying to create a map of country values, such that:
Each country is colored depending on which item received the highest cumulative value (in this case, if Item 1 is red, 2 is blue, and 3 is green, Afghanistan would be colored blue)
The item with the highest cumulative value is displayed when hovering over the country
I know that I could just manually calculate it on my own end, but I want to introduce additional filters to the file so that you could, for example, exclude Category A or B and have it recalculate the top value item.
Thanks in advance for the help!
Here's what worked for me:
Create a Max value calculation using the Fixed syntax: {FIXED [Country] : max([Value])}
Drag that column into the Dimensions area
Place that calculated field on the color marker shelf

Using Data from One Table in Another Table in Access

Hallo StackOverflow Users
I am struggling with transferring values between Access database tables which I will use in a Delphi program to tally election votes and determine the winning candidates. I have a total of six tables. One is my overall table, tblCandidates which identifies each candidate and contains the amount of votes they received from each party, namely the Grade Heads, the Teachers and the Learners. When it comes to the Learners we have four participating grades, namely the grade 8’s, 9’s, 10’s and 11’s, and each grade again has multiple participating classes, namely class A, B, C, etc.
Now, I have set up tables for each grade that contains all the classes in that grade. I named these tables tblGrX with X being the grade represented by 8 through 11. Each one of these tables has two extra fields, namely a field to identify a candidate and a field that will add up all the votes that candidate received from each of the classes in that grade. Lastly I have another table, tblGrTotals with fields Total_GrX (once again with X being the grade), that will contain all the total votes a candidate received from each grade, adding them up in another field for my tblCandidates table to use in its Total_Learners field.
So in short, I want, for example, tblGrTotals to use the value in the field Total of tblGr8 in its Total_Gr8 field, and then tblCandidates to use the value in field Total of tblGrTotals in its Total_Learners field. Is there any way to keep these values updated between tables like cells are updated in Excel the moment a change is made?
Thank you in advance!
You need to rethink your table design. I guess your background is Excel, and your tables are laid out like you would do in Excel sheets, but a relational database works differently.
Think about the objects you are modelling.
Candidates - that's easy. ID, Name, perhaps additional info that belongs to each candidate. But nothing about votes here.
"Groups that are voting" or Parties. Not so trivial, due to the different types of parties. Still I would put them in one table, with Grade and Class only set for Learners, NULL for Heads and Teachers.
e.g.
+----------+------------+-------+-------+
| Party_ID | Party_Type | Grade | Class |
+----------+------------+-------+-------+
| 1 | Head | | |
| 2 | Teacher | | |
| 3 | Learner | 8 | A |
| 4 | Learner | 8 | B |
| 5 | Learner | 8 | C |
| 6 | Learner | 9 | A |
| 7 | Learner | 9 | B |
| 8 | Learner | 10 | A |
+----------+------------+-------+-------+
Votes: they are a Junction Table between Candidates and Parties.
e.g.
+----------+--------------+-----------+
| Party_ID | Candidate_ID | Num_Votes |
+----------+--------------+-----------+
| 1 | 1 | 5 |
| 1 | 2 | 17 |
| 3 | 1 | 2 |
| 3 | 2 | 6 |
| 3 | 3 | 10 |
+----------+--------------+-----------+
Now: if you want to know the votes of Class 8A:
SELECT Candidate_ID, SUM(Num_Votes)
FROM Parties p INNER JOIN Votes v
ON p.Party_ID = v.Party_ID
WHERE p.Party_Type = 'Learner'
AND p.Grade = 8
AND p.Class = 'A'
GROUP BY Candidate_ID
Or of all Grade 8? Simply omit the p.Class criteria.
For the votes per candidate you join Candidates with Votes.
Edit:
for the votes counting differently, this is an attribute of Party_Type.
We don't have a table for them yet, so create one:
+------------+---------------+
| Party_Type | Multiplicator |
+------------+---------------+
| Head | 4 |
| Teacher | 3 |
| Learner | 1 |
+------------+---------------+
and to count all votes:
SELECT c.Candidate_ID, c.Candidate_Name, SUM(v.Num_Votes * t.Multiplicator) AS SumVotes
FROM Parties p
INNER JOIN Votes v ON p.Party_ID = v.Party_ID
INNER JOIN Party_Types t ON p.Party_Type = t.Party_Type
INNER JOIN Candidates c ON v.Candidate_ID = c.Candidate_ID
GROUP BY c.Candidate_ID, c.Candidate_Name
With a design like this, you don't need to keep updating data from one table into another - you calculate it when and how you need it, and it's always current.
The magic of databases. :)

Ingredients for drink new table or string in column?

I am thinking about my application and I want to store data about drinks. Now I am thinking what is best that if I save ingredients just as nvarchar column in table with drink or if I create new table with ingredients and create relationship one to many? I want database to be just read-only and I want to have option filter by ingredients. So what´s best way for windows phone (for performance)? And if the new table would be the better choice, I should use EntitySet, EntityRef, am I right? And I would have for every ingredient new row in table? Let´s say I have 100drinks, in average that every drink has 4ingredients so I have in first table 100rows and in second cca 400rows? Thanks for help
Actually, both solutions proposed are wrong. A drink can have many ingredients and an ingredient can be used in many drinks. Hence, we have a many-to-many relationship here. The proper way to model this is the following (I'm adding data for it to be more understandable):
Ingredients (PK: Id)
+----+--------------------+
| Id | Name |
+----+--------------------+
| 1 | Water |
| 2 | Sugar |
| 3 | Coffe |
| 4 | Virgin Islands Tea |
| 5 | Ice |
+----+--------------------+
Drinks (PK: Id)
+----+-------------+
| Id | Name |
+----+-------------+
| 1 | Black Coffe |
| 2 | Tea |
| 3 | Ice Tea |
+----+-------------+
Drinks_Ingredients (PK: Drink_Id, Ingredient_Id)
+----------+---------------+------------+
| Drink_Id | Ingredient_Id | Proportion |
+----------+---------------+------------+
| 1 | 1 | 70 |
| 1 | 2 | 10 |
| 1 | 3 | 20 |
| 2 | 1 | 90 |
| 2 | 4 | 10 |
| 3 | 1 | 80 |
| 3 | 4 | 10 |
| 3 | 5 | 10 |
+----------+---------------+------------+
I'm adding this Proportion column to show you how to add data that is dependant on the pair of drink-ingredient. Now, if you're worried about the size of the tables it'll be quite small as the only tables that will have the more complex data types (varchars) will be the ingredients and drinks tables, which will have the minimum amount of records possible: one per each drink and one per each ingredient.
If you still have doubts keep looking at the example, you'll get it :)
I would do a table for ingredients with a description and an id, and store the ids in the drink table cause it's the elegant way to do it. For 100 drinks, you won't see a difference for the performance.

Resources