Understanding-Data-Types (1)
Understanding-Data-Types (1)
Data types define the kind of data that can be stored in a column of a database table. Choosing the
right data type is crucial for:
• Data Integrity: Ensuring that only valid data is stored.
• Storage Efficiency: Using the minimum necessary space.
• Query Performance: Optimizing how data is retrieved.
Common Data Types in MySQL (phpMyAdmin)
Here's a breakdown of commonly used data types in MySQL, focusing on their sizes and usage:
1. Numeric Data Types:
• INT (Integer):
o Stores whole numbers (positive, negative, or zero).
o Sizes:
▪ TINYINT: 1 byte (-128 to 127 or 0 to 255 unsigned)
▪ SMALLINT: 2 bytes (-32768 to 32767 or 0 to 65535 unsigned)
▪ MEDIUMINT: 3 bytes (-8388608 to 8388607 or 0 to 16777215 unsigned)
▪ INT (or INTEGER): 4 bytes (-2147483648 to 2147483647 or 0 to 4294967295
unsigned)
▪ BIGINT: 8 bytes (-9223372036854775808 to 9223372036854775807 or 0 to
18446744073709551615 unsigned)
o Use: For storing counts, IDs, and other whole numbers.
• DECIMAL (or NUMERIC):
o Stores exact numeric values with a fixed precision and scale.
o Syntax: DECIMAL(precision, scale)
▪ precision: Total number of digits.
▪ scale: Number of digits after the decimal point.
o Use: For financial data or any data requiring exact precision.
Examples
DECIMAL(5, 2):
Precision: 5
Scale: 2
This means the number can have a total of 5 digits, with 2 of those digits after the
decimal point.
The range of values that could be stored would be from -999.99 to 999.99.
DECIMAL(10, 3):
Precision: 10
Scale: 3
This means the number can have a total of 10 digits, with 3 of those digits after the
decimal point.
DECIMAL(3, 0):
Precision: 3
Scale: 0
This means that there will be a total of 3 digits, and none of those digits will be after
the decimal point. Therefore, this acts as an integer value.
Key note:
The "VARCHAR(255)" limit is a historical convention that's still commonly seen, and
it's important to understand its origins and how it applies in modern MySQL.
Historical Context:
In older versions of MySQL (prior to 5.0.3), the maximum length for a VARCHAR
column was indeed 255 bytes. This limitation stemmed from how the length of the
variable-length string was stored internally.
Because of this historical limitation, developers commonly used VARCHAR(255) as
a standard. This practice has carried over, even though modern MySQL versions
have greatly expanded the maximum VARCHAR length.
Modern MySQL:
In modern MySQL versions (5.0.3 and later), the maximum length for a VARCHAR
column is 65,535 bytes.However, it's crucial to remember that this is a byte limit,
not a character limit. The number of characters that can be stored depends on the
character set used.
For example, with a single-byte character set like latin1, VARCHAR(255) can
store 255 characters.
With a multi-byte character set like utf8mb4, which can use up to 4 bytes
per character, VARCHAR(255) can store fewer characters.
Also the 65,535 byte limit is a row limit, so all of the columns in a given row,
must not total more than 65,535 bytes.
Example:
• This would allow you to store customer names up to 255 characters long
(depending on the character set).
Key Considerations:
• Storage Space: Choose the smallest data type that can accommodate your data to save
storage space.
• Performance: Using appropriate data types can improve query performance.
• Data Validation: Data types help enforce data integrity by restricting the type of data that
can be stored.
• UNSIGNED: Many numeric datatypes can be declared as unsigned. This means that the
column will only store positive numbers and zero, effectively doubling the maximum
positive number that can be stored.
______________
References:
MySQL. (n.d.). MySQL 8.4 Reference Manual: Data Types. Retrieved March 7, 2025, from
https://dev.mysql.com/doc/refman/8.4/en/data-types.html