105

I've written a MySQL script to create a database for hypothetical hospital records and populate it with data. One of the tables, Department, has a column named Description, which is declared as type varchar(200). When executing the INSERT command for Description I get an error:

error 1406: Data too long for column 'Description' at row 1.

All the strings I'm inserting are less than 150 characters.

Here's the declaration:

CREATE TABLE Department(
    ...
    Description varchar(200)
    ...);

And here's the insertion command:

INSERT INTO Department VALUES
(..., 'There is some text here',...), (..., 'There is some more text over here',...);

By all appearances, this should be working. Anyone have some insight?

Raging Bull
  • 18,593
  • 13
  • 50
  • 55
Ben C.
  • 1,761
  • 5
  • 15
  • 24

16 Answers16

121

Change column type to LONGTEXT

Alexander Serkin
  • 1,679
  • 1
  • 12
  • 11
  • 5
    should be the right answer - instead masking away the issue by disabling strict, rather fixing the cause – Eugen Mayer May 30 '18 at 11:57
  • 2
    Additional Info: If the text you want to store in the database is longer than 65535 characters, you have to choose MEDIUMTEXT or LONGTEXT, but be careful: MEDIUMTEXT stores strings up to 16 MB, LONGTEXT up to 4 GB. ;) – subodhkalika Feb 25 '21 at 05:24
60

I had a similar problem when migrating an old database to a new version.

Switch the MySQL mode to not use STRICT.

SET @@global.sql_mode= 'NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

Error Code: 1406. Data too long for column - MySQL

Community
  • 1
  • 1
calraiden
  • 1,686
  • 1
  • 27
  • 37
  • 1
    This has suited well and solved a similar problem I was facing today. Just out of curiosity...are there any drawbacks with this solution? – Ravi Gaurav Pandey May 05 '18 at 08:36
  • It depends on your code, because strict mode is an extra control of mysql to avoid inserting invalid data. If your code works fine in the previous version of mysql, you will dont have problem in the new version. The long explantion: https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html#sql-mode-strict – calraiden May 06 '18 at 19:08
19

There is an hard limit on how much data can be stored in a single row of a mysql table, regardless of the number of columns or the individual column length.

As stated in the OFFICIAL DOCUMENTATION

The maximum row size constrains the number (and possibly size) of columns because the total length of all columns cannot exceed this size. For example, utf8 characters require up to three bytes per character, so for a CHAR(255) CHARACTER SET utf8 column, the server must allocate 255 × 3 = 765 bytes per value. Consequently, a table cannot contain more than 65,535 / 765 = 85 such columns.

Storage for variable-length columns includes length bytes, which are assessed against the row size. For example, a VARCHAR(255) CHARACTER SET utf8 column takes two bytes to store the length of the value, so each value can take up to 767 bytes.

Here you can find INNODB TABLES LIMITATIONS

Community
  • 1
  • 1
STT LCU
  • 4,348
  • 4
  • 29
  • 47
11

in mysql if you take VARCHAR then change it to TEXT bcoz its size is 65,535 and if you can already take TEXT the change it with LONGTEXT only if u need more then 65,535.

total size of LONGTEXT is 4,294,967,295 characters

Krishna Jangid
  • 4,961
  • 5
  • 27
  • 33
9

Varchar has its own limits. Maybe try changing datatype to text.!

Jayesh Amin
  • 314
  • 2
  • 12
9

Turns out, as is often the case, it was a stupid error on my part. The way I was testing this, I wasn't rebuilding the Department table after changing the data type from varchar(50) to varchar(200); I was just re-running the insert command, still with the column as varchar(50).

Ben C.
  • 1,761
  • 5
  • 15
  • 24
7

For me, I defined column type as BIT (e.g. "boolean")

When I tried to set column value "1" via UI (Workbench), I was getting a "Data too long for column" error.

Turns out that there is a special syntax for setting BIT values, which is:

b'1'
Illidan
  • 4,047
  • 3
  • 39
  • 49
3

If your source data is larger than your target field and you just want to cut off any extra characters, but you don't want to turn off strict mode or change the target field's size, then just cut the data down to the size you need with LEFT(field_name,size).

INSERT INTO Department VALUES
(..., LEFT('There is some text here',30),...), (..., LEFT('There is some more text over here',30),...);

I used "30" as an example of your target field's size.

In some of my code, it's easy to get the target field's size and do this. But if your code makes that hard, then go with one of the other answers.

Buttle Butkus
  • 9,206
  • 13
  • 79
  • 120
2

With Hibernate you can create your own UserType. So thats what I did for this issue. Something as simple as this:

    public class BytesType implements org.hibernate.usertype.UserType {

         private final int[] SQL_TYPES = new int[] { java.sql.Types.VARBINARY };
     //...
    }

There of course is more to implement from extending your own UserType but I just wanted to throw that out there for anyone looking for other methods.

simbabque
  • 53,749
  • 8
  • 73
  • 136
hugh
  • 37
  • 1
2

Very old question, but I tried everything suggested above and still could not get it resolved.

Turns out that, I had after insert/update trigger for the main table which tracked the changes by inserting the record in history table having similar structure. I increased the size in the main table column but forgot to change the size of history table column and that created the problem.

I did similar changes in the other table and error is gone.

rahimv
  • 541
  • 4
  • 12
  • 1
    Ha! My problem too. Forgot I had [`simple_history`](https://django-simple-history.readthedocs.io/en/latest/) on that Django model. – Ross Rogers Apr 21 '20 at 23:29
1

I try to create a table with a field as 200 characters and I've added two rows with early 160 characters and it's OK. Are you sure your rows are less than 200 characters?

Show SqlFiddle

Joe Taras
  • 15,166
  • 7
  • 42
  • 55
1

There was a similar problem when storing a hashed password into a table. Changing the maximum column length didn't help. Everything turned out to be simple. It was necessary to delete the previously created table from the database, and then test the code with new values ​​of the allowable length.

vollander
  • 11
  • 1
1

If you re using type: DataTypes.STRING, then just pass how long this string can be like DataTypes.STRING(1000)

0

In my case this error occurred due to entering data a wrong type for example: if it is a long type column, i tried to enter in string type. so please check your data that you are entering and type are same or not

0

For me, I try to update column type "boolean" value When I tried to set column value 1 MySQL Workbench, I was getting a "Data too long for column" error.

So for that there is a special syntax for setting boolean values, which is:

   UPDATE `DBNAME`.`TABLE_NAME` SET `FIELD_NAME` = false WHERE (`ID` = 'ID_VALUE');   //false for 0

   UPDATE `DBNAME`.`TABLE_NAME` SET `FIELD_NAME` = true WHERE (`ID` = 'ID_VALUE');  //true for 1
0

I had a different problem which gave the same error so I'll make a quick recap as this seems to have quite different sources and the error does not help much to track down the root cause.

Common sources for INSERT / UPDATE

Size of value in row

This is exactly what the error is complaining about. Maybe it's just that. You can:

  • increase the column size: for long strings you can try to use TEXT, MEDIUMTEXT or LONGTEXT
  • trim the value that is too long: you can use tools from the language you're using to build the query or directly in SQL with LEFT(value,size) or RIGHT(...) or SUBSTRING(...)

Beware that there is a maximum row size in a MySQL table as reported by this answer. Check documentation and InnoDB engine limitations.

Datatype Mismatch

One or more rows are of the wrong datatype. common sources of error are

  • ENUM
  • BIT: don't use 1 but b'1'

Data outlier

In a long list of insert one can easily miss a row which has a field not adhering to the column typing, like an ENUM generated from a string.

Python Django

Check if you have sample_history enabled, after a change in a column size it must be updated too.

Guglie
  • 2,121
  • 1
  • 24
  • 43