I'm building products database where a product may have multiple variants such as Size, Color, Shape, etc. for a single product. Tricky part I have trouble with is structure of the database allowing me to link the dependencies e.g. "Red" "M" "T-shirt" or "Blue" "XS" "Trousers" which in this case are variants color and size together. I want those variants to be generic so, I'm avoiding sizes, shapes, colors tables etc. Some items may not even have any variant at all or have just one e.g. just the color.
Ideally the shape would look like so
products
id
name
1
T-shirt
2
Trousers
variants
id
name
1
Color
2
Size
variant_values
id
variant_id
name
1
1
Blue
2
1
Red
3
2
XS
4
2
S
5
2
M
product_variants
I probably don’t even need variant_id here if I provide variant_value_id which already has relationship with a specific variant.
id
product_id
variant_id
variant_value_id
1
1
1
1
The product_variants structure allows me to have "Blue T-shirt" record but how do I have those variant_value_ids work together so I could store a Blue M T-shirt or Red XS Trousers etc?
Here's my best attempt
I have inspired my model from those threads...
Modeling Product Variants
Schema design for products with multiple variants/attributes?
...but could not find an answer that would apply to my case where I could combine the variants together.
Related
Is this possible using excel formulas? To find keyword and number then match and color the highest number for that specific keyword, e.g. below:
this is the list Cell A keyword and B numbers
shoes 9
shoes 5
shoes 3
furniture 2
furniture 4
furniture 5
beauty 6
beauty 8
health 35
health 4
health 2
grocery 3
grocery 2
computers 9
computers 7
laptop 2
laptop 11
laptop 2
laptop 6
pets 9
pets 3
books 5
books 5
shoes 9 Highlight this number
shoes 5
shoes 3
furniture 2
furniture 4
furniture 5 Highlight this number
beauty 6
beauty 8 Highlight this number
health 35 Highlight this number
health 4
health 2
grocery 3 Highlight this number
grocery 2
computers 9 Highlight this number
computers 7
laptop 2
laptop 11 Highlight this number
laptop 2
laptop 6
pets 9 Highlight this number
pets 3
books 5 ignore if its equal
books 5
You can use conditional formatting, choosing "Use a formula..." and use a formula such as =b1=maxifs($B$1:$B$100,$A$1:$A$100,a1). Be mindful of absolute vs. relative reference to ensure that you're tracking the right ranges.
In particular when tagged vba you should be showing what you have tried. macros Usage guide specifically states "DO NOT USE for VBA / MS-Office languages" and excel wiki states "Questions tagged with excel should be version-agnostic.". However, with a formula is possible in versions earlier than those with MAXIFS (ie not: Excel for Office 365 Excel for Office 365 for Mac Excel 2016 Excel 2016 for Mac Excel Online Excel for iPad Excel for iPhone Excel for Android tablets Excel for Android phones Excel Mobile), if in a more long-winded way:
Assuming you have 11 in B18. Add a column (say I) and populate I1 with 0 and enough of it from I2 downwards with:
=IF(A1<>A2,I1+1,I1)
copied down to sort your data on ColumnI Smallest to Largest then by ColumnB Largest to Smallest (to preserve the order of the values in ColumnA).
Then select B2 down to as far as required, clear any existing CF rules from it and HOME > Styles - Conditional Formatting, New Rule..., Use a formula to determine which cells to format and Format values where this formula is true::
=AND(A1<>A2,B2<>B3)
Format..., select choice of formatting, OK.
The above should not, as specified, highlight the values for books though if working I suspect #nutsch's current answer might.
Sorry, I forgot to adjust my guess for what was where, once I realised a header row would make things easier.
This does though stil have a problem, in that text that changes from one row to the next but shares the same quantity, one row to the next, will not trigger highlighting - a more complex formula may be required.
based on #pnuts idea, found a simpler way to do it.
Sort Z to A of B row, then sort column A, A to Z, with expand the selection for both
next write a formula to highlight duplicates excluding the first one from column A and drag down the formula, it higlights all the correct ones.
thank you
My colleague and I are learning more about database design. We found a situation where we don't know what the next step is. I like to think of it of having 3 combo boxes. I choose the first 2 values and the last one is filtered accordingly to the first 2. Imagine we have 3 tables: Color Table, Material Table, Size Table.
Below is Color Table Design:
ID | Color_Name | Material_ID | Size_ID
In order to choose a Color, it depends on filtering by Material_ID and Size_ID. We have over 600 Colors to choose from. We have 6 different Materials. Lets say Color Red can be used by 4 Materials. So in the Color Table you would have at least 4 records for Red. So this table could technically have a max of 600 * 6 * (# of sizes).
The problem with this is that we would have to enter in all those records. The Color Table would be a static table(very rare we would enter in more colors). Would it be best practice to enter in every combination possible in this table?
Or do we use a matrix to find out every possible combination? I would assume the matrix would be a table, but not sure how you would create a matrix table that compares more than 2 fields(would have to create multidimensional tables).
We would like to follow best practice for designing the database which would help with maintenance. We are open to all suggestions/ideas on how this is handled in the real world. Thank you for your time!
Your Color table is poorly designed and in need of normalization. Every color should be recorded only once:
Color (ID PK, Color_Name)
And then the combinations of materials, sizes and colors can be represented as a ternary relation:
ColorMaterialSize (Color_ID, Material_ID, Size_ID)
Take some time to think about this one. Is any combination of one or two columns unique/identifying, or are all combinations of the three columns valid? Define your primary key on the smallest unique set of columns.
You can then select colors by joining and filtering:
SELECT c.ID, c.Color_Name
FROM Color AS c
INNER JOIN ColorMaterialSize AS cms ON c.ID = cms.Color_ID
WHERE cms.Material_ID = 1 AND cms.Size_ID = 2
I wanted to make sure the design for a particular part is correct or not.
The scenario is:
A pizza can be customized by its size, toppings, crust etc. but the prize of toppings will depend on the size; like when size 7" is selected the prize for each topping will be $1 and for size 14" the prize will be $2 (or some other customization can be dependent on some other one).
One of the customization can be available only for a particular size; like when size 7" is selected then only another customization will be displayed.
(This scenario is just an example and there are many more things other than pizza which will have customization as it is a food ordering app )
Currently for this problem I've designed 3 tables:
Dishes(d_id,name,desc,base_price)
Customization(c_id,d_id(foreign key,name)
options(o_id,c_id(foreign key),parent_id,name,price)
The customization table will hold the name of customization like size, crust, toppings etc.
And the options table will hold the contents of each customization.
The parent_id in options will refer the options table because toppings price will be dependent on the size selected options.
eg:
1) dishes
id name
1 pizza
2) customization
c_id d_id name
1 1 size
2 1 toppings
3) options
o_id c_id parent_id name price
1 1 1 7" 5$
2 1 2 14" 10$
3 2 1 abc 1$
4 2 2 abc 2$
Is there any better way of representing this in the db.
Please guys revert back as soon as possible and please suggest any other changes or functionality I should include in the design(other than this part too).
It will be great if any helpful links are provided.
Thank you.
Is parent_id in options table is the foreign key of d_id in dishes table?
I am implementing a voting feature to allow users to vote for their favourite images. They are able to vote for only 3 images. Nothing more or less. Therefore, I am using checkboxes to do validation for it. I need to store these votes in my database.
Here is what i have so far :
|voteID | name| emailAddress| ICNo |imageID
(where imageID is a foreign key to the Images table)
I'm still learning about database systems and I feel like this isn't a good database design considering some of the fields like email address and IC Number have to be repeated.
For example,
|voteID | name| emailAddress | ICNo | imageID
1 BG email#example.com G822A28A 10
2 BG email#example.com G822A28A 11
3 BG email#example.com G822A28A 12
4 MO email2#example.com G111283Z 10
You have three "things" in your system - images, people, and votes.
An image can have multiple votes (from different people), and a person can have multiple votes (for different images).
One way to represent this in a diagram is as follows:
So you store information about a person in one place (the Person table), about Images in one place (the Images table), and Votes in one place. The "chicken feet" relationships between them show that one person can have many votes, and one image can have many votes. ("Many" meaning "more than one").
Say I have two question types: Multiple Choice and Range. A Range question allows users to answer by specifying a range of values in their answer (1-10 or 2-4 for example).
I inherited a database where the answers to these question types are stored in the same table which is structured like so:
Answers
-------
Id
QuestionId
choice
range_from
range_to
This results in data like below:
1 1 null 1 10
2 1 null 2 4
3 2 Pants null null
4 2 Hat null null
Does it make sense to include columns from every answer type in the answer table? Or should they be broken out into separate tables?
This is a very slimmed-down version of my real database. In reality there are about 8 question types, so with every answer there are several columns that are left unused.
Does it make sense to include columns from every answer type in the answer table?
This is "all classess in the same table" strategy for implementing inheritance, which is suitable for small number of classes. As the number of classes grows, you might consider one of the other strategies. There is no predefined "cut-off point" for that - you'll have to measure and decide for yourself.
The alternative would be an EAV-like system as proposed by blotto, but that would shift the enforcement of data consistency away from the DBMS. This is a valid solution if you don't know the structure of data at design-time and want to avoid DML at run-time, but if you do know the structire of data at design-time better stick with inheritance.
You could have a single field that represents the 'type' of question, that seems best suited in the Question table ( not the Answer table). For example:
question_type ENUM('choice', 'range', 'type_3', 'type_4'..)
Then make a one-to-many link ( a join table ) that represents the Question-to-Answers relationship
AnswerId (pk) | QuestionId (fk)
1 1
2 1
3 2
4 2
Finally, your Answer table is a collection of values for each Answer . It can designate each record more specifically by having its own ENUM.
answer_type ENUM('low_range', 'high_range', 'choice', etc)
Id (pk)| AnswerId (fk) | Type | Value
1 1 low_range 1
2 1 high_range 10
3 2 low_range 2
4 2 high_range 4
5 3 choice Pants
6 4 choice Hat
This is much more scalable, and basically pivots the fields in your previous table to values in the answers table. So you can always add new 'Type's both for questions an answers without adding new fields to the schema.