SoFunction
Updated on 2025-04-18

MySQL data types: strings, numbers, and dates detailed explanation

Detailed explanation of MySQL data types: string, number, date

In MySQL, select the appropriate data type for the databaseStorage efficiencyandQuery performanceIt is crucial. MySQL provides three major data types: String, Number, and Date, and Time. Each type has a different subtype to meet different business needs.

1. String data type

String type is used to store text data, mainly divided intoFixed length (CHAR)andVariable length (VARCHAR),as well asLarge text (TEXT and BLOB)

1.1 Fixed and variable length strings

Data Type Storage method Features Applicable scenarios
CHAR(n) Fixed length storage (not enough to fill spaces) Fast access speed and fixed space Suitable for fixed-length data, such as country code, ID number
VARCHAR(n) Variable length storage (allocate space on demand) Save storage space, but may require additional storage overhead when querying Suitable for text with irregular lengths, such as user nicknames, emails

CHAR vs. VARCHAR

  • CHAR(10)Always take up 10 bytes (not enough to fill spaces).
  • VARCHAR(10)Store only actual characters, up to 10 bytes, and an additional 1-2 bytes are used to store length information.

Generally speaking:

  • Short, fixed-length text(such as country code, MD5 hash)CHAR
  • Text with unfixed length(such as name and address)VARCHAR

1.2 TEXT and BLOB

When storing longer text or binary data, useTEXT and BLOBtype:

Data Type Maximum storage size Whether it is case sensitive Applicable scenarios
TINYTEXT 255 bytes ✅Case sensitive Short text, such as tweet content
TEXT 65,535 bytes (64KB) ✅Case sensitive Article content, comments
MEDIUMTEXT 16,777,215 bytes (16MB) ✅Case sensitive Books, log records
LONGTEXT 4GB ✅Case sensitive Extra-long text, such as encyclopedia entries
TINYBLOB 255 bytes ❌Case insensitive Small binary data, such as picture thumbnails
BLOB 64KB ❌Case insensitive Pictures, audio
MEDIUMBLOB 16MB ❌Case insensitive Large media files
LONGBLOB 4GB ❌Case insensitive Extra large files, like movies

TEXT vs. VARCHAR

  • TEXTNot supporteddefault value, also unavailableIndex (only prefix indexing is supported), but can store a lot of text.
  • VARCHARThe entire field can be indexed and is suitable for text fields that require frequent searches.

2. Numeric data type

Number types are used to store integers or decimals, mainly divided intoInteger TypeandFloating point/fixed point type

2.1 Integer Type

Data Type Storage size Value range (unsigned) Applicable scenarios
TINYINT 1 byte 0 ~ 255 Boolean value (0/1), rating
SMALLINT 2 bytes 0 ~ 65,535 Small range of values, such as age
MEDIUMINT 3 bytes 0 ~ 16,777,215 Applicable to ID, self-increase fields
INT(INTEGER) 4 bytes 0 ~ 4,294,967,295 Suitable for most scenarios, such as user ID
BIGINT 8 bytes 0 ~ 18,446,744,073,709,551,615 Suitable for large values, such as bank account balance

Unsigned (UNSIGNED) vs. Signed

  • TEXTNot supporteddefault value, also unavailableIndex (only prefix indexing is supported), but can store a lot of text.
  • VARCHARThe entire field can be indexed and is suitable for text fields that require frequent searches.

2. Numeric data type

Number types are used to store integers or decimals, mainly divided intoInteger TypeandFloating point/fixed point type

2.1 Integer Type

Data Type Storage size Value range (unsigned) Applicable scenarios
TINYINT 1 byte 0 ~ 255 Boolean value (0/1), rating
SMALLINT 2 bytes 0 ~ 65,535 Small range of values, such as age
MEDIUMINT 3 bytes 0 ~ 16,777,215 Applicable to ID, self-increase fields
INT(INTEGER) 4 bytes 0 ~ 4,294,967,295 Suitable for most scenarios, such as user ID
BIGINT 8 bytes 0 ~ 18,446,744,073,709,551,615 Suitable for large values, such as bank account balance

Unsigned (UNSIGNED) vs. Signed

  • Integers are signed by default(i.e., negative numbers are supported).
  • UNSIGNEDKeywords can make integers store onlyNon-negative number, thereby expanding the available range.
CREATE TABLE users (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY
);

2.2 Floating and fixed-point types

Data Type Storage size Applicable scenarios
FLOAT(M, D) 4 bytes Suitable for non-strict precision calculations such as game ratings
DOUBLE(M, D) 8 bytes High-precision calculations, such as scientific calculations
DECIMAL(M, D)(NUMERIC) variable Financial calculations to avoid loss of accuracy

FLOAT vs. DECIMAL

FLOAT/DOUBLE uses binary storage, which may result in accuracy errors:

SELECT 0.1 + 0.2; -- The result may not be 0.3

DECIMAL uses string storage without losing precision:

CREATE TABLE transactions (
    amount DECIMAL(10,2) NOT NULL
);

suggestion:

  • Use of financial dataDECIMAL, avoid calculation errors.
  • Non-critical calculations (such as statistics) are availableFLOATorDOUBLE

3. Date & Time

MySQL provides multiple date and time types:

Data Type Storage size Applicable scenarios
FLOAT(M, D) 4 bytes Suitable for non-strict precision calculations such as game ratings
DOUBLE(M, D) 8 bytes High-precision calculations, such as scientific calculations
DECIMAL(M, D)(NUMERIC) variable Financial calculations to avoid loss of accuracy

DATETIME vs. TIMESTAMP

  • TIMESTAMP is affected by time zone, suitable for storing the time of event occurrence.
  • DATETIME is not affected by time zone, suitable for storage time.
CREATE TABLE events (
    event_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

4. Best practices for choosing a data type

  • If you can use integers, don't use strings (such as gender is availableTINYINT)。
  • When storing amounts and financial data, useDECIMALInsteadFLOAT
  • Short text (<255 characters)VARCHAR, for long textTEXT
  • Try to avoid index fieldsTEXTandBLOB, affects query performance.
  • Try to use timestamp dataTIMESTAMPreplaceDATETIME, save storage space.

Summarize

MySQL provides a variety of data types, each with its applicable scenarios. Reasonable selection of data types can improve storage efficiency, optimize query performance, and avoid accuracy losses. I hope this article can help you make better choices when designing databases! 🚀

This is the end of this article about MySQL data type detailed explanation: strings, numbers, and dates. For more related content of mysql data type, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!