简体   繁体   中英

NULL values in MySQL database after I set default '0' to the column

I have the payouts table to whom I set the default as 0.000 for my total_tips column (extract from my table schema):

total_tips       | decimal(12, 4) | YES  |      | 0.0000 | <- Default set as '0.0000'

But now, can anyone explain why I have still NULL values inside my table:

mysql> select total_tips from payouts where id = 4157;
+------------+
| total_tips |
+------------+
|       NULL |
+------------+
1 row in set (0.00 sec)

Before that I ran a ALTER command which look like this (to set the default value):

mysql> ALTER TABLE payouts change total_tips total_tips decimal(12,4) default 0 ;

You maybe didn't set the value as NOT NULL , so the NULL value is accepted. You need to update your whole base with:

UPDATE payouts SET total_tips = 0 WHERE total_tips IS NULL

if you set defaults on existing columns in tables which already stores data then the existing rows are not automatically updated.

i mean this is only true for mssql and mysql. in oracle existing lines WILL BE updated.

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