4 ms·
First example: why would you want to handle null and a zero length string differently in that case? That seems a little contrived to me because in both instance
by pushpop 7y ago
First example: why would you want to handle null and a zero length string differently in that case? That seems a little contrived to me because in both instances the user has elected not to enter that information.
Re your timestamp example, just compare if created is the same as updated. That’s a really easy problem to solve. Though I have seen other scenarios when a timestamp needed to by nullable so I agree there will be edge cases.
I agree that null does have its uses (it’s basically essential for outer joins) but most of the time people think they need null they don’t actually need it. Plus it frequently it causes hidden faults, so it’s usually best to avoid allowing nullable fields unless you’re sure there isn’t any other easy way.
- marcc 7y agoThe tool referenced here is for go, a programming language that has pointers. Pointers can be nil too, which is useful for many of the same reasons that null database columns are useful. There is a difference between uninitialized, initialized with default values, and a custom value. You may not have this use case you in your application, but I do. I have real scenarios that need to know if a value is initialized or not. I agree that I could solve this in other ways, but nullable columns just aren’t an archaic database option just because you can implement the same behavior in the application code or by rewriting the SQL query.
- pushpop 7y agoI’ve had a great deal of experience with Go and there are many who’d argue that nil pointers in there is a mistake as well (though I’ve personally never had an issue - but I do write pretty robust tests too). I have ran into some instances when I have needed nullable fields in relational databases as well. I’m not claiming null shouldn’t exist. My point was that they should be an opt in rather than opt out because they cause more problems than generally they solve.
- pxue 7y agoRegarding timestamp, I often have a soft delete timestamp 'deletedAt' in which it must have a null default value. I think this is a perfectly acceptable usecase of db null values
- pushpop 7y agoIt is though I’d be more tempted to have that as it’s own table with more data attached such as the UID who deleted it, reason (if application supported that), etc. That way you can have more detail on “deleted” records without having to null several fields nor increase your table size for your main table with lots of “deleted” metadata when typically records wouldn’t be marked as “deleted”. Though “deleted” isn’t really the right term here either because you’re not really deleting data, just removing it from view.