Relational database with multiple preferences and categories - php

I'm trying to wrap my head around designing my database which will store one or more preferences for many categories for each user. So in other words, each user can select one or more options from the Colors category, one or more options from the Shapes category, and so on.
My initial thought was to first have a User table with generic user information. Next, there would be a table to store all the different categories as so:
CATEGORY_ID | CATEGORY_VALUE
--------------------------------
1 | Colors
2 | Shapes
3 | Sizes
I'd separate each Category into it's own table (Colors for example):
OPTION_ID | OPTION_VALUE
------------------------------
1 | Red
2 | Blue
3 | Green
Finally, I would have a User Preferences table:
USER_ID | CATEGORY_ID | OPTION_ID
----------------------------------------
1 | 1 | 2
1 | 1 | 3
1 | 3 | 2
2 | 1 | 3
Am I on the right track here or is there a better/more efficient way to designing this. I will be setting up a search results page which will allow visitors to filter through these different categories.
Thanks!

Related

MySQL schema EAV one column for different value or multiple columns

I am running a project on MySQL 8 and PHP 7.2 on Laravel 6 framework
I have a EAV situation as this:
page table:
id | project_id | page
1 | 1 | about-us
2 | 1 | home
3 | 2 | home
page_attributes table:
id | page_id | attribute | type
1 | 1 | speed | float
2 | 1 | title | text
attribute_values table:
id | page_attribute_id | value
1 | 1 | 5.22
2 | 2 | page title is this
Now my question is that it's better in attribute_values table to have just one column for both float and text types like i did or to have two separated columns one for value_text and another value_float?
And if I had more types like int, boolean,... what about then? which way is beter?
UPDATE:
#strawberry mentioned a way that I didn't thing of it's considered if it's better to have multiple tables for each data type
So it's now like this:
having one column for all data types
having multiple columns for each data types
having multiple tables for each data types
Note: I am dealing with about 50 Million records

multiple fields in mysql

Ok, I hope I can make this question as clear as possible.
I have the following table:
table: phones
phone_id | name | ... ... ... | manufacturer_code
-----------------------------------------------------
1 | samsung | | 001
2 | apple | | 002
3 | htc | | 003
and so on...
I have A LOT of columns (... ... ... means at least 40 different columns like width, height, depth, color, has_bluetooth, ... stuff like that).
Now I want to do this differently, but I have NO IDEA how to start.
How I THINK to do it? (correct me if i'm wrong!)
1) Store the field names in a different table
table: phone_fields
field_id | name
--------------------------------
1 | name
2 | width
3 | weight
4 | depth
... | ...
41 | manufacturer_code
2) Connect the fields with the data for each phone in another table
table phones
row_id | phone_id | field_id | value
------------------------------------------------------
1 | 1 | 1 | samsung
2 | 1 | 2 | 10,50 cm
3 | 1 | 3 | 1 kg
... (and so on).
I want this to be searchable/filterable, I have a filter now which filters on brand, color, pricerange, ... and this works fine.
How should you guys do it? Or does anyone have a useful link/tutorial about this? English is not my native language so I don't know exactly how to search for it.
Thanks in advance!
Edit: why am I doing this? I want to be able to add extra fields through my admin-panel if necessary (and not via phpmyadmin or something like that).
If your request always returns the entire set of fields, then leave it in one table
If not, then you can leave only the most important fields in one table, and in the other table place the remaining attributes and link it via foreign key

Laravel: how to make a key of a column name?

I'm bending my mind for some time now over this problem. Could someone please help me?
I have two tables: products and product_attributes. The product has all basic product information and product_attributes has all specific information for products on different categories. It's much like the magenta attribute system. this table has 4 columns: id, product_id, attribute_name, attribute_value.
Now let's say a product has 2 attributes:
------------------------------------------------------
| id | product_id | attribute_name | attribute_value |
------------------------------------------------------
| 1 | 123 | length | 123cm |
------------------------------------------------------
| 2 | 123 | material | Denim |
------------------------------------------------------
| 3 | 123 | season | Summer |
------------------------------------------------------
Now if I set up the eloquent relationships and query a product, I get a product object with all three attributes. So far this is what I wanted. But now in a blade template I would like to be able to do something like this:
$product->attribute->length
Is this even possible or do I need to achieve these kind of things with a total different approach (like creating different tables for different product types/categories)?
Thanks in advance!
length is a tuple value not an attribute you need
$product->attribute->where('attribute_name', 'length')
or
$product->attribute->whereAttributeName('length')

Categorize and structure database

I've been pondering the best way to approach the structure of this database. What I have so far is:
table category
cat_id (PK)
title
table sub_cat
sc_id (PK)
cat_id (FK)
title
This is all fine since I could use it as follows:
Car -> Mazda
computer hardware -> CPU
clothing -> Male :: Pants
Car Parts -> Mazda :: Spoilers
...
The problem is how could I best structure the database to further utilize the best of each category. The cars data will require a lot of fields suitable for cars alone which another category may require something completely different.
It should be said there will be a lot of categories.
Possible is to create a table 'Details', built something like this
= ItemId === DetailId === DetailValue =
| Somecar1 | 1 | Blue |
| Somecar2 | 2 | Four |
| Somecar2 | 1 | Pink |
| Somecar2 | 2 | Two |
=======================================
Where DetailId refers to something like this, detailreference;
= CategoryId === DetailId === DetailString =
| 1 | 1 | Color |
| 1 | 2 | Doors |
============================================
If you want to get the info of Somecar1, you should query the Details table for the data, then query detailreference to get the type of detail stored inside of a certain number, for a certain category.

Mapping products with similar names

I am trying to find a good solution to accomplish the following:
I have a table which includes the name of various products, such as:
"Tide - Original Scent".
In addition, I also have the amount in there for e.g. 50 fl oz.
The problem I have right now is, that the product not only comes in containers of 50 fl oz but also in different sizes such as 75 fl oz and 100 fl oz. For each of these I have new rows in the product table:
id| productName | amount | unit
1 |"Tide - Original Scent" | 50 | "fl oz"
2 |"Tide - Original Scent" | 75 | "fl oz"
3 |"Tide - Original Scent" | 100 | "fl oz"
Now I have a web interface to perform a search on this table and whenever I search for "tide" I get all three rows - which is supposed to be like that of course. However I would like a different behavior and this is where I need your help:
Instead of returning all three rows I would like one row only. I would then need to be able to process it in php so that if the user clicks on "Tide - Original Scent" that the user is then prompted to select the size.
To add even more complexity to the task:
I also have products in the same table named:
"Tide - Mountain Spring".
In this case, it would be great to have some relations set up so I know that "Tide - Original Scent" is linked with "Tide - Mountain Spring". Within php I would then like to not only give the user the choice of selecting the size but also the (in this case) scent.
What would your recommendation be on how I can accomplish this (not the php part)?
How would your database look like?
Do I need to create a table where I map these products? How would this look like if you would create this :)
2 possibilities:
Don't store the sizes in that table - along with the other specific information. Move that to another table. Denormalize your structure.
or
Query but group by the name. For the size column, do a count(amount). If it's more than one, you can then populate a drop down with choices. This is good temporary fix.
SELECT productName, count(amount) AS numOfChoices FROM YOUR_TABLE
WHERE LOWER(productName) LIKE 'tide%'
GROUP BY productName
then after the choice is made
SELECT id, amount FROM YOUR_TABLE
WHERE id = "$selectedId"
to present a choice of sizes that will pin point which one.
I would personally setup my tables like this..
Products Table:
ID| Product ID | Product Name | Description
1 | 0404888282 | Tide - Original Scent | Smells Good
Quantity Table:
ID| Product ID | Size| Price | Quantity
1 | 0404888282 | 50 | 4.99 | 23
2 | 0404888282 | 75 | 5.99 | 120
3 | 0404888282 | 100 | 7.99 | 10
This structure you have a table for each unique item, another for the sizes and quantity of each size. Keeps the structure clean and easy to understand.
"Instead of returning all three rows I would like one row only."
SELECT DISTINCT productName FROM YOUR_TABLE WHERE LOWER(productName) LIKE 'tide%'
And you'll need a functional index on LOWER(productName) for good performance. Alternatively a case-insensitive collation sequence could be used on DBMSes that support that (e.g. MS SQL Server).
"I would then need to be able to process it in php so that if the user clicks on "Tide - Original Scent" that the user is then prompted to select the size."
SELECT amount FROM YOUR_TABLE WHERE productName = whatever_user_selected
"To add even more complexity to the task: I also have products in the same table named:
"Tide - Mountain Spring"."
The query above will also return that.
What you can do is :
$Query = 'SELECT productName, amount, unit FROM products';
$Data = array();
while($Assoc = mysql_fetch_assoc($Query)){
if(!isset($Data[$Assoc['productName']])){
$Data[$Assoc['productName']] = array();
}
$Size = $Assoc['amount'].' '$Assoc['unit'];
$Data[$Assoc['productName']][] = $Size;
}
// Now what you can do is :
foreach($Data as $ProductName => $Amount){
echo $ProductName.' has :<br />';
if(count($Amount) > 0){
foreach($Amount as $Key => $Value){
echo $Value.'<br />';
}
} else {
echo 'Nothing<br />';
}
}
This however doesn't solve the problem on MySQL's side. IT will work in PHP wihtout problem. It's not beautiful but it's working.
For the first problem, you could create two tables:
products - this is where you store all the information about a product except for the specifics such as different sizes, colors, etc.
attributes - you would link to the product and for each attribute you specify a value
products
id | description
---+------------
1 | crazy shirt
2 | clown shoe
attributes
product | name | value
--------+-------+-------
1 | color | green
1 | color | blue
1 | size | medium
2 | size | large
You can optimize the attributes table further by creating a attribute_names table (and even an attribute_values table).
For your second problem, you could either:
create a related product id column inside the products table; this would limit you to only one related product per product.
create a related product table in which you store combinations between two products.
related_products
product_id | related_product_id
-----------+-------------------
1 | 2
1 | 3
That would create a relationship between product 1 and products 2 and 3.
Hope this helps.
Create a foreign key to sublabels, and order them with a counter. This will look like.
Product Table:
Product ID (key)
Brand ID
Price
Brand Table:
Brand ID (key)
Brand
Sublable Table:
ID
Product ID
Order Index
Value
Size Table:
Size ID
Value
Unit
ProductSize Table:
Size ID
Product ID
Then, you'll divide into subcategories using subsequent sublabels.
Products
10 | 6 | 1.99
11 | 6 | 2.99
12 | 6 | 3.99
13 | 6 | 1.99
14 | 6 | 2.99
15 | 6 | 3.99
Brand
6 | Tide
Sublabel
30 | 10 | 1 | Original Scent
30 | 11 | 1 | Original Scent
30 | 12 | 1 | Original Scent
30 | 13 | 1 | Mountain Spring
30 | 14 | 1 | Mountain Spring
30 | 15 | 1 | Mountain Spring
Size Table
1 | 50 | fl.oz.
2 | 75 | fl.oz.
3 | 100 | fl.oz.
Product Size Table
1 | 10
1 | 13
2 | 11
2 | 14
3 | 12
3 | 15

Categories