Error
Error Code:
1406
MariaDB Error 1406: Data Length Exceeds Column Capacity
Description
This error signifies that an attempt was made to store a value in a database column that is larger than the column's defined maximum length or capacity. It typically occurs during INSERT or UPDATE operations when the data provided for a specific column exceeds its data type limits, preventing the operation from completing successfully.
Error Message
Data too long for column '%s' at row %ld
Known Causes
4 known causesData Exceeds Column Length
The value being inserted or updated is longer than the column's defined maximum length for string types (e.g., VARCHAR, CHAR, TEXT).
Incorrect Data Type Capacity
Attempting to store a numeric value too large for an INT, DECIMAL, or FLOAT column, or a date/time value that doesn't fit its respective type.
Character Set Conversion Issues
When converting data between different character sets, multi-byte characters may require more bytes, causing the data to exceed the column's byte-length limit.
Strict SQL Mode Enforcement
With strict SQL modes enabled, MariaDB explicitly throws this error instead of silently truncating data that is too long for a column.
Solutions
4 solutions available1. Increase Column Length medium
Modify the table schema to accommodate larger data.
1
Identify the table and column causing the error. The error message usually provides this information.
2
Determine the maximum possible length of the data you need to store. Consider the data type and potential future growth.
3
Alter the table to increase the column's length. For `VARCHAR` or `TEXT` types, you can specify a larger length. For `BLOB` or `TEXT` types, you might need to choose a larger variant (e.g., `MEDIUMTEXT` to `LONGTEXT`).
ALTER TABLE your_table_name MODIFY COLUMN your_column_name VARCHAR(255); -- Example for VARCHAR
ALTER TABLE your_table_name MODIFY COLUMN your_column_name LONGTEXT; -- Example for TEXT/BLOB
4
Re-run the operation that caused the error.
2. Truncate Data Before Insertion easy
Shorten the data to fit within the existing column's capacity.
1
Identify the specific data that is too long. This might require inspecting the application code or the data itself.
2
Modify your application code or SQL queries to truncate the data before attempting to insert or update it. Use appropriate string manipulation functions.
INSERT INTO your_table_name (your_column_name) VALUES (LEFT(your_data, 255)); -- Example for VARCHAR
UPDATE your_table_name SET your_column_name = LEFT(your_data, 255) WHERE id = your_id; -- Example for VARCHAR
3
Consider if data loss is acceptable. Truncation means losing information.
3. Use a More Suitable Data Type medium
Switch to a data type that supports larger data storage.
1
Analyze the type of data being stored. If it's text that can be very long, consider `TEXT` variants like `MEDIUMTEXT` or `LONGTEXT` instead of `VARCHAR` with a fixed, insufficient length.
2
If you are storing binary data (images, files), ensure you are using `BLOB` types (`TINYBLOB`, `BLOB`, `MEDIUMBLOB`, `LONGBLOB`) appropriately.
3
Alter the table to change the column's data type to one that can accommodate the required data size.
ALTER TABLE your_table_name MODIFY COLUMN your_column_name MEDIUMTEXT; -- Example for long text
ALTER TABLE your_table_name MODIFY COLUMN your_column_name LONGBLOB; -- Example for large binary data
4
Re-run the operation that caused the error.
4. Normalize Data to a Separate Table advanced
Move overly large data to a related table to keep the primary table lean.
1
If the oversized data is not critical to every record in the primary table, consider creating a new table to store it.
2
Create a new table with a foreign key relationship to the original table. The new table will hold the large data.
CREATE TABLE your_table_name_large_data (
id INT AUTO_INCREMENT PRIMARY KEY,
your_table_id INT,
your_large_column_name TEXT,
FOREIGN KEY (your_table_id) REFERENCES your_table_name(id)
);
3
Modify your application logic to insert the large data into the new table and store only a reference (or null) in the original table.
4
When you need the large data, join the tables. This approach improves performance for queries that don't require the large data.
SELECT t1.*, t2.your_large_column_name FROM your_table_name t1 JOIN your_table_name_large_data t2 ON t1.id = t2.your_table_id;