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.
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:
In the example above:
item_id: Is anINTto allow a large number of items. It isunsignedto 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, usingSMALLINT UNSIGNEDensures the range is suitable for the expected values.
When to Use unsigned in MySQL
Key Scenarios
| Scenario | Why Use unsigned? |
| Primary Keys | Primary keys like AUTO_INCREMENT IDs are naturally non-negative. Expanding their range to support more entries without additional storage is beneficial. |
| Quantity or Stock Counts | Values such as item quantities, stock levels, or production counts generally do not need to be negative. Optimizing range is sensible here. |
| Flags and Status Codes | When storing bit masks, flags, or enumerated status codes, negative values are unnecessary. |
| Financial Transactions | Use 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 UNSIGNEDfor storing ages can be more efficient versusINT. - Future-Proofing: Opting for
unsignedwhen 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:
Outputs:
| Field | Type | Null | Key | Default | Extra |
| item_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| stock | smallint(5) unsigned | NO | NULL |
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
- What effects does using a binary collation have?
- What exactly differs fuzzy search from Full Text Search?
- What exactly does transaction.state.log.min.isr mean?
- What happens if a TiDB leader goes down? How does TiDB use Raft to ensure data security and consistency?
- What happens to long running clickhouse updates if the client dies?
- What happens when all contact point in data-center goes down in cassandra
- What happens with constraints when a view is removed
- What is a good choice of database for a small .NET application?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.