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.
Data types are categories of data that define the following aspects:
Choosing the right data type for your columns is important for several reasons:
MySQL supports SQL data types in several categories:
Category | Examples | Description |
---|---|---|
Numeric types | INT, DECIMAL, FLOAT, BIT | These 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 types | DATE, DATETIME, TIMESTAMP, TIME | These 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 types | CHAR, VARCHAR, BLOB, TEXT | These 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 types | GEOMETRY, POINT, LINESTRING, POLYGON | These 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 type | JSON | This 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. |
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:
Here is a quick reference table that summarizes the main characteristics of the MySQL data types:
Data type | Category | Size | Range | Format | Default | Example |
---|---|---|---|---|---|---|
INT UNSIGNED | Numeric | 4 bytes | 0 to 4294967295 | Integer | 0 | 42 |
VARCHAR(50) | String | Variable | Up to 50 characters | Text | Empty string | ‘Hello’ |
DECIMAL(10,2) | Numeric | Variable | -99999999.99 to 99999999.99 | Decimal | 0.00 | 3.14 |
TEXT | String | Variable | Up to 65535 characters | Text | Empty string | ‘This is a long text’ |
BLOB | String | Variable | Up to 65535 bytes | Binary | Empty string | 0x89504E470D0A1A0A |
DATETIME | Date and time | 8 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’ |
TIMESTAMP | Date and time | 4 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’ |
POINT | Spatial | Variable | Any point in a 2D plane | ‘X Y’ | NULL | ‘1 2’ |
JSON | JSON | Variable | Any valid JSON data | JSON | NULL | ‘{“name”: “John”, “age”: 25}’ |
Here are some useful tips and tricks for working with MySQL data types:
SHOW COLUMNS FROM products;
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'
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
SELECT JSON_EXTRACT(json, '$.name') FROM products; -- returns the name property of the JSON column
SELECT ST_DISTANCE(location, POINT(0,0)) FROM products;
SELECT * FROM products WHERE name LIKE '%book%';
-- returns the products whose name contains the word 'book'
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'
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.
Free 7-day trial. No credit card required.
Have a question? Need help getting started?
Get in touch via chat or at [email protected]