MySQL
integer data types
tinyint
bigint
SQL指南

What is the difference between tinyint, smallint, mediumint, bigint and int in MySQL?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

In MySQL, choosing the appropriate integer data type is crucial for database optimization, storage efficiency, and ensuring that the values fit the intended range. MySQL provides several integer data types that serve different storage needs:

  1. TINYINT
  2. SMALLINT
  3. MEDIUMINT
  4. INT
  5. BIGINT

Each of these data types differs in terms of storage requirements and range of values they can store. Let's explore them in detail.

Data Types Overview

TINYINT

  • Storage Size: 1 byte
  • Unsigned Range: 0 to 255
  • Signed Range: -128 to 127

TINYINT is the smallest integer type and is often used when you need to store very small numbers. This is useful for things like storing boolean values (0 and 1) or small categorical data.

Example of usage:

sql
CREATE TABLE users (
  isActive TINYINT(1) UNSIGNED NOT NULL
);

SMALLINT

  • Storage Size: 2 bytes
  • Unsigned Range: 0 to 65,535
  • Signed Range: -32,768 to 32,767

SMALLINT is suitable for slightly larger numbers than those handled by TINYINT, such as ages, counts, and other small numerical data.

Example of usage:

sql
CREATE TABLE products (
  stock SMALLINT UNSIGNED NOT NULL
);

MEDIUMINT

  • Storage Size: 3 bytes
  • Unsigned Range: 0 to 16,777,215
  • Signed Range: -8,388,608 to 8,388,607

MEDIUMINT offers a larger range than SMALLINT and is less commonly used, but can be handy for storing medium-sized numbers, like a moderate database ID system.

Example of usage:

sql
CREATE TABLE events (
  eventId MEDIUMINT UNSIGNED AUTO_INCREMENT PRIMARY KEY
);

INT

  • Storage Size: 4 bytes
  • Unsigned Range: 0 to 4,294,967,295
  • Signed Range: -2,147,483,648 to 2,147,483,647

INT is one of the most commonly used integer types and handles most standard numerical data. This suits typical use cases like user IDs, prices, or any moderate numerical data.

Example of usage:

sql
CREATE TABLE accounts (
  accountId INT UNSIGNED AUTO_INCREMENT PRIMARY KEY
);

BIGINT

  • Storage Size: 8 bytes
  • Unsigned Range: 0 to 18,446,744,073,709,551,615
  • Signed Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

BIGINT is used for very large numbers that exceed the capacity of INT. It's appropriate for cases like large financial transactions, astronomical computations, or storing timestamps well into the future.

Example of usage:

sql
CREATE TABLE transactions (
  transactionId BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY
);

Key Differences and Summary Table

The primary differences between these data types lie in their storage size and range of values:

Data TypeStorage SizeSigned RangeUnsigned Range
TINYINT1 byte-128 to 1270 to 255
SMALLINT2 bytes-32,768 to 32,7670 to 65,535
MEDIUMINT3 bytes-8,388,608 to 8,388,6070 to 16,777,215
INT4 bytes-2,147,483,648 to 2,147,483,6470 to 4,294,967,295
BIGINT8 bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 0 to 18,446,744,073,709,551,615

Additional Considerations

Choosing the Right Type

Choosing the correct integer type can significantly affect the size and performance of the database. Smaller integer types save space but limit the range of data, whereas larger integer types consume more storage.

Signed vs. Unsigned

Unsigned types can only store non-negative numbers, effectively doubling the maximum positive range compared to their signed counterparts. Use unsigned integers when you know the values will be non-negative, like IDs or countable quantities.

Storage Efficiency

Optimizing the storage size can lead to significant improvements, particularly in large tables. The choice affects how the data is stored, indexed, and retrieved.

Practical Scenarios

  • Use TINYINT for small flags, boolean fields, or very small numbers.
  • Use SMALLINT for small-scale numerical data like small counts or ranges.
  • Use MEDIUMINT when you anticipate larger numbers than SMALLINT, but want to conserve space.
  • Use INT for the majority of cases as it's a balanced choice for many applications.
  • Use BIGINT for extremely large values, such as timestamps or financial transactions.

Selecting the right integer type not only saves storage space and reduces memory overhead but also impacts query performance and ensures data integrity through appropriate constraints.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design