Unsigned integer vs signed integer in MySQL 8: Explained with examples

Updated: January 26, 2024 By: Guest Contributor Post a comment

Introduction

When designing a database with MySQL, one of the key decisions developers need to make is whether to use signed or unsigned integers for their numeric columns. This choice can have implications for both the data integrity of the application and the database performance. In this article, we’ll delve into the differences between unsigned and signed integers in MySQL 8, providing code examples to illuminate the concepts.

Understanding Signed and Unsigned Integers

Integers, the basic data type representing whole numbers, can be either signed or unsigned in MySQL. A signed integer can hold both positive and negative values, including zero, while an unsigned integer can only hold non-negative values (zero or positive numbers).

In MySQL 8, when you define a column as INT without specifying signed or unsigned, it defaults to signed. Here’s an example:

CREATE TABLE example_signed (
    id INT,
    value INT
);

To create an unsigned integer column, you explicitly add the UNSIGNED attribute:

CREATE TABLE example_unsigned (
    id INT,
    value INT UNSIGNED
);

Comparing Ranges

The most significant difference between signed and unsigned integers is their range. For example, a signed INT in MySQL can store numbers from -2,147,483,648 to 2,147,483,647, whereas an unsigned INT can store from 0 to 4,294,967,295.

Choosing Between Signed and Unsigned

Whether you should use signed or unsigned integers depends on the nature of the data you wish to store. If you need to accommodate negative numbers, a signed integer is necessary. Unsigned integers are ideal for data that will only ever be positive, such as age, quantity, or other counting numbers. Here are some examples:

CREATE TABLE people (
    age TINYINT UNSIGNED
);

CREATE TABLE inventory (
    quantity SMALLINT UNSIGNED
);

Inserting Data

When inserting data into these columns, attempting to store a number outside of the permissible range results in an error. Consider the following example:

INSERT INTO example_unsigned (id, value) VALUES (1, -10);

This will throw an error since -10 is outside of the range for an unsigned integer. The same insertion on a signed integer table would be successful:

INSERT INTO example_signed (id, value) VALUES (1, -10);

Practical Implementation

Let’s look at a practical example by setting up a scenario. Suppose we are managing a user system where we are certain a user’s ID will always be positive.

CREATE TABLE users (
    user_id MEDIUMINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(50) NOT NULL
);

Here, user_id is set as an UNSIGNED MEDIUMINT. Since it’s also a primary key which auto-increments, there’s no need for it to store negative values, making the unsigned attribute appropriate.

Handling Overflow

An important issue to consider when using unsigned integers is the possibility of overflow. If a value exceeds the maximum value for the data type, it doesn’t wrap around to zero – instead, an error occurs. When dealing with large counts or sums, it’s crucial to choose an appropriate data size or handle possible overflows. For example:

CREATE TABLE big_numbers (
    huge_number BIGINT UNSIGNED
);

INSERT INTO big_numbers (huge_number) VALUES (18446744073709551615);
UPDATE big_numbers SET huge_number = huge_number + 1 WHERE huge_number = 18446744073709551615;

The UPDATE query will result in an error as it tries to increment a value that’s already at its maximum for an UNSIGNED BIGINT.

Best Practices

Adopt the following best practices when using signed and unsigned integers:

  • Assess the range of data that will be stored in the integer field and decide if negative values will ever be needed.
  • For primary keys and fields representing counts, sizes, or other inherently non-negative attributes use UNSIGNED integers to maximize the range.
  • Be mindful of the potential risks of overflow and carefully manage arithmetic operations on unsigned integers.
  • When you need to ensure cross-platform compatibility or if there’s considerable interaction with signed integers, stick to signed integers to avoid conversion issues.

Conclusion

In this tutorial, we have clarified the differences between unsigned and signed integers in MySQL 8 and explored the implications for database design. Remember to choose wisely based on the data your tables will store, and consider not just the current requirements but also future expansion. Proactive planning and understanding the intricacies of your data types can help you avoid unnecessary hitches and optimize your database’s efficiency and reliability.