MySQL Data Types

MySQL Data Types

MySQL Data Types

In MySQL, data types define the data type that can be stored in a column. Choosing the right data type ensures efficient storage, better performance, and data integrity.


Categories of MySQL Data Types

MySQL data types are broadly classified into the following categories:

  1. Numeric Types
  2. Date and Time Types
  3. String (Character) Types
  4. Spatial Types
  5. JSON Type

1. Numeric Data Types

Integer Types

TypeStorage (Bytes)Minimum ValueMaximum ValueUnsigned Maximum Value
TINYINT1-1281270 to 255
SMALLINT2-32,76832,7670 to 65,535
MEDIUMINT3-8,388,6088,388,6070 to 16,777,215
INT/INTEGER4-2,147,483,6482,147,483,6470 to 4,294,967,295
BIGINT8-9,223,372,036,854,775,8089,223,372,036,854,775,8070 to 18,446,744,073,709,551,615

Floating-Point Types

TypeStorage (Bytes)PrecisionExample Values
FLOAT(p)4 or 8Approx. 7 digits3.14, -2.71
DOUBLE/REAL8Approx. 16 digits2.718281828459

Fixed-Point Types

TypeDescriptionExample
DECIMAL(p,s) or NUMERIC(p,s)Precision (p) and scale (s)DECIMAL(10,2) for 12345678.90

2. Date and Time Data Types

TypeStorage (Bytes)FormatDescription
DATE3YYYY-MM-DDStores date values.
DATETIME8YYYY-MM-DD HH:MM:SSStores date and time.
TIMESTAMP4YYYY-MM-DD HH:MM:SSStores UTC-based date and time.
TIME3HH:MM:SSStores time values.
YEAR1YYYYStores 4-digit year values.

3. String (Character) Data Types

Character Types

TypeStorage (Bytes)Description
CHAR(n)Fixed length (1-255)Fixed-length string. E.g., CHAR(10)
VARCHAR(n)Variable length (1-65,535)Variable-length string. E.g., VARCHAR(50)

Text Types

TypeStorage (Bytes)Maximum LengthDescription
TINYTEXT1 + L255 charactersSmall text.
TEXT2 + L65,535 charactersLarge text.
MEDIUMTEXT3 + L16,777,215 charactersMedium-sized text.
LONGTEXT4 + L4,294,967,295 charactersVery large text.

Binary Data Types

TypeStorage (Bytes)Description
BINARY(n)Fixed length (1-255)Fixed-length binary data.
VARBINARY(n)Variable length (1-65,535)Variable-length binary data.

Blob Types

TypeStorage (Bytes)Maximum LengthDescription
TINYBLOB1 + L255 bytesSmall binary objects.
BLOB2 + L65,535 bytesLarge binary objects.
MEDIUMBLOB3 + L16,777,215 bytesMedium binary objects.
LONGBLOB4 + L4,294,967,295 bytesVery large binary objects.

4. Spatial Data Types

MySQL supports geographic data types through the Spatial Extensions.

TypeDescription
GEOMETRYStores any spatial data.
POINTStores a single point (X, Y).
LINESTRINGStores a series of points forming a line.
POLYGONStores a closed geometric shape.

5. JSON Data Type

TypeDescription
JSONStores JSON-formatted text data.

Choosing the Right Data Type

  1. Numeric Data: Use exact types (INT, DECIMAL) for precise calculations and approximate types (FLOAT, DOUBLE) for scientific data.
  2. String Data: Use CHAR for fixed-length fields and VARCHAR for variable-length fields.
  3. Large Text/Binary Data: Use TEXT or BLOB for large unstructured data.
  4. Date and Time: Use TIMESTAMP for UTC-based logging and DATETIME for general date-time storage.
  5. Hierarchical/Spatial Data: Use spatial types for GIS or geometric data.

Common Mistakes and Best Practices

  • Overusing TEXT or BLOB: Only use these types when absolutely necessary, as they can impact performance.
  • Choosing Inappropriate Size: For example, using BIGINT when INT is sufficient to increase storage overhead.
  • Not Indexing JSON Data: JSON data types can be indexed using generated columns.

This guide provides an overview of MySQL data types and their best use cases. Let me know if you need deeper insights or further examples!

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close