MySQL 错误 1406:列数据过长 的修复方法(但它不应该过长?)
如果尝试设置的数据超过允许的限制,则可能发生此错误。例如,您无法将字符串存储在 bit 类型的列中,因为 varchar 或字符串占用的大小超过 bit 数据类型。
您需要对 bit 类型列使用以下语法
anyBitColumnName= b ‘1’ OR anyBitColumnName= b ‘0’
为了理解上述语法,让我们创建一个表。创建表的查询如下所示
mysql> create table IncasesensitiveDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> Name varchar(10), -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.70 sec)
使用 insert 命令在表中插入一些记录。插入记录的查询如下所示
mysql> insert into ErrorDemo(Name,isStudent) values('John',1); Query OK, 1 row affected (0.18 sec) mysql> insert into ErrorDemo(Name,isStudent) values('Sam',0); Query OK, 1 row affected (0.21 sec) mysql> insert into ErrorDemo(Name,isStudent) values('Mike',0); Query OK, 1 row affected (0.16 sec) mysql> insert into ErrorDemo(Name,isStudent) values('Larry',1); Query OK, 1 row affected (0.23 sec) mysql> insert into ErrorDemo(Name,isStudent) values('Carol',1); Query OK, 1 row affected (0.11 sec) mysql> insert into ErrorDemo(Name,isStudent) values('Robert',0); Query OK, 1 row affected (0.17 sec) mysql> insert into ErrorDemo(Name,isStudent) values('James',1); Query OK, 1 row affected (0.18 sec) mysql> insert into ErrorDemo(Name,isStudent) values('Bob',1); Query OK, 1 row affected (0.19 sec) mysql> insert into ErrorDemo(Name,isStudent) values('David',1); Query OK, 1 row affected (0.15 sec) mysql> insert into ErrorDemo(Name,isStudent) values('Ricky',0); Query OK, 1 row affected (0.17 sec)
使用 select 语句显示表中的所有记录。查询如下所示
mysql> select *from ErrorDemo;
以下是输出结果
+----+--------+-----------+ | Id | Name | isStudent | +----+--------+-----------+ | 1 | John | | | 2 | Sam | | | 3 | Mike | | | 4 | Larry | | | 5 | Carol | | | 6 | Robert | | | 7 | James | | | 8 | Bob | | | 9 | David | | | 10 | Ricky | | +----+--------+-----------+ 10 rows in set (0.00 sec)
实际的示例输出快照如下所示
如上所述,错误如下。它在下面的查询中生成
mysql> update ErrorDemo set isStudent='1' where Id=9; ERROR 1406 (22001): Data too long for column 'isStudent' at row 1
要避免上述错误,您需要在“1”之前添加前缀 b。现在查询如下所示
mysql> update ErrorDemo set isStudent=b'1' where Id=9; Query OK, 0 rows affected (0.00 sec) Rows matched: 1 Changed: 0 Warnings: 0
使用 select 语句再次检查表记录。查询如下所示
mysql> select *from ErrorDemo;
以下是输出结果
+----+--------+-----------+ | Id | Name | isStudent | +----+--------+-----------+ | 1 | John | | | 2 | Sam | | | 3 | Mike | | | 4 | Larry | | | 5 | Carol | | | 6 | Robert | | | 7 | James | | | 8 | Bob | | | 9 | David | | | 10 | Ricky | | +----+--------+-----------+ 10 rows in set (0.00 sec)
实际的示例输出快照如下所示
查看 is Student 列。
现在,我们将使用值 0 更新相同的 ID。这将为相应的 ID 提供一个空白值。查询如下所示
mysql> update ErrorDemo set Name='Maxwell', isStudent=b'0' where Id=9; Query OK, 1 row affected (0.16 sec) Rows matched: 1 Changed: 1 Warnings: 0
检查上面更新的特定行的记录。这是 ID 9。现在行已使用记录 ID 9 更新。查询如下所示
mysql> select *from ErrorDemo where Id=9;
以下是输出结果
+----+---------+-----------+ | Id | Name | isStudent | +----+---------+-----------+ | 9 | Maxwell | | +----+---------+-----------+ 1 row in set (0.00 sec)
广告