简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM