mysql:如何截断字段的长度 [英] mysql: how to truncate the length of a field

查看:282
本文介绍了mysql:如何截断字段的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Alter table merry_parents change mobile mobile char(10).

执行上述操作时出现错误:

#1265-第2行移动"列的数据被截断了

如何将移动字段截断为char(10)?当前是char(12).

解决方案

该错误告诉您第2行(可能还有其他行)中有12个字符长的数据,因此它停止了alter命令以避免丢失数据.

尝试使用SUBSTRING()更新表以缩短列.目前尚不清楚为什么要这样做,因为会丢失数据,但这会将数据截断为10个字符长:

UPDATE merry_parents SET mobile=SUBSTRING(mobile, 1, 10)

然后运行您的alter命令:

ALTER TABLE merry_parents CHANGE mobile mobile char(10).

Alter table merry_parents change mobile mobile char(10).

When I do the above I'm getting error:

#1265 - Data truncated for column 'mobile' at row 2

How can I truncate my mobile field to char(10)? Currently it is char(12).

解决方案

The error is telling you that there is data 12 characters long in row 2 (and probably others) so it's stopped the alter command to avoid losing data.

Try updating your table using SUBSTRING() to shorten the column. It's unclear why you want to do this as you'll lose data, but this will truncate the data to 10 characters long:

UPDATE merry_parents SET mobile=SUBSTRING(mobile, 1, 10)

Then run your alter command:

ALTER TABLE merry_parents CHANGE mobile mobile char(10).

这篇关于mysql:如何截断字段的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆