Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL alter table modify column failing at rows with null values

Tags:

mysql

I have a table with about 10K rows, which I am trying to alter so that the field fielddelimiter is never null. 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.

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. 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. [ETA: I get that MySQL could update in any direction, but I can actually track its progress as I change rows.]

There's a warning in the MySQL docs:

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. The distinct values in the field are NULL, '' (empty string), 'p', 't', and 'y'.]

like image 968
barclay Avatar asked Oct 19 '11 21:10

barclay


People also ask

How do I modify a MySQL column to allow NULL?

Here is the syntax to allow NULL value. alter table yourTableName modify column yourColumnName datatype; Apply the above syntax to modify the column to allow NULL. The query is as follows.

How do I change a column from NULL to not null in MySQL?

From the table view, switch to the database structure at the bottom. Select the column you want to modify. Select column_defaul and choose NULL. Remember to hit Cmd + S to commit changes to the server.

Why would you set a column to NOT NULL when creating a MySQL table?

The NOT NULL constraint enforces a column to NOT accept NULL values. This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field.

How do you fix a column that Cannot be NULL?

Using PHPMYADMIN you can go to the table, Clik on Structure and the under ACTIONS edit the column that is giving you trouble. Then select NULL and save: Or if you prefer coding it directly on a query add the following: ALTER TABLE table_name CHANGE column_name column_name type DEFAULT NULL.


2 Answers

I have just encountered this error, and it seems the solution was to use the IGNORE statement:

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!!!)

like image 168
Simon Avatar answered Sep 23 '22 13:09

Simon


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';
like image 27
xordon Avatar answered Sep 26 '22 13:09

xordon