“Data too long for column” - why?

前端 未结 12 677
Happy的楠姐
Happy的楠姐 2020-12-12 19:47

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,

相关标签:
12条回答
  • 2020-12-12 20:11

    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.

    0 讨论(0)
  • 2020-12-12 20:13

    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).

    0 讨论(0)
  • 2020-12-12 20:14

    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 讨论(0)
  • 2020-12-12 20:19

    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

    0 讨论(0)
  • 2020-12-12 20:20

    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

    0 讨论(0)
  • 2020-12-12 20:20

    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.

    0 讨论(0)
提交回复
热议问题