MySQL尝试使用大整数创建临时表失败

时间:2016-01-23 15:06:59

标签: mysql

我正在尝试创建一个临时表,将原始表中的char(10)数字tkey存储为无符号整数。 遇到第109516行时失败:

mysql> CREATE TEMPORARY TABLE IF NOT EXISTS lutemp AS (SELECT cast(tkey as unsigned),ca01,ce01 FROM lu);
ERROR 1264 (22003): Out of range value for column 'cast(tkey as unsigned)' at row 109516

但是如果我选择该行作为强制转换为整数,那么它就没有问题:

mysql> select cast(tkey as unsigned) from lu limit 109515,1;


+--------------------------+
| cast(tkey as unsigned) |
+--------------------------+
|               4298001000 |

+--------------------------+
1 rows in set (0.10 sec)

    

我做错了什么?

1 个答案:

答案 0 :(得分:1)

不要依赖数据库从BIGINT为您提供CAST(... UNSIGNED)

UNSIGNED INT的最大值是

4,294,967,295

here。你的价值是

4,298,001,000

尝试使用BIGINT UNSIGNED显式:

CREATE TEMPORARY TABLE IF NOT EXISTS lutemp
(SELECT tkey, ca01,ce01 FROM lu LIMIT 0);

ALTER TABLE lutemp MODIFY tkey BIGINT UNSIGNED;

INSERT INTO lutemp (tkey, ca01,ce01)
SELECT tkey, ca01,ce01 FROM lu
;