02.10.2023

Data types in MySQL

Introduction

There are different areas in our world with information system and each of them require effectiveness, stability and desirable rapidly speed of working. But it's impossible to process data in the one format, sometimes we need to use text, numeric data or even a bunch of mixed information. For that people have developed, types of data and in the various examples of DBMS, they have distinguished. We will consider all of them for MySQL system and choosing appropriate data for different cases.

How to choose type?

In the official documentation you can see a bunch of different information, but we can categorise all data for parameters: type, speed of using data, taken storage and accuracy of contain values. Each of set data have their configuration. When deciding on the appropriate data type, it's advisable to opt for the most compact option capable of accommodating your specific data requirements. This approach not only conserves storage space but also enhances overall system performance.

In cases where data length varies, such as with product descriptions, it is recommended to employ a variable—length data type like VARCHAR. For exceptionally large data, such as video files, employing a binary data type like LONGBLOB is the optimal choice.
Moreover, when dealing with data that necessitates frequent searching or sorting, it is crucial to select a data type optimized for these operations. For instance, the CHAR data type outperforms VARCHAR when comparing strings efficiently.

Numeric types

One of the most commonly used type of data, represent various set of range. First it's integer, have 5 type of using range from tiny to big.

Screenshot №1 — Range of integer

We can make it by using several commands:

USE your_database
CREATE TABLE t1 (c1 TINYINT, c2 BIGINT);

Screenshot №2 — Table of integer

Then entering needed data to your table by the command below:

INSERT INTO t1 (c1, c2) VALUES(127, 256);

After that we need to make sure how data look in the table. If you want to use range without minus numbers, then indicate UNSIGNED option.

SELECT c1, c2 FROM t1;

Screenshot №3 — Result of integer

The form or mask of the table we can check by the typing that command:

DESCRIBE t1;

Highlight! Don't forget to replace t1 for your table name!

Screenshot №4 — Description of table

The UNSIGNED attribute offers two primary advantages:

Nevertheless, there are some considerations to bear in mind when utilizing the UNSIGNED attribute:

In the DBMS for using that option enter:

ALTER TABLE t1 ADD COLUMN c3 INT UNSIGNED;
DESCRIBE t1;

Screenshot №5 — Unsigned

Consider these scenarios as examples of when to employ each data type.

Float:

Double:

Decimal:

Integer:

String types

In the MySQL we can use string types for store text data, but sometimes we don't know, how much space of length they will be. Therefore, there are two types of storage data variable and fixed. One specific string type in MySQL is CHAR, which falls into the fixed—length category. When you define a CHAR column in a database table, you specify its length, indicating how many characters it can hold. For instance, if you create a CHAR column with a length of 10, it will always store exactly 10 characters, even if you enter less than that.

ALTER TABLE t1 ADD COLUMN string1 CHAR(10);

Then check the result of entering command:

DESCRIBE t1;

Screenshot №6 — String type

The inherent flexibility of variable string types manifests in their efficient utilization of storage space, dynamically adapting to accommodate text of varying lengths. This adaptability renders them invaluable for diverse applications where text content exhibits substantial variations in length.

Another types

MySQL includes binary data types specifically designed for storing binary data, such as images, audio clips, and video files, in their raw, unencoded, and uncompressed form. Below, you'll find a concise overview of these binary types:

MySQL also provides other data types with distinct functions:

These diverse data types empower database designers to effectively manage a wide range of data, from binary content to enumerated choices and logical flags.

Conclusion

MySQL offers a rich array of data types to handle a wide variety of data effectively. The choice of data type should align with the specific data characteristics and system requirements, ensuring efficient storage and retrieval of data in diverse applications.