Close

2023-10-17

TINYINT Data Type in MySQL

TINYINT Data Type in MySQL

In MySQL, TINYINT is a data type used to store small integer values. Let’s delve into its specifics and see how it compares to other integer types:

TINYINT

  • Storage: TINYINT Uses 1 byte of storage.
  • Range:
  • If signed (allows both positive and negative values): -128 to 127
  • If unsigned (only non-negative values): 0 to 255

Comparison with Other Integer Types:

  1. SMALLINT
  • Storage: 2 bytes
  • Range:
    • Signed: -32,768 to 32,767
    • Unsigned: 0 to 65,535
  1. MEDIUMINT
  • Storage: 3 bytes
  • Range:
    • Signed: -8,388,608 to 8,388,607
    • Unsigned: 0 to 16,777,215
  1. INT or INTEGER
  • Storage: 4 bytes
  • Range:
    • Signed: -2,147,483,648 to 2,147,483,647
    • Unsigned: 0 to 4,294,967,295
  1. BIGINT
  • Storage: 8 bytes
  • Range:
    • Signed: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
    • Unsigned: 0 to 18,446,744,073,709,551,615

Key Differences:

  • Storage Size: The primary difference among these integer types is the amount of storage they use, which ranges from 1 byte for TINYINT to 8 bytes for BIGINT.
  • Range: With increasing storage size, the range of values each type can store increases. TINYINT is suitable for tiny numbers, while BIGINT can handle huge numbers.
  • Use Cases: The choice of integer type often depends on the use case. For instance, TINYINT might be used for storing age (assuming age won’t exceed 255) while BIGINT might be used for unique identifiers that require a vast range.

When designing a database schema, choosing the appropriate integer type based on the expected range of values is essential to optimize storage and performance.