In MySQL, the MEDIUMINT data type is used to store integer values within a specific range. It is a fixed-width integer type that occupies 3 bytes of storage. This means that it can represent a range of values larger than the smaller TINYINT and SMALLINT types, but smaller than the INT type.
The MEDIUMINT type is often used when you need to store integer values that fall within the range of -8388608 to 8388607 for signed numbers, and 0 to 16777215 for unsigned numbers. The signed version can represent both positive and negative numbers, while the unsigned version can only represent non-negative values.
Syntax
Here is a basic syntax example of how to define a column with the MEDIUMINT data type:
CREATE TABLE example_table ( mediumint_column MEDIUMINT );
You can also specify whether the column should store signed or unsigned values explicitly:
CREATE TABLE example_table ( signed_mediumint_column MEDIUMINT, unsigned_mediumint_column MEDIUMINT UNSIGNED );
When working with MEDIUMINT columns, it’s important to choose the appropriate signed or unsigned version based on the nature of the data you are storing. If the values should only be positive, you can use the unsigned version to effectively double the range of positive values that can be stored.
In summary, the MEDIUMINT data type in MySQL is a space-efficient choice for storing integer values within a moderate range, striking a balance between storage space and the range of representable values. It is particularly useful when you want to conserve storage but need more range than what is provided by TINYINT or SMALLINT.