Data Types in MySQL 8: A Comprehensive Cheat Sheet

Updated: January 25, 2024 By: Guest Contributor Post a comment

Introduction

In database management, understanding data types is crucial for the proper structuring of databases and ensuring data integrity. MySQL 8, the popular open-source relational database management system (RDBMS), supports a wide array of data types that facilitate the storage of different kinds of information. This article serves as a comprehensive cheat sheet, providing insights and code examples of the various data types in MySQL 8.

String Data Types

MySQL supports various string data types for storing text. Below are some of the most commonly used string types:

  • VARCHAR: A variable-length string that can contain up to 65,535 characters.
  • CHAR: A fixed-length string that can contain up to 255 characters.
  • TEXT: A long text string that can contain up to 65,535 characters.

Example:

CREATE TABLE example_string_types (
    name VARCHAR(255),
    nickname CHAR(30),
    comments TEXT
);

This creates a table `example_string_types` with a `VARCHAR` field for name, a `CHAR` field for nickname, and a `TEXT` field for comments.

Numeric Data Types

Numeric data types in MySQL are used to store numbers, whether they are integers or floating-point numbers. Here they are categorized based on their storage and precision:

  • INT: A normal-size integer that can be signed or unsigned.
  • DECIMAL: A fixed-point number with defined precision and scale.
  • DOUBLE: A double-precision floating-point number.

Example:

CREATE TABLE example_numeric_types (
    age INT,
    salary DECIMAL(10,2),
    average_score DOUBLE(7,2)
);

Here, `age` is an integer, `salary` is a decimal with two digits after the decimal point, and `average_score` is a double with two digits after the decimal point.

Date and Time Data Types

Date and time data types are used to store dates, times, and timestamps. The following are some of the most significant date and time types:

  • DATE: A date in the format YYYY-MM-DD.
  • TIMESTAMP: A timestamp, combining date and time in the format YYYY-MM-DD HH:MM:SS.
  • DATETIME: Similar to TIMESTAMP but without timezone support.

Example:

CREATE TABLE example_date_types (
    birthday DATE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    event_time DATETIME
);

This table example includes a `DATE` field for birthday, a `TIMESTAMP` field that auto-fills with the current timestamp, and a `DATETIME` for scheduling events.

Specialized Data Types

MySQL 8 introduces some specialized data types for specific use cases:

  • JSON: A type for storing JSON (JavaScript Object Notation) documents.
  • ENUM: A string object that can only have one value, chosen from a list of possible values.
  • SET: Similar to ENUM but can store one or more of the allowed values.

Example:

CREATE TABLE example_special_types (
    settings JSON,
    shirt_size ENUM('small', 'medium', 'large'),
    interests SET('sports', 'music', 'travel', 'books')
);

This is an example of how to store JSON data and use ENUM and SET data types that limit the choices in a meaningful way.

Binary Data Types

For binary data, like images or files, MySQL supports the following data types:

  • BLOB: A large binary object that can hold a variable amount of data.
  • VARBINARY: Similar to VARCHAR, but for binary strings.
  • BINARY: Similar to CHAR, but for binary strings.

Example:

CREATE TABLE example_binary_types (
    profile_picture BLOB,
    file_checksum BINARY(16)
);

This table example defines fields for storing a binary object like a profile picture and a fixed-length binary string such as a file checksum.

Spatial Data Types

Spatial data types are used to store geometric data. Some of these types are:

  • GEOMETRY: The base data type for all spatial data types.
  • POINT: A single location in space.
  • LINESTRING: A sequence of points that form a line.
  • POLYGON: A planar surface representing a multi-sided shape.

Example:

CREATE TABLE example_spatial_types (
    location POINT,
    path LINESTRING,
    area POLYGON
);

This example demonstrates how to use spatial data types to represent different geometric data.

Conclusion

Understanding data types is key to designing efficient and robust databases in MySQL 8. This guide has outlined the main data types and provided examples to show how they are used in table creation. With the right data types, you can ensure that your data is stored reliably and that your database performs optimally.