What is TINYINT in MySQL: Definition and Usage
TINYINT in MySQL is a very small integer data type that stores whole numbers using 1 byte of storage. It can hold values from -128 to 127 for signed or 0 to 255 for unsigned numbers.How It Works
Think of TINYINT as a tiny box that can hold only small whole numbers. It uses just 1 byte of space, which means it can store numbers in a limited range. If you imagine a ruler marked from -128 to 127, that's the range of numbers a signed TINYINT can hold. If you only need positive numbers, you can make it unsigned, which shifts the range to 0 through 255.
This small size makes TINYINT very efficient when you know your numbers won't be large. It's like choosing a small jar to store a few candies instead of a big jar, saving space in your database.
Example
This example shows how to create a table with a TINYINT column and insert some values.
CREATE TABLE example_tinyint ( id INT AUTO_INCREMENT PRIMARY KEY, age TINYINT, score TINYINT UNSIGNED ); INSERT INTO example_tinyint (age, score) VALUES (-10, 200), (127, 255), (0, 0); SELECT * FROM example_tinyint;
When to Use
Use TINYINT when you need to store small numbers and want to save space. For example, it is perfect for storing age (if you expect ages under 128), small counters, flags (like 0 or 1 for true/false), or status codes. It helps keep your database efficient and fast by using less storage.
If you expect larger numbers, consider using bigger integer types like SMALLINT or INT.
Key Points
- TINYINT uses 1 byte of storage.
- Signed range: -128 to 127.
- Unsigned range: 0 to 255.
- Good for small numbers and flags.
- Saves space compared to larger integer types.
Key Takeaways
TINYINT stores small whole numbers using 1 byte in MySQL.TINYINT limits.