Error Code: 1406 - Data Truncation in MySQL
The "Error Code: 1406. Data too long for column" occurs when attempting to insert data into a MySQL table where the data exceeds the maximum length defined for the column's data type.
In the example provided, the TESTcol column has a data type of VARCHAR(45) and we are trying to insert a value that is 47 characters long. This exceeds the column's width of 45 characters.
Solution
To resolve this issue, ensure that the data being inserted does not exceed the maximum length allowed for the column's data type. In this case, we need to truncate the data to 45 characters.
Alternative Approach
While MySQL truncates data by default, it is possible to disable this behavior by changing the SQL mode to not use STRICT. This can be done by modifying the my.ini file or running the following SQL query:
SET @@global.sql_mode= '';
However, disabling STRICT mode is not recommended as it can lead to potential data integrity issues. Instead, it is better to ensure that the data being inserted adheres to the defined column widths.
The above is the detailed content of How to Resolve MySQL Error Code 1406: Data Truncation?. For more information, please follow other related articles on the PHP Chinese website!