What is this error? "Database query failed: Data truncated for column 'column_name' at row 1

前端 未结 3 914
别跟我提以往
别跟我提以往 2021-02-05 04:12

I\'m building a PHP/MySQL application and I\'m running into a problem with my create and update query. I have 5 columns that are set to type FLOAT that are also set as NULL colu

3条回答
  •  庸人自扰
    2021-02-05 04:33

    '' and null are not the same. if your mysql server is in strict mode, then it will refuse to do the insert since you have passed invalid data for the column. without strict mode, it returns a warning.

    mysql> create table a (a float not null);
    Query OK, 0 rows affected (0.11 sec)
    
    mysql> insert a values ('');
    Query OK, 1 row affected, 1 warning (0.05 sec)
    
    mysql> show warnings;
    +---------+------+----------------------------------------+
    | Level   | Code | Message                                |
    +---------+------+----------------------------------------+
    | Warning | 1265 | Data truncated for column 'a' at row 1 |
    +---------+------+----------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> set sql_mode = 'STRICT_ALL_TABLES';
    Query OK, 0 rows affected (0.02 sec)
    
    mysql> insert a values ('');
    ERROR 1265 (01000): Data truncated for column 'a' at row 1
    

    either insert explicit nulls, or don't even specify the column in the insert.

    when you're updating you can send all of the values you have because mysql will automatically ignore the unchanged ones.

提交回复
热议问题