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?
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.
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.
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.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With