U
U
Urukhayy2015-12-21 11:27:31
MySQL
Urukhayy, 2015-12-21 11:27:31

What is the best way to store such data in a table?

Example:
While using the application, the user picks fruit. There are 18 types of fruits in total. In the entire history of using the application, he may not collect any types of fruit, or he may not collect a single fruit at all.
When a user wants to sell some type of fruit, there must be at least a certain number of fruits of this type, say - 100.
What is the best way to store data for such a task in a table?
1) In the form of 19 columns (1 for the user ID and 18 for fruit types), and if the user picks only one fruit of one type, the remaining columns will be zero and exist in vain (or maybe he will change his mind about picking fruit at all?).
2) As 2 columns (1 for user ID and 1 for fruit species ID), and multiple rows. Each line will mean the presence of a certain type of fruit.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
Cat Anton, 2015-12-21
@Urukhayy

Table with three columns: User ID, Fruit ID, Quantity.

A
Alexey Ukolov, 2015-12-21
@alexey-m-ukolov

2) As 2 columns (1 for user ID and 1 for fruit species ID), and multiple rows. Each line will mean the presence of a certain type of fruit.
This option is more flexible.
The first option will allow you to very efficiently select data about all the user's fruits at once, but when adding a new type of fruit, you will need to edit the table. And you most likely will not notice the difference in the speed of these options.
Therefore, the second option is preferable.
PS It turns out that I inattentively read the second option. Of course, it needs to be replaced with what 27cm offers .

A
Alexander Taratin, 2015-12-21
@Taraflex

I am for the first option. It will be most productive, though not correct if you read smart books.
Let it go. 18 ints won't take up much space.
Adding a new column in the future is not such a problem if you are sculpting new fruits, but the logic of working with the database itself will be greatly simplified.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question