Error Code: 1406. Data too long for column - MySQL
MySQL will truncate any insert value that exceeds the specified column width.
to make this without error try switch your SQL mode
to not use STRICT
.
Mysql reference manual
EDIT:
To change the mode
This can be done in two ways:
- Open your
my.ini
(Windows) ormy.cnf
(Unix) file within the MySQL installation directory, and look for the text "sql-mode".
Find:
Code:
# Set the SQL mode to strict
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
Replace with:
Code:
# Set the SQL mode to strict
sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
Or
- You can run an SQL query within your database management tool, such as phpMyAdmin:
Code:
SET @@global.sql_mode= '';
This happened to me recently. I was fully migrate to MySQL 5.7, and everything is in default configuration.
All previously answers are already clear and I just want to add something.
This 1406 error could happen in your function / procedure too and not only to your table's column length.
In my case, I've trigger which call procedure with IN parameter varchar(16) but received 32 length value.
I hope this help someone with similar problem.
I think that switching off the STRICT mode is not a good option because the app can start losing the data entered by users.
If you receive values for the TESTcol from an app you could add model validation, like in Rails
validates :TESTcol, length: { maximum: 45 }
If you manipulate with values in SQL script you could truncate the string with the SUBSTRING command
INSERT INTO TEST
VALUES
(
1,
SUBSTRING('Vikas Kumar Gupta Kratika Shukla Kritika Shukla', 0, 45)
);
This is a step I use with ubuntu. It will allow you to insert more than 45 characters from your input but MySQL will cut your text to 45 characters to insert into the database.
-
Run command
sudo nano /etc/mysql/my.cnf
-
Then paste this code
[mysqld] sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
-
restart MySQL
sudo service mysql restart;