MySQL Alter table导致错误:无效使用NULL值

[英]MySQL Alter table causes Error: Invalid use of NULL value


My existing table:

我的现有表:

+-----------------+---------------+------+-----+---------+-------------------+
| Field           | Type          | Null | Key | Default | Extra             |
+-----------------+---------------+------+-----+---------+-------------------+
| creation_date   | timestamp     | YES  |     | NULL                        |

I wanted to alter table like this:

我想这样改变桌子:

ALTER TABLE enterprise MODIFY creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;

But I got this error:

但我犯了一个错误:

ERROR 1138 (22004) at line 7: Invalid use of NULL value

第7行错误1138(22004):无效使用NULL值

The problem looks like from changing the Nullable which was YES to NOT NULL. Do I need to drop the column and add afterwards?

问题看起来是这样的:将可空的YES改为NOT NULL。我需要删除列然后添加吗?

2 个解决方案

#1


66  

It looks like there are few rows with NULL value.Update all null values to a default date in that column and then try to do a alter.

看起来几乎没有空值的行。将该列中的所有空值更新为默认日期,然后尝试进行更改。

Try this

试试这个

--update null value rows
UPDATE enterprise
SET creation_date = CURRENT_TIMESTAMP
WHERE creation_date IS NULL;


ALTER TABLE enterprise 
MODIFY creation_date TIMESTAMP NOT NULL 
DEFAULT CURRENT_TIMESTAMP;

#2


2  

You can't use this query until you have null values in creation_date column.

在creation_date列中有空值之前,不能使用该查询。

Update your creation_date column with some default date, then alter the table.

使用默认日期更新creation_date列,然后修改表。

Like this

像这样

UPDATE enterprise SET creation_date = CURRENT_TIMESTAMP WHERE creation_date IS NULL;

ALTER TABLE enterprise MODIFY creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2014/04/09/7df104f2eab3007e0e18d22391d6e1d4.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告