Mastering MySQL Data Types: A Comprehensive Guide

SimpleBackups developer

Nour Sofanati

Developer, SimpleBackups

November 9, 2023

MySQL is one of the most popular and widely used relational database management systems in the world. It supports a variety of data types that allow you to store different kinds of data in your tables. In this article, we will explore the different data types that MySQL offers, their characteristics, and how to use them effectively.

Table of Contents

What are Data Types?

Data types are categories of data that define the following aspects:

  • The range of values that a column can store.
  • The storage format and size of the data.
  • The operations that can be performed on the data.
  • The way the data is displayed.

Choosing the right data type for your columns is important for several reasons:

  • It ensures the accuracy and integrity of your data.
  • It optimizes the performance and efficiency of your queries.
  • It saves storage space and reduces memory usage.

MySQL Data Types Categories

MySQL supports SQL data types in several categories:

CategoryExamplesDescription
Numeric typesINT, DECIMAL, FLOAT, BITThese types are used to store numeric values, such as integers, decimals, and fractions.
They can be signed or unsigned, meaning they can have positive
or negative values, or only positive values.
Date and time typesDATE, DATETIME, TIMESTAMP, TIMEThese types are used to store date and time values,
such as years, months, days, hours, minutes, seconds, and fractions of seconds.
They can also store time zones and intervals.
String typesCHAR, VARCHAR, BLOB, TEXTThese types are used to store text and binary data,
such as characters, words, sentences, paragraphs, images, videos, and files.
They can have fixed or variable lengths, depending on the type.
Spatial typesGEOMETRY, POINT, LINESTRING, POLYGONThese types are used to store geometric and geographic data,
such as points, lines, polygons, and regions.
They can be used for spatial analysis and operations,
such as finding the distance, area, or intersection of shapes.
JSON typeJSONThis type is used to store JSON (JavaScript Object Notation) data,
which is a lightweight and flexible format for exchanging and storing data.
JSON data can be composed of arrays, objects, strings, numbers, booleans, and nulls.
The JSON type allows you to manipulate
and query JSON data using SQL functions and operators.

MySQL Data Types Examples

Let’s look at some examples of how to use the different data types in MySQL. We will use the following table as a reference:

CREATE TABLE products (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  name VARCHAR(50) NOT NULL,
  price DECIMAL(10,2) NOT NULL,
  description TEXT,
  image BLOB,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  location POINT NOT NULL,
  PRIMARY KEY (id)
);

This table stores information about some products, such as their id, name, price, description, image, creation date, update date, and location. Let’s see how each column uses a different data type:

  • id: INT UNSIGNED - A numeric type that can store integers from 0 to 4,294,967,295. It is unsigned and auto-incremented.
  • name: VARCHAR(50) - A string type that can store up to 50 characters. It is variable-length and cannot have empty values.
  • price: DECIMAL(10,2) - A numeric type that can store exact decimal values with a precision of 10 digits and a scale of 2 digits. It cannot have zero or null values.
  • description: TEXT - A string type that can store up to 65,535 characters. It is variable-length and can store any kind of text.
  • image: BLOB - A string type that can store up to 65,535 bytes of binary data. It can store any kind of binary data but cannot be indexed or searched.
  • created_at: DATETIME - A date and time type that can store values from '1000-01-01 00:00:00' to '9999-12-31 23:59:59'. It has a fixed format and cannot have empty values. It has a default value of CURRENT_TIMESTAMP.
  • updated_at: TIMESTAMP - A date and time type that can store values from '1970-01-01 00:00:01' UTC to 'January 19, 2038 03:14:07' UTC. It has a fixed format and cannot have empty values. It has a default value of CURRENT_TIMESTAMP and updates automatically when the row is modified.
  • location: POINT - A spatial type that can store a single point in a two-dimensional plane. It has a fixed format of 'X Y' and cannot have empty values. It can be used for spatial operations.

MySQL Data Types Cheatsheet

Here is a quick reference table that summarizes the main characteristics of the MySQL data types:

Data typeCategorySizeRangeFormatDefaultExample
INT UNSIGNEDNumeric4 bytes0 to 4294967295Integer042
VARCHAR(50)StringVariableUp to 50 charactersTextEmpty string‘Hello’
DECIMAL(10,2)NumericVariable-99999999.99 to 99999999.99Decimal0.003.14
TEXTStringVariableUp to 65535 charactersTextEmpty string‘This is a long text’
BLOBStringVariableUp to 65535 bytesBinaryEmpty string0x89504E470D0A1A0A
DATETIMEDate and time8 bytes‘1000-01-01 00:00:00’ to ‘9999-12-31 23:59:59’‘YYYY-MM-DD HH:MM:SS’CURRENT_TIMESTAMP‘2023-11-09 18:06:28’
TIMESTAMPDate and time4 bytes‘1970-01-01 00:00:01’ UTC to ‘2038-01-19 03:14:07’ UTC‘YYYY-MM-DD HH:MM:SS’CURRENT_TIMESTAMP‘2023-11-09 18:06:28’
POINTSpatialVariableAny point in a 2D plane‘X Y’NULL‘1 2’
JSONJSONVariableAny valid JSON dataJSONNULL‘{“name”: “John”, “age”: 25}’

MySQL Data Types Tips and Tricks

Here are some useful tips and tricks for working with MySQL data types:

  • You can use the SHOW COLUMNS command to see the data types of the columns in a table. For example:
SHOW COLUMNS FROM products;
  • You can use the CAST function to convert a value from one data type to another. For example:
SELECT CAST('3.14' AS INT); -- returns 3
SELECT CAST(42 AS CHAR); -- returns '42'
SELECT CAST('2023-11-09' AS DATE); -- returns '2023-11-09'
  • You can use the IS NULL and IS NOT NULL operators to check if a value is null or not. For example:
SELECT * FROM products WHERE image IS NULL; -- returns the products without an image
SELECT * FROM products WHERE location IS NOT NULL; -- returns the products with a location
  • You can use the JSON_EXTRACT function to extract a value from a JSON column. For example:
SELECT JSON_EXTRACT(json, '$.name') FROM products; -- returns the name property of the JSON column
  • You can use the ST_DISTANCE function to calculate the distance between two points in a spatial column. For example:
SELECT ST_DISTANCE(location, POINT(0,0)) FROM products;
  • You can use the LIKE operator to perform a pattern matching on a string column. For example:
SELECT * FROM products WHERE name LIKE '%book%';
-- returns the products whose name contains the word 'book'
  • You can use the ENUM and SET types to store a predefined list of values in a column. For example:
CREATE TABLE colors ( 
  id INT NOT NULL,
  name ENUM('red', 'green', 'blue') NOT NULL,
  shades SET('light', 'dark', 'bright') NOT NULL,
  PRIMARY KEY (id) 
);

This table stores the name and shades of some colors, using the ENUM type for the name column and the SET type for the shades column. The ENUM type can store only one value from the list, while the SET type can store zero or more values from the list. For example:

INSERT INTO colors VALUES (1, 'red', 'dark,bright'); -- inserts a row with the name 'red' and the shades 'dark' and 'bright'
INSERT INTO colors VALUES (2, 'green', ''); -- inserts a row with the name 'green' and no shades
INSERT INTO colors VALUES (3, 'blue', 'light'); -- inserts a row with the name 'blue' and the shade 'light'

Conclusion

In this article, we have learned about the different data types that MySQL supports, their characteristics, and how to use them effectively. We have also seen some examples, a cheatsheet, and some tips and tricks for working with MySQL data types. We hope you have found this article useful and informative. Thank you for reading.



Back to blog

Stop worrying about your backups.
Focus on building amazing things!

Free 7-day trial. No credit card required.

Have a question? Need help getting started?
Get in touch via chat or at [email protected]

Customer support with experts
Security & compliance
Service that you'll love using