MySQL alter table modify column failing at rows with null values

前端 未结 4 1870
野的像风
野的像风 2021-02-20 03:39

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 a

相关标签:
4条回答
  • 2021-02-20 04:12

    If your column has NULL values, you can't alter it to be "NON NULL". Change the NULL values first to something else, then try it.

    0 讨论(0)
  • 2021-02-20 04:14

    In my case I was setting the column to 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 (i.e. allow NULL values), so if you want to allow NULL then you can do the following:

    ALTER TABLE `request_info` 
    CHANGE COLUMN `col_2` `col_2` 
    VARCHAR(2000) 
    DEFAULT NULL -- changed from NOT --> DEFAULT 
    AFTER `col_1`
    
    0 讨论(0)
  • 2021-02-20 04:28

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

    0 讨论(0)
  • 2021-02-20 04:30

    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';
    
    0 讨论(0)
提交回复
热议问题