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

前端 未结 8 1506
抹茶落季
抹茶落季 2020-11-27 05:26

Error Code: 1406. Data too long for column

CREATE  TABLE `TEST` 
(

  `idTEST` INT NOT NULL ,

  `TESTcol` VARCHAR(45) NULL ,

  PRIMARY KEY (`idTEST`) 
);
         


        
相关标签:
8条回答
  • 2020-11-27 05:45

    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.

    0 讨论(0)
  • 2020-11-27 05:52

    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:

    1. Open your my.ini (Windows) or my.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

    1. You can run an SQL query within your database management tool, such as phpMyAdmin:

    Code:

    SET @@global.sql_mode= '';
    
    0 讨论(0)
  • 2020-11-27 05:56

    Besides the answer given above, I just want to add that this error can also occur while importing data with incorrect lines terminated character.

    For example I save the dump file in csv format in windows. then while importing

    LOAD DATA INFILE '/path_to_csv_folder/db.csv' INTO TABLE table1
     FIELDS TERMINATED BY ',' 
     ENCLOSED BY '"'
     ESCAPED BY '"'
     LINES TERMINATED BY '\n'
    IGNORE 1 LINES;
    

    Windows saved end of line as \r\n (i.e. CF LF) where as I was using \n. I was getting crazy why phpMyAdmin was able to import the file while I couldn't. Only when I open the file in notepadd++ and saw the end of file then I realized that mysql was unable to find any lines terminated symbol (and I guess it consider all the lines as input to the field; making it complain.)

    Anyway after making from \n to \r\n; it work like a charm.

    LOAD DATA INFILE '/path_to_csv_folder/db.csv' INTO TABLE table1
     FIELDS TERMINATED BY ',' 
     ENCLOSED BY '"'
     ESCAPED BY '"'
     LINES TERMINATED BY '\r\n'
    IGNORE 1 LINES;
    
    0 讨论(0)
  • 2020-11-27 05:56

    Go to your Models and check, because you might have truncated a number of words for that particular column eg. max_length="150".

    0 讨论(0)
  • 2020-11-27 05:59

    I got the same error while using the imagefield in Django. post_picture = models.ImageField(upload_to='home2/khamulat/mydomain.com/static/assets/images/uploads/blog/%Y/%m/%d', height_field=None, default=None, width_field=None, max_length=None)

    I just removed the excess code as shown above to post_picture = models.ImageField(upload_to='images/uploads/blog/%Y/%m/%d', height_field=None, default=None, width_field=None, max_length=None) and the error was gone

    0 讨论(0)
  • 2020-11-27 06:03

    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.

    1. Run command

      sudo nano /etc/mysql/my.cnf

    2. Then paste this code

      [mysqld] sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

    3. restart MySQL

      sudo service mysql restart;

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