MySQL too long varchar truncation/error setting

后端 未结 3 1405
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 10:06

I have two MySQL instances. The 1st one truncates strings on insert when data is too long. The 2nd one raises an error:

ERROR 1406 (22001): Data too long for c

相关标签:
3条回答
  • 2020-11-28 10:13

    If strict SQL mode is not enabled and you assign a value to a CHAR or VARCHAR column that exceeds the column's maximum length, the value is truncated to fit and a warning is generated. For truncation of nonspace characters, you can cause an error to occur (rather than a warning) and suppress insertion of the value by using strict SQL mode. See Section 6.1.7, “Server SQL Modes”.

    How you can change it: http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html


    Found two ways to disable strict mode:

    1. add below to my.cnf

      sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

    2. way is using mysql console.

      SET @@global.sql_mode= '';

    Please test them before running on production environment.

    0 讨论(0)
  • 2020-11-28 10:13

    if you use cpanel ,

    replace

    sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
    

    into /usr/my.cnf

    to

    sql-mode=""
    

    run

    /etc/init.d/mysql restart
    
    0 讨论(0)
  • 2020-11-28 10:16

    You can disable STRICT_TRANS_TABLES and STRICT_ALL_TABLES. This allows the automatic truncation of the inserted string.

    Quote from MySQL Documantation.

    Strict mode controls how MySQL handles invalid or missing values in data-change statements such as INSERT or UPDATE. A value can be invalid for several reasons. For example, it might have the wrong data type for the column, or it might be out of range. A value is missing when a new row to be inserted does not contain a value for a non-NULL column that has no explicit DEFAULT clause in its definition. (For a NULL column, NULL is inserted if the value is missing.)

    Reference: MySQL Server SQL Modes

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