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

Cover Image for What is the difference between tinyint, smallint, mediumint, bigint and int in MySQL?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

Understanding the Integers in MySQL 🧮

When working with MySQL, you might come across different integer data types like tinyint, smallint, mediumint, bigint, and int. 🤔 It's important to understand the differences between them and know when to use each one. Let's dive right in!

Tinyint 😄

The tinyint data type is the smallest integer type in MySQL. It can store values from -128 to 127 or 0 to 255 when specified as unsigned. This means it uses only 1 byte of storage. 🔢

When to use tinyint? 🤷‍♂️

Tinyint is commonly used to represent boolean values, where 0 represents false and any other value represents true. For instance, imagine a column called "active" in a user table with values 0 for inactive and 1 for active. ☑️

Smallint 😄

The smallint data type is slightly larger than tinyint and can store values from -32,768 to 32,767 or 0 to 65,535 when specified as unsigned. It uses 2 bytes of storage. 📊

When to use smallint? 🤷‍♀️

Smallint is suitable for cases where you need a greater range of integer values compared to tinyint but still want to conserve space. It can be used for things like storing the number of items in an order or tracking a product's stock. 🛒

Mediumint 😄

Mediumint is the next step up and can store values from -8,388,608 to 8,388,607 or 0 to 16,777,215 when specified as unsigned. It requires 3 bytes of storage. 📈

When to use mediumint? 🤔

Mediumint is useful for scenarios where you need larger numbers than smallint can handle, but you don't want the overhead of using a 4-byte integer (int). It strikes a balance between range and storage efficiency. For example, tracking monthly sales figures could be a good use case for mediumint. 💰

Bigint 😄

The bigint data type is the largest integer type in MySQL. It can store values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 or 0 to 18,446,744,073,709,551,615 when specified as unsigned. It requires 8 bytes of storage. 📈

When to use bigint? 🧐

Bigint should be used when you need to handle extremely large numbers, such as primary keys that require a wide range of unique values or when dealing with financial transactions that involve large sums of money. Think of it as a heavy-duty data type for heavy-duty tasks. 💪

Int 😄

Lastly, we have the int data type. Int stands for "integer" and is the most commonly used integer type. It can store values from -2,147,483,648 to 2,147,483,647 or 0 to 4,294,967,295 when specified as unsigned. It uses 4 bytes of storage. ⚙️

When to use int? 🧠

Integers are widely used in various scenarios, such as auto-incrementing primary keys, storing quantities, or representing IDs. It strikes a fine balance between range and storage efficiency.

💡 Remember that choosing the appropriate data type is crucial for database performance and storage optimization.

To summarize:

  • Use tinyint for boolean values or small numeric ranges.

  • Use smallint for larger numeric ranges while conserving space.

  • Use mediumint for larger ranges than smallint and space optimization.

  • Use bigint for extremely large numbers or vast numeric ranges.

  • Use int for general-purpose integer values.

Got any doubts or questions? Feel free to drop them in the comments section below! Let's get the conversation started. 💬

Happy coding! 😊👩‍💻👨‍💻


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello