How to make db tables for product filters?

Dump

I want to create product filter for multiple categories. User will select for ex. Mobile phones category via form select element and then there will be showed options for this category which user can choose, for ex. Color, processor,...

And I want to know how best way how to set up database tables for this purpose. My idea is to create three tables:

1. Items
Id | Item_Name   | Cat_Id | Option_1 | Option_2 | ... 
1  | Samsung S20 | 1      | blue     | Intel    | ...
2  | iPhone 5    | 1      | red      | AMD      | ...
3  | T-shirt     | 2      | red      | NULL     | ...

2. Categories:
Id | Cat_Name | 
1  | mobiles  | 
2  | clothes  | 

3. Options
Id | Option    | Values                 |
1  | color     | red, blue, purple, ... |      
2  | processor | Intel, AMD, ...        |

4. Cats_Opts
Id | Cat_id | Option_id |
1  | 1      | 1         | 
2  | 1      | 2         | 

Problem is, that in my structure many fields will be empty bcz for ex. clothes category will not have value for option_2.

Is this good way how to do db structure for this purpose?

bloodyKnuckles

A database is normalized, at least in part, when there's a single column for each type of information. So when you see more than one column having the same information, that part of the database is not "normalized".

Similarly when you see lists of data in a column, such as red, blue, purple in a single column, this is a candidate for normalization. (Not to say there is no place for "un-normalized" data in a relational database.)

1. Items
| Id | Item_Name   | Cat_Id |
| 1  | Samsung S20 | 1      |
| 2  | iPhone 5    | 1      |
| 3  | T-shirt     | 2      |

2. Item_Color_Options
| Item_Id | Color  |
| 1       | blue   |
| 1       | red    |
| 2       | blue   |
| 3       | purple |

3. Item_Processor_Options
| Item_Id | Processor |
| 1       | Intel     |
| 1       | AMD       |
| 2       | Intel     |

Another opportunity for normalization is when a column has the same data repeated in more than one rows, such as:

| Item_Id | Color  |
| 1       | blue   |
| 1       | red    |
| 2       | blue   |
| 3       | purple |

// and

| Item_Id | Processor |
| 1       | Intel     |
| 1       | AMD       |
| 2       | Intel     |

...where blue and Intel are repeated.

In this case, and particularly when the same value or option may be applied to more that one item, the duplicated options can be eliminated using linking tables:

// primary tables

1. Items
| Id | Item_Name   | Cat_Id |
| 1  | Samsung S20 | 1      |
| 2  | iPhone 5    | 1      |
| 3  | T-shirt     | 2      |

2. Categories
| Id | Cat_Name | 
| 1  | mobiles  | 
| 2  | clothes  | 


// option tables

3. Color_Options
| Id | Color       |
| 1  | red         |
| 2  | blue        |
| 3  | purple      |

4. Processor_Options
| Id | Processor       |
| 1  | Intel           |
| 2  | AMC             |


// linking tables

5. Item_Color_Options
| Item_Id | Color_Id |
| 1       | 1        |
| 1       | 2        |
| 2       | 1        |
| 2       | 3        |

6. Item_Processor_Options
| Item_Id | Processor_Id |
| 1       | 1            |
| 2       | 2            |
| 3       | 1            |

4. Cats_Opts
| Id | Cat_id | Option_id |
| 1  | 1      | 1         | 
| 2  | 1      | 2         | 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive