Error
Error Code: 79

MySQL Error 79: Invalid Unsigned Integer Input

📦 MySQL
📋

Description

This error occurs when MySQL receives a value that it expects to be an unsigned integer but the provided value is invalid. This can happen when setting configuration parameters, defining column properties, or providing input to functions that require a non-negative whole number.
💬

Error Message

Incorrect unsigned integer value: '%s'.
🔍

Known Causes

3 known causes
⚠️
Negative Value Provided
A configuration setting, system variable, or input parameter requiring a non-negative integer was supplied with a negative number.
⚠️
Non-Numeric Input
A string or other non-numeric character sequence was provided where a numerical unsigned integer was expected.
⚠️
Value Exceeds Data Type Limit
The positive integer value supplied for an option or variable surpasses the maximum allowed for the specific unsigned integer type (e.g., `TINYINT UNSIGNED`, `BIGINT UNSIGNED`).
🛠️

Solutions

4 solutions available

1. Sanitize Input Before Insertion easy

Ensure that values intended for unsigned integer columns are valid integers.

1
Identify the column in your table that is of an unsigned integer type (e.g., `INT UNSIGNED`, `BIGINT UNSIGNED`).
2
Before executing an `INSERT` or `UPDATE` statement, validate that the value you are trying to insert or update is a non-negative integer. If the value comes from user input, this is especially critical.
SELECT column_name FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'your_database_name' AND TABLE_NAME = 'your_table_name' AND COLUMN_TYPE LIKE '%unsigned%';
3
If the input is not a valid unsigned integer (e.g., it's a string like 'abc', a negative number, or an empty string), handle it appropriately. This might involve:
- Returning an error to the user.
- Assigning a default valid value (like 0).
- Attempting to convert the value if it's a numeric string.
4
Example of sanitizing input in application code (Python pseudocode):
try:
    unsigned_value = int(user_input)
    if unsigned_value < 0:
        # Handle negative input, e.g., set to 0 or raise error
        unsigned_value = 0
    # Proceed with INSERT/UPDATE using unsigned_value
except ValueError:
    # Handle non-integer input, e.g., set to 0 or raise error
    unsigned_value = 0

2. Convert String Representations of Numbers easy

Explicitly cast string values that represent numbers to an unsigned integer type.

1
When inserting or updating data, if you have a value that is a string but is intended to be a number, use the `CAST()` or `CONVERT()` function in your SQL statement to ensure it's treated as a numeric type before being assigned to an unsigned integer column.
UPDATE your_table SET unsigned_column = CAST('123' AS UNSIGNED INTEGER) WHERE id = 1;
INSERT INTO your_table (unsigned_column) VALUES (CONVERT('456', UNSIGNED));
2
Be aware that `CAST()` or `CONVERT()` might still fail if the string cannot be interpreted as a number or if it's a negative number. The sanitization step is still recommended for robust error handling.

3. Review Data Types and Constraints medium

Verify that the column type is indeed unsigned integer and check for any triggers or stored procedures that might be manipulating data incorrectly.

1
Examine the table definition to confirm the exact data type of the column causing the error. Ensure it's an unsigned integer type (e.g., `INT UNSIGNED`, `BIGINT UNSIGNED`, `SMALLINT UNSIGNED`, `TINYINT UNSIGNED`).
SHOW CREATE TABLE your_table;
2
Investigate any `BEFORE INSERT` or `BEFORE UPDATE` triggers associated with the table. These triggers might be attempting to insert or modify data in a way that violates the unsigned integer constraint.
SHOW TRIGGERS LIKE 'your_table%';
3
If you have stored procedures or functions that interact with this table, review their logic to ensure they are correctly handling unsigned integer values.

4. Adjust Column to Signed Integer Type advanced

If the application logic inherently deals with potentially negative numbers, change the column type to a signed integer.

1
Before making this change, thoroughly analyze your application's data handling. If there's a legitimate need for negative values, changing the column type is the correct approach.
2
Create a backup of your table or the entire database before proceeding with schema modifications.
mysqldump -u your_user -p your_database your_table > your_table_backup.sql
3
Alter the table to change the data type of the affected column from unsigned to its signed equivalent (e.g., `INT UNSIGNED` to `INT`).
ALTER TABLE your_table MODIFY COLUMN unsigned_column INT;
4
After altering the table, re-test your application thoroughly to ensure data integrity and correct behavior with the new signed integer type.
🔗

Related Errors

5 related errors