Flags, performance and maintenance of PHP and MySQL

Let's say I have a MySQL database with a user table as follows:

|ID| Name| email       |...
|1 | John| john@doe.com|...

I will have a php page where the user will select using checkboxes, between the various options in two independent areas of interest. In this example, we can say: favorite fruits and cars.

Fruits: apple, lemon, pear, banana. Cars: Ferrari, Mercedes, Volvo, BMW.

Now I have two options:

1.) Creating a column for each fruit and each car associated with each user, something like this:

|ID| Name| email       |apple | lemon | pear | banana | ferrari | mercedes | volvo | BMW |
|1 | John| john@doe.com| 1    | 1     | 0    | 1      | 0       | 1        |  0    | 1   |

2) Create two new tables: fruits and cars.

|ID| Name  |
| 1| Apple |
| 2| Lemon |
| 3| Pear  |
| 4| Banana|

|ID| Name     |
| 1| Ferrari  |
| 2| Mercedes |
| 3| Volvo    |
| 4| BMW      |

Then create a user_fruit relational table and another called user_cars. For instance:

|ID_user| ID_fruit |
| 1     | 1        |
| 1     | 2        |
| 1     | 4        |

(Note that fruit id 3, Pear is not displayed because it is not selected → 0)

|ID_user| ID_car   |
| 1     | 2        |
| 1     | 4        |

: , ? , , tinyint. , , , , php, .

, - .

- , , , , .

!

+4
4

. . , "" .

+5

"-10 ",

|ID    | Name     |
| 1001 | Ferrari  |
| 1002 | Mercedes |
| 1003 | Volvo    |
| 1004 | BMW      |
| 2001 | Apple    |
| 2002 | Lemon    |
| 2003 | Pear     |
| 2004 | Banana   |

"", 2xxx - , 1xxx - .. , .

|ID    | Family  |
| 1000 | Cars    |
| 2000 | Fruits  |

JOIN... JOIN... JOIN, "". , ID (SMALLINT TINYINT).

, , .

+1

- . , .

0

|ID| Name| email       |apple | lemon | pear | banana | ferrari | mercedes | volvo | BMW |
|1 | John| john@doe.com| 1    | 1     | 0    | 1      | 0       | 1        |  0    | 1   |

, , . , , - , /, . .

, .

0

Source: https://habr.com/ru/post/1655104/


All Articles