Drakla wrote:Unless it's really necessary avoid using NULL as it adds bytes to your tables and adds extra processing.
Half correct.
PostgreSQL stores nulls for a row in a single byte for the first eight null columns, and adds a byte for the next eight and so on. So, storage wise, postgreSQL is actually VERY efficient at storing NULLs.
However, the extra processing point is somewhat correct.
Remember, NULL means that a value exists, we just don't know what it is. So, if we have two rows like this:
id color size
0 red NULL
1 NULL big
2 NULL medium
3 blue NULL
Rows 0 and 3 do NOT necessarily have the same size and 1 and 2 do NOT have the same color. They might, but we just don't know.
Now, for something that doesn't apply, you should use something that conveys that, not NULL. Use a different key, like NA. If there is no color or size for an item in that table, then it would make sense that they would "match" on that, since the absence of a value is not the same as a value that is unknown.
Note that you shouldn't "avoid" nulls anymore than you should try to use them a lot. You should simply use them where it's appropriate, and remember that NULL is NOT a value, but an unknown, possibly unknowable value.
learn coalesce and nullif, both pretty standard functions in dbland.