Which of the following is better for storing MySQL Data with tinyint and varchar?

Source: Internet
Author: User
After the tinyint server determines the number, it displays the corresponding content. varchar directly stores the content to be displayed. For example, it stores the XXX category. For example, it stores the corresponding content after the tinyint server determines the number.
Varchar directly stores the content to be displayed.

Such as the classification of stored XXX
Such as male/female

(Answers for beginners)

Reply content:

The tinyint server determines the number and then displays the corresponding content.
Varchar directly stores the content to be displayed.

Such as the classification of stored XXX
Such as male/female

(Answers for beginners)

According to the "rules", the database design should conform to three paradigms, that is, to create a special table to store the "category ". However, in actual situations, it would be better to directly store the "content to be displayed" if the "category is basically not changed", so that you do not need to query another table when getting data, reduce one query. Of course, the disadvantage is that the occupied space is too large, but it is wise to take the space for time.

For PHP, there are numbers in the database. It is also a good practice to get the "category" name through an array when getting the numbers.

In addition, when the number of categories is only a few, for example, "male and female", it is also common to directly specify 1 as male and 0 as female, but remember to add comments/descriptions to the field.

Finally, a small detail is provided. strings with fixed lengths are stored in char and stored in varchar, which is faster in reading.

Use enum

We recommend that you use enum in MySQL,
It represents the string form of varchar,
But actually only occupies one tinyint (maybe, I'm not sure. I remember that enum seems to occupy 2 bytes of space.

Char, which cannot be converted.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.