且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

这是什么错误? “数据库查询失败:第1行的列'column_name'的数据被截断

更新时间:2022-06-19 00:28:13

''null不同.如果您的mysql服务器处于严格模式,则它将拒绝执行插入操作,因为您已为该列传递了无效数据.如果不使用严格模式,它将返回警告.

'' 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

要么插入显式的null,要么甚至不指定插入中的列.

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

更新时,您可以发送所有具有的值,因为mysql会自动忽略未更改的值.

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