Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql update increment int field that is null

I have a very large table with two INT columns that are null on Default. This is a problem because since they are INT fields, it would help in many cases if they were originally set to 0.

So my questions are, is there a way I can UPDATE and INCREMENT(+1) these fields while they are like this (null on Default)? BTW.. I didn't have luck so far, it seems increment only works when the default=0

..or is my only option to Change the Default to none from null

like image 331
sa511 Avatar asked Mar 20 '11 07:03

sa511


People also ask

Can int be NULL in SQL?

You can insert NULL value into an int column with a condition i.e. the column must not have NOT NULL constraints.

How do I increment in MySQL?

Syntax for MySQLMySQL uses the AUTO_INCREMENT keyword to perform an auto-increment feature. By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record. VALUES ('Lars','Monsen'); The SQL statement above would insert a new record into the "Persons" table.

Can int column be NULL in MySQL?

But the short answer is: yes, int columns can have NULL values.


1 Answers

UPDATE TableName SET column = IFNULL(column, 0) + 1 WHERE ...

More info on IFNULL. It returns the first argument if it is not NULL, the second otherwise.

like image 106
Charles Avatar answered Oct 19 '22 23:10

Charles