DWQA QuestionsCategory: DatabaseWhich is better for MySQL's limited number of data to be stored in tinyint or varchar?
ReSur asked 6 months ago

The tinyint server displays the corresponding content after judging the number
Varchar stores the content to be displayed directly
Classification of XXX
If there is a man/woman
(novice solution)

JohnLui replied 6 months ago

Considering the compatibility of database transplantation, it is the best choice to store 0 and 1 with tinyint. Of course, it should be clearly marked in the database design document.

4 Answers
Best Answer
Juwan answered 6 months ago

According to the “rules”, database design should conform toThree paradigmsWhat we need is to create a new special table to store “categories”. However, in practice, if the category is basically unchanged, it would be better to store the “content to be displayed” directly, so that when data is retrieved, there is no need to query another table and reduce the query once. Of course, the disadvantage is that it takes up a lot of space, but it is wise to trade space for time.
As far as PHP is concerned, there are numbers in the database. It is also a good way to get the name of “category” through an array when taking out the numbers.
In addition, when the number of categories is only a few, such as “men and women”, it is also common to use 1 to represent men and 0 to represent women, but remember to add notes/descriptions to fields.
Lastly, let’s mention a little detail. Fixed length strings are stored in char, which is faster in reading than varchar.

dryoung answered 6 months ago

Using enum

Liu Xiang answered 6 months ago

Recommend enum in MySQL.
It takes the form of varchar strings.
But actually only one tinyint (maybe, I’m not sure). I remember that enum seems to take up 2 bytes of space.

Kyle_Feng answered 6 months ago

Char, this conversion is nothing.