In-depth understanding of four common and important mysql data types-MySQL

Source: Internet
Author: User
To achieve this goal, we need to fully understand the four common and important data types of mysql.

Next, let's take a look at the important data types of mysql.

1. the numeric type can be divided into integer and floating point decimal types.

The so-called "decimal" refers to decimal and numeric. they are of the same type. Strictly speaking, it is not a numeric type, because they are actually stored as strings, and each numeric value (including the decimal point) occupies a byte storage space, therefore, this type consumes a lot of storage space, but its advantage is that its value will not lose the floating point computing accuracy, it is more suitable for some high computing accuracy requirements, such as price calculation. Float depends on the type of precision, which can be float or double. Their advantages are decimal precision. FLOAT can represent very small values, which can be a minimum value of about 1.17E-38 (0.000... 0117, 37 after the decimal point), can express a smaller number, the minimum number can be about 2.22E-308 (0.000... 0222, decimal point and decimal point followed by 307 zeros. Float and double are 4-byte buckets and 8-byte buckets respectively.

For integer types, there are many different types of integers in MySQL. in the design database table, we can have a byte TINYINT or 8-byte BIGINT, so we should consider which type to use too much, to obtain the minimum storage space without losing any accuracy value.

TINYINT, SMALLINT, MEDIUMINT, INT and BIGINT are 1-byte, 2-byte, 3-byte, 4-byte, and 8-byte. For unsigned integers, the maximum integers of these types are 255, 65535, 16777215,4294967295 18446744073709551615. If we need to save the user's age, TINYINT is enough; if it is an auto-increment ID, we should use MEDIUMINT instead of INT, INT is too big. Many data tables do not reach the MEDIUMINT range.

2. the date and time types entered by date and time are relatively simple.

Such as DATE, TIME, DATETIME, TIMESTAMP, and YEAR. If we only need to care about the Date, but there is no minute or second, we should use Date instead of DATETIME, but DATETIME is the most commonly used one. everything is designed as needed

3. do not assume that the character type is only CHAR

The difference between CHAR and VARCHAR is that CHAR is of a fixed length. If you define a field CHAR (10), then no matter how many bytes of data, this will require 10 bytes of space; for 18-bit ID card numbers, char (18 ),

VARCHAR is variable length. if the value of a field has different lengths, we should use VARCHAR.

4. enumeration and set type enumeration (ENUM)

You can select a maximum of 65,535 different strings.

This may depend on the number of values in one or two bytes of the enumeration type. SET type. a SET can contain up to 64 different members. you can select zero or multiple members. the number of SET members is determined. For example, in SQLServer, you can use the BIT type to indicate gender (male/female), but in MySQL, BIT values are different in different database versions, TINTINT is sometimes a waste. you can use ENUM ('male', 'female ') to save a lot of space.

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.