Introduction
In this chapter, we will learn about the various data types available in MySQL. Data types are essential in defining the nature of the data that can be stored in each column of a table. Choosing the appropriate data type for each column ensures data integrity and optimizes storage and performance. We will cover the major categories of data types in MySQL, including numeric, string, date and time, and other types, along with examples and important considerations.
Numeric Data Types
Integer Types
-
TINYINT: A very small integer.
- Storage: 1 byte
- Range: -128 to 127 (signed), 0 to 255 (unsigned)
TINYINT column_name;
-
SMALLINT: A small integer.
- Storage: 2 bytes
- Range: -32,768 to 32,767 (signed), 0 to 65,535 (unsigned)
SMALLINT column_name;
-
MEDIUMINT: A medium-sized integer.
- Storage: 3 bytes
- Range: -8,388,608 to 8,388,607 (signed), 0 to 16,777,215 (unsigned)
MEDIUMINT column_name;
-
INT (INTEGER): A standard integer.
- Storage: 4 bytes
- Range: -2,147,483,648 to 2,147,483,647 (signed), 0 to 4,294,967,295 (unsigned)
INT column_name;
-
BIGINT: A large integer.
- Storage: 8 bytes
- Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (signed), 0 to 18,446,744,073,709,551,615 (unsigned)
BIGINT column_name;
Floating-Point Types
-
FLOAT: A small (single-precision) floating-point number.
- Storage: 4 bytes
FLOAT(column_name);
-
DOUBLE: A large (double-precision) floating-point number.
- Storage: 8 bytes
DOUBLE(column_name);
Fixed-Point Types
-
DECIMAL: A fixed-point number.
- Storage: Varies (based on the precision and scale)
DECIMAL(column_name, precision, scale);
String Data Types
Character String Types
-
CHAR: A fixed-length string.
- Storage: 1 byte per character
CHAR(column_name, length);
-
VARCHAR: A variable-length string.
- Storage: 1 byte per character + 1 or 2 bytes for length
VARCHAR(column_name, length);
Text Types
-
TINYTEXT: A very small text string.
- Storage: Up to 255 bytes
TINYTEXT(column_name);
-
TEXT: A small text string.
- Storage: Up to 65,535 bytes
TEXT(column_name);
-
MEDIUMTEXT: A medium-length text string.
- Storage: Up to 16,777,215 bytes
MEDIUMTEXT(column_name);
-
LONGTEXT: A large text string.
- Storage: Up to 4,294,967,295 bytes
LONGTEXT(column_name);
Binary Data Types
-
BINARY: A fixed-length binary string.
- Storage: 1 byte per byte
BINARY(column_name, length);
-
VARBINARY: A variable-length binary string.
- Storage: 1 byte per byte + 1 or 2 bytes for length
VARBINARY(column_name, length);
BLOB Types
-
TINYBLOB: A very small binary large object.
- Storage: Up to 255 bytes
TINYBLOB(column_name);
-
BLOB: A small binary large object.
- Storage: Up to 65,535 bytes
BLOB(column_name);
-
MEDIUMBLOB: A medium-sized binary large object.
- Storage: Up to 16,777,215 bytes
MEDIUMBLOB(column_name);
-
LONGBLOB: A large binary large object.
- Storage: Up to 4,294,967,295 bytes
LONGBLOB(column_name);
Date and Time Data Types
-
DATE: A date value.
- Format: ‘YYYY-MM-DD’
DATE(column_name);
-
DATETIME: A date and time value.
- Format: ‘YYYY-MM-DD HH:MM:SS’
DATETIME(column_name);
-
TIMESTAMP: A timestamp value.
- Format: ‘YYYY-MM-DD HH:MM:SS’
TIMESTAMP(column_name);
-
TIME: A time value.
- Format: ‘HH:MM:SS’
TIME(column_name);
-
YEAR: A year value.
- Format: ‘YYYY’
YEAR(column_name);
Other Data Types
-
ENUM: An enumeration, a string object with a value chosen from a list of permitted values.
- Storage: 1 or 2 bytes, depending on the number of enumeration values
ENUM('value1', 'value2', ..., 'valueN');
-
SET: A set, a string object that can have zero or more values, each chosen from a list of permitted values.
- Storage: 1, 2, 3, 4, or 8 bytes, depending on the number of set members
SET('value1', 'value2', ..., 'valueN');
Important Considerations
- Choose the Appropriate Data Type: Select data types based on the nature of the data to ensure data integrity and optimal performance.
- Storage Requirements: Consider the storage requirements of each data type, especially for large tables.
- Indexing: Some data types may impact the performance of indexes. For example, indexing large text or BLOB columns can be inefficient.
- Data Validation: Use constraints and data types to enforce data validation at the database level.
Conclusion
Understanding MySQL data types is crucial for designing efficient and effective databases. This chapter covered the major categories of data types, provided syntax and examples for each type, and discussed important considerations.