简体   繁体   English

在MySQL / Sequel Pro中将VARCHAR列转换为INT

[英]Converting VARCHAR column to INT in MySQL / Sequel Pro

I'm trying to set the column in my database from VARCHAR to INT (values are already in DB) 我正在尝试将数据库中的列从VARCHAR设置为INT(值已经在DB中)

When doing this action, the following happens (example): 执行此操作时,会发生以下情况(示例):

| number (varchar) | number (int |
| 20090911913      > 2147483647  |
| 3009092113       > 2147483647  |

Every number is unique (with VARCHAR value), when setting the column to INT all values become the same (2147483647). 每个数字都是唯一的(使用VARCHAR值),当将列设置为INT时,所有值都变为相同(2147483647)。 Note that all values while being VARCHAR do not contain letters (I checked it) 请注意,作为VARCHAR的所有值都不包含字母(我检查过)

Now I don't know why this happens, data comes from a CSV and I checked while importing in Sequel Pro if records have a different number (not being 2147483647). 现在我不知道为什么会发生这种情况,数据来自CSV,如果记录有不同的数字(不是2147483647),我在Sequel Pro中导入时检查。 In the GUI it shows it's different but when all is imported it will automatically become 2147483647. 在GUI中它显示它不同但是当全部导入时它将自动变为2147483647。

What is happening, and how can I prevent this? 发生了什么,我该如何防止这种情况发生?

When you convert numbers that are outside the range of signed integer type Int , which is from -2147483648.. 2147483647 (ie from -2 31 to 2 31 -1), MySql limits the value to the max value that can be stored in an Int , ie 2147483647. 当您转换超出有符号整数类型Int的范围内的数字,即-2147483648 ... 2147483647(即从-2 31到2 31 -1)时,MySql将该值限制为可以存储在一个最大值中的值。 Int ,即2147483647。

Both values that you show are outside the range representable by an Int . 您显示的两个值都超出了Int表示的范围。 You need to use BigInt form them instead. 你需要使用BigInt来代替它们。

Try changing the type of the column to BIGINT , this should solve your problem. 尝试将列的类型更改为BIGINT ,这应该可以解决您的问题。

The issue here is that the number stored in the VARCHAR is bigger then the maximum number that an INTEGER can hold and therefore out of the range. 这里的问题是存储在VARCHAR中的数字大于INTEGER可以容纳的最大数量,因此超出范围。

Here you can find a document explaining all about Numerical types. 在这里,您可以找到解释所有数字类型的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM