MySQL TINYINT is a data type used to store integer values that are small in size. It is a 1-byte integer data type, which means it can store values in the range of -128 to 127 for signed TINYINT and 0 to 255 for unsigned TINYINT. The TINYINT data type is commonly used when you need to store small integers to conserve storage space.
Key points
Here are some key points about the MySQL TINYINT data type:
Size
TINYINT is a compact data type that occupies 1 byte of storage.
It is smaller in size compared to other integer data types such as INT or BIGINT.
Range
For signed TINYINT, the range is from -128 to 127.
For unsigned TINYINT, the range is from 0 to 255.
The signed or unsigned attribute can be specified when defining a column of type TINYINT.
Storage
TINYINT is a fixed-size data type, which means it always takes up the same amount of storage regardless of the actual value stored.
It is more space-efficient than larger integer types, making it suitable for columns that will store relatively small numbers.
Usage
TINYINT is commonly used when you need to store small integers, such as boolean values (0 or 1) to represent true/false conditions.
It is also useful for columns where the range of possible values is known to be within the TINYINT range.
Boolean Representation
In MySQL, TINYINT is often used to represent BOOLEAN values, where 0 is considered as false and any non-zero value is considered as true.
For example, TINYINT(1) is commonly used to store boolean values, where 0 represents false and 1 represents true.
Default Values
When a TINYINT column is defined without a default value, it defaults to 0.
The default value can be explicitly set to a different value if needed.
Example
Example of creating a table with a TINYINT column:
CREATE TABLE example_table ( id INT PRIMARY KEY, status TINYINT, age TINYINT UNSIGNED );
In the above example, the status column is a signed TINYINT, and the age column is an unsigned TINYINT.
In summary, the MySQL TINYINT data type is a compact and efficient way to store small integer values, making it a good choice for columns where space optimization is important.