MySQL Forums
Forum List  »  General

Re: Add Not null column to existing table
Posted by: Peter Brawley
Date: January 30, 2014 10:08PM

drop table t;
create table t(i int,j int);
insert into t values(1,null);
select * from t;
+------+------+
| i    | j    |
+------+------+
|    1 | NULL |
+------+------+
alter table t modify column j int not null;
ERROR 1138 (22004): Invalid use of NULL value
alter table t add column k int not null;
select * from t;
+------+------+---+
| i    | j    | k |
+------+------+---+
|    1 | NULL | 0 |
+------+------+---+

MySQL will not let you set a nullable column not null if null values for it exist. It will let you add a not null column and set missing values to the assumed default 0 or whatever default you specify, which is the sounder approach.

Options: ReplyQuote


Subject
Written By
Posted
Re: Add Not null column to existing table
January 30, 2014 10:08PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.