简体   繁体   English

MySQL 更改表修改列在具有空值的行中失败

[英]MySQL alter table modify column failing at rows with null values

I have a table with about 10K rows, which I am trying to alter so that the field fielddelimiter is never null.我有一个大约有 10K 行的表,我试图对其进行更改,以便字段fielddelimiter永远不会为空。 I am attempting to do an alter statement, expecting any null values to be changed to the default value, but I get an error back from the sql statement.我正在尝试执行一个更改语句,期望将任何空值更改为默认值,但是我从 sql 语句中收到错误消息。

alter table merchant_ftp_account modify column `fielddelimiter` char(1) NOT NULL DEFAULT 't';

17:08:48  [ALTER - 0 row(s), 0.000 secs]  [Error Code: 1265, SQL State: 01000]  Data truncated for column 'fielddelimiter' at row 3987
... 1 statement(s) executed, 0 row(s) affected, exec/fetch time: 0.000/0.000 sec  [0 successful, 0 warnings, 1 errors]

As I understand it this means that the data exceeds the field size at this row, but (a) the data in the field is (null) at that row, and (b) I am able to update that row directly with the value 't', and I don't get a truncation error.据我了解,这意味着该行的数据超过了字段大小,但是 (a) 该行中的字段中的数据为 (null),并且 (b) 我能够直接使用值 ' 更新该行t',我没有收到截断错误。 If I update that row with a nonnull value and try to re-run the alter statement, it fails at the next row where fielddelimiter is null.如果我用非空值更新该行并尝试重新运行alter 语句,它会在fielddelimiter为空的下一行失败。 [ETA: I get that MySQL could update in any direction, but I can actually track its progress as I change rows.] [ETA:我知道 MySQL可以向任何方向更新,但我实际上可以在更改行时跟踪其进度。]

There's a warning in the MySQL docs: MySQL 文档中有一个警告:

Warning This conversion may result in alteration of data. For example, if you shorten a
string column, values may be truncated. To prevent the operation from succeeding if
conversions to the new data type would result in loss of data, enable strict SQL mode
before using ALTER TABLE (see Section 5.1.6, “Server SQL Modes”).

But the values that it's supposedly truncating are nulls.但它应该截断的值是空值。 Can anybody explain to me what is going on here?有人可以向我解释这里发生了什么吗? And how to resolve it?以及如何解决?

[ETA: The existing fielddelimiter field definition is char(1) (allows nulls, no default value), so it should not have values > 1 char, and a select confirms that it does not. [ETA:现有的fielddelimiter字段定义是 char(1)(允许空值,无默认值),因此它的值不应 > 1 个字符,并且选择确认它没有。 The distinct values in the field are NULL, '' (empty string), 'p', 't', and 'y'.]该字段中的不同值为 NULL、''(空字符串)、'p'、't' 和 'y'。]

I have just encountered this error, and it seems the solution was to use the IGNORE statement:我刚刚遇到这个错误,似乎解决方案是使用IGNORE语句:

ALTER IGNORE TABLE `table` CHANGE COLUMN `col` `col` int(11) NOT NULL;

Note that you may still have data truncation issues, so be sure this is the desired result.请注意,您可能仍然存在数据截断问题,因此请确保这是所需的结果。 Using the IGNORE statement it will suppress the data truncated errors for NULL values in columns (and possibly other errors!!!)使用 IGNORE 语句,它将抑制列中 NULL 值的数据截断错误(可能还有其他错误!!!)

If your column has NULL values, you can't alter it to be "NON NULL".如果您的列具有 NULL 值,则不能将其更改为“NON NULL”。 Change the NULL values first to something else, then try it.首先将 NULL 值更改为其他值,然后再尝试。

First remove any null values首先删除任何空值

UPDATE merchant_ftp_account SET fielddelimiter='t' WHERE fielddelimiter IS NULL;

Then然后

ALTER TABLE merchant_ftp_account MODIFY COLUMN `fielddelimiter` char(1) NOT NULL DEFAULT 't';

In my case I was setting the column to NOT NULL在我的情况下,我将列设置为NOT NULL

ALTER TABLE `request_info` 
CHANGE COLUMN `col_2` `col_2` 
VARCHAR(2000) 
NOT NULL -- here was setting it to NULL when the existing col allowed NULL
AFTER `col_1`

when previously I set the column to DEFAULT NULL (ie allow NULL values), so if you want to allow NULL then you can do the following:以前我将列设置为DEFAULT NULL (即允许 NULL 值),所以如果你想允许 NULL 那么你可以执行以下操作:

ALTER TABLE `request_info` 
CHANGE COLUMN `col_2` `col_2` 
VARCHAR(2000) 
DEFAULT NULL -- changed from NOT --> DEFAULT 
AFTER `col_1`

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

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