MySQL
unsigned
database
data types
SQL usage

What does unsigned in MySQL mean and when to use it?

System Design practice on Codemia

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

Practice system design

MySQL, one of the most popular relational database management systems, offers various data types to suit the storage and querying needs of applications. Among these data types, numeric types have the option to be either signed or unsigned. In this article, we will explore what unsigned means in MySQL, its implications, and when to use it.

Understanding the unsigned Attribute

In MySQL, numeric data types such as TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT, FLOAT, and DOUBLE can be specified with an unsigned attribute. This keyword specifies that the column cannot store negative numbers, effectively doubling the upper limit of positive values that can be stored, by reallocating the storage space usually offered to negative numbers.

Technical Explanation

Essentially, the unsigned attribute is a way to optimize your database by maximizing the range of positive numbers you can store with a given number of bytes. For instance, consider the INT data type:

  • Signed INT: Uses 4 bytes and stores values from -2,147,483,648 to 2,147,483,647.
  • Unsigned INT: Uses the same 4 bytes but stores values from 0 to 4,294,967,295.

By declaring a column as unsigned, the database system knows that all stored values will be non-negative, allowing it to repurpose bits (otherwise reserved for indicating negative values) to expand the range of positive numbers.

Example

Consider an example where we're designing a database for a store to keep track of item stocks and IDs:

sql
1CREATE TABLE inventory (
2  item_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
3  stock SMALLINT UNSIGNED NOT NULL,
4  PRIMARY KEY (item_id)
5);

In the example above:

  • item_id: Is an INT to allow a large number of items. It is unsigned to double its range, which is beneficial if the store has a vast inventory.
  • stock: Can't be negative, as it doesn't make sense to have a negative stock count. Therefore, using SMALLINT UNSIGNED ensures the range is suitable for the expected values.

When to Use unsigned in MySQL

Key Scenarios

ScenarioWhy Use unsigned?
Primary KeysPrimary keys like AUTO_INCREMENT IDs are naturally non-negative. Expanding their range to support more entries without additional storage is beneficial.
Quantity or Stock CountsValues such as item quantities, stock levels, or production counts generally do not need to be negative. Optimizing range is sensible here.
Flags and Status CodesWhen storing bit masks, flags, or enumerated status codes, negative values are unnecessary.
Financial TransactionsUse caution here, as financial transactions might involve credit (negative) and debit (positive) values; banks or similar applications may still need signed numbers.

Considerations

  • Data Type Selection: Choose the smallest data type that can accommodate your data requirements. For example, using TINYINT UNSIGNED for storing ages can be more efficient versus INT.
  • Future-Proofing: Opting for unsigned when the context permits non-negative values can ensure your application scales better as the dataset grows.
  • Error Handling: Be cautious about making assumptions regarding the range. It’s vital to ensure the application logic aligns with database constraints.

Additional Details

Impact on Storage:
MySQL automatically optimizes storage for unsigned types. This does not affect the storage size (in bytes), but rather how the range of values are utilized within those bytes.

Checking Unsigned Columns:
To check if a column is unsigned, you can run a DESCRIBE or SHOW COLUMNS command. The Type will indicate both the data type and if it’s unsigned.

Example:

sql
DESCRIBE inventory;

Outputs:

FieldTypeNullKeyDefaultExtra
item_idint(10) unsignedNOPRINULLauto_increment
stocksmallint(5) unsignedNONULL

In conclusion, the use of unsigned in MySQL is an effective way to manage non-negative numeric data comprehensively, while also optimizing the database to extend its handling capacity without requiring additional storage resources. Careful consideration of the data requirements and expected future growth will guide the prudent application of the unsigned keyword.


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

All Rights Reserved.