I have a table which should not have any NULL values at all. When I set a NOT NULL constraint, it disallows the statement and it fails with the constraint error. Default constraint will take place only if the column is not referenced in the insert statement.
How can we get around this? If a insert statement has a NULL value for any of the columns, then the DEFAULT value must be taken instead of the NULL values.
create table temp1 (col0 int, col1 int default 0);
insert into temp1 (col0) values (1); --> col0 -> 1 and col1 ->0
insert into temp1 (col0,col1) values (1,NULL); --> col0 -> 1 and col1 -> NULL (I would expect a 0 here instead of NULL)
alter table temp1 (add column col2 int not null default 0); --> col2 -> 0 for all the existing rows
insert into temp1 (col0) values (2); --> col0 -> 2 and col1 ->0 and col2-> 0
select * from temp1;
COL0 |COL1 |COL2
1 |0 |0
1 |(null) |0
2 |0 |0