Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Alter table causes Error: Invalid use of NULL value

Tags:

mysql

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

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

like image 320
Daniel Qiu Avatar asked Apr 09 '14 18:04

Daniel Qiu


People also ask

How do you fix invalid use of NULL?

Error 94: "Invalid Use of Null" Follow This means there is an empty field in a database or it is not in the expected format. This can be caused by database corruption. This error is fixed by searching the database to delete the blank or incorrect field or by correcting the database all together.

How do I modify a MySQL column to allow NULL?

alter table yourTableName modify column yourColumnName datatype; Apply the above syntax to modify the column to allow NULL. The query is as follows. After executing the above query, you can insert NULL value to that column because the column is modified successfully above.

Can we use NULL in MySQL?

Conceptually, NULL means “a missing unknown value” and it is treated somewhat differently from other values. Because the result of any arithmetic comparison with NULL is also NULL , you cannot obtain any meaningful results from such comparisons. In MySQL, 0 or NULL means false and anything else means true.

Why do we use not null in MySQL?

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.


1 Answers

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; 
like image 64
rs. Avatar answered Sep 24 '22 04:09

rs.