HiveBrain v1.2.0
Get Started
← Back to all entries
debugsqlModerate

Cannot alter MYSQL Table (stuck in a invalid default value loop)

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
cannotstuckloopvaluemysqldefaultinvalidaltertable

Problem

Some how I have 2 columns with incorrect default values.
Not sure how it got created in the first place as I imported the db from another source.

Problem now is, I cannot make any changes to the table cause MYSQL will complain:


MySQL said: Invalid default value for 'campaign_start_date'

if I tried adding a column.

But when I try changing the DEFAULT VALUE to CURRENT_TIMESTAMP for campaign_start_date, it complains campaign_end_date has an invalid default value.

So I'm stuck in a loop that doesn't allow me to make any changes to the table.

Is there anyway to fix this?

Solution

Try changing both columns defaults at the same time:

ALTER TABLE yourtable 
    CHANGE campaign_start_date campaign_start_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CHANGE campaign_end_date campaign_end_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;

Code Snippets

ALTER TABLE yourtable 
    CHANGE campaign_start_date campaign_start_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CHANGE campaign_end_date campaign_end_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;

Context

StackExchange Database Administrators Q#153237, answer score: 14

Revisions (0)

No revisions yet.