DWQA QuestionsCategory: DatabaseWhich is better for MySQL's limited number of data to be stored in tinyint or varchar?
ReSur asked 1 week 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 1 week 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 1 week 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 1 week ago

Using enum

Liu Xiang answered 1 week 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 1 week ago

Char, this conversion is nothing.