michi
michi

Reputation: 304

How to change TINYINT value NULL to DEFAULT '0'

I want to edit a column in a MySQL table. Right now, it is tinyint with value NULL. I want to change the value to '0'. I tried using this expression:

ALTER TABLE hosts MODIFY COLUMN hide TINYINT NOT NULL DEFAULT 1; and this ALTER TABLE hosts MODIFY COLUMN hide TINYINT DEFAULT 1 NOT NULL; and more variations. But this didn't work for me..

I get this error

EXECUTE FAIL:

ALTER TABLE hosts MODIFY COLUMN hide TINYINT(1) DEFAULT 0 NOT NULL

Message :

  Invalid use of NULL value

What's the problem and what is the correct statement?

Upvotes: 1

Views: 3917

Answers (1)

Thallius
Thallius

Reputation: 2619

You cannot alter the table to „not null“ as long as you have data in it with null value.

First set all you data to 0

Update hosts set hide = 0 where hide is null

Upvotes: 4

Related Questions