4 ms·
In my experience most of the time a field is nullable it’s by accident. Some RDBMS and/or SQL “IDEs” default to allowing null too, which doesn’t help matters. P
by pushpop 7y ago
In my experience most of the time a field is nullable it’s by accident. Some RDBMS and/or SQL “IDEs” default to allowing null too, which doesn’t help matters. Personally I think NULL shouldn’t be supported unless the developer categorically sets a “yes, I actually do want this archaic option enabled” flag in their DB backend.
Edit: getting a lot of heat for this comment from people misunderstanding it. I’m not saying “null” shouldn’t exist, just that it shouldn’t be the default. Side point: most of the time people think they need null they don’t.
- camdenlock 7y ago“Unset” is an extremely valuable state to be able to check. It’s why we have null/nil in well-designed languages. The zero value of a golang int is not the same as indicating a lack of presence. There are plenty of scenarios where “unset” is a necessary state vs the zero value.
- pushpop 7y agoI agree however it is only valuable under specific circumstances while it can frequently create hidden traps. On balance it causes more problems than it solves. Which is why I suggested it should be an optional flag enabled on the backend rather than the default.
- baq 7y agoMaybe/Option would be better. NULL is what we've got though. setting columns to not null is leaps ahead of most programming languages anyway.
- peteretep 7y ago> On balance it causes more problems than it solves This is far from clear
- pushpop 7y agoLike everything, it depends on your language and framework. But after 30 years of experience writing software in strictly typed languages, I’ve seen lots of people get caught out with nullable fields. However if you’re main body of experience is Perl (for example) then NULL isn’t ever an issue.
- peteretep 7y agoRight, because no Perl developer has ever used SQL
- pushpop 7y agoI get Perl isn’t trendy these days but Perl actually has a very robust set of DB libraries and I have worked in several places were it was the preferred language.
- peteretep 7y agoPerl has by far the best DB tooling of any of the many languages I’ve worked with. My point was sarcasm as rebuttal to the idea that a Perl developer might not have encountered a null value
- pushpop 7y agoActually I said NULL isn’t an issue in some languages with looser type systems, not that other developers wouldn’t have encountered a NULL before. Eg Perl doesn’t have a nil type like Go, so variables passed to DBI which are NULL in the returned SQL query will be undef’ed instead. Effectively a bit like NULL but it only causes a problem if your running with strict flags (which you shouldn’t do on production systems anyway for various reasons; but that’s another topic entirely) otherwise that value falls back to the language default of “”/0/false. With languages like Go you can easily end up with a panic if you’re not careful (where you’re trying to cast a nil interface{}). Which, I’m guessing, is why this Go library being discussed here does it’s own NULL checking.
- trinitry3 7y agoOnly if your developers are inexperienced.
- pushpop 7y agoI’m sure you’d love to consider yourself infallible but human error can happen even to the best of us. However if we’re talking from idealistic perspective then you’d have your own DBAs who would create the DB and write the SQL so your developers just call your (for example) stored procedures like APIs rather than writing their own - often unoptimised - SQL themselves.
- marcc 7y agoNullable is being set by accident? Nullable values are an “archaic” option? Nullable is different than empty. Consider a contrived example where I’ve extended a user table to include a new column “fullName”. By making this a nullable column, I can determine that a user has not set this value. But maybe the user sets it to an empty string. The API might want to know this and handle it differently — prompting for a value if it’s null, but ignoring “” as “this is what the user wanted to set”. Without nullable values, I would have to create bit values in a lot of tables indicating if the value was ever set. Another example is time stamps. Databases have timestamps for createdAt and updatedAt often. There are people who set updatedAt when they set createdAt, but there are people who don’t. I’m in the later camp. If an object was created but never updated, I want to be able to know that. How would I do this if the updatedAt column is not nullable? I’d either have to set a bit field that it has been updated, which feels suboptimal, or I could set it to something like “time.Empty{}”, which is a magic number tied to the programming language? There are plenty of other solutions here, sure, but what’s wrong with nullable fields?
- pushpop 7y agoFirst 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.
- josteink 7y ago> In my experience most of the time a field is nullable it’s by accident. Then consider getting some more.
- dang 7y agoPlease don't break the site guidelines no matter how wrong someone is or you feel they are. https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html Edit: we've given you many warnings. Continuing will eventually get your account banned, so would you please take the spirit of HN more to heart and fix this? We'd be grateful.
- zzzeek 7y agoDo you think SQL should have outer joins? Then you need NULL.
- pushpop 7y agoFirstly, I’ve never said people shouldn’t have NULL. I said columns should default to be nullable. Secondly, I’d already given outer joins as an example of why SQL needs to support NULL. I would be nice that people like yourself bothered to read the comments your reacting to instead of jumping off the deep end (-4 on my original post now and people like yourself are literally just reiterating the same points I’ve been making. It’s insane)
- zzzeek 7y agoIf five people misunderstood your comment (which says nothing about outer joins as I reread it) maybe it is your writing that would need to be clearer.
- pushpop 7y agoMaybe but my experience on HN is that there’s sometimes a mentality of “fools never differ” on here. I’ve seen countless correct and/or experienced commenters get downvoted even when the comment is well written. It’s also worth remembering that five people is still only a very small subset of the hundreds that would have read the comment and did understand/agree but didn’t bother to comment for fear of the wrath of those who have already expressed disagreement. For example I was actually voted up several times before the others commented (as well as a few times since). So it’s really not worth blaming individuals for the comments of others. The only possible outcome from that is starting another, much stupider, argument.
- wrs 7y agoYeah, the world would probably be a better place if the default in a column definition was NOT NULL rather than NULL. That's not really relevant to this discussion, though -- once you do have a nullable column in your schema, it doesn't make sense in the Go layer to pretend you don't.
- pushpop 7y ago> That's not really relevant to this discussion, though It’s tangential but still relevant to the discussion. > once you do have a nullable column in your schema, it doesn't make sense in the Go layer to pretend you don't. I agree and never suggested what this Go API was doing did make sense.
- andreareina 7y agoI agree that NOT NULL should be the default for columns, unfortunately that ship has sailed. Assuming that the nullable foreign key problem could be solved, I think that NULL is valuable enough (and not that rare) that it ought to be the database owner's choice whether to allow them or not, as opposed to the superuser (not sure which way you're suggesting).
- pushpop 7y ago> I agree that NOT NULL should be the default for columns, unfortunately that ship has sailed. Not really. Defaults change all the time. I’m not asking for a feature to be removed after all. I don’t deny the change in any specific RDBMS would have to be carefully considered but I’ve worked on a lot harder projects to make transitions in default behaviours than this would be so it’s certainly doable if the momentum was there. However I doubt enough people care enough (if this thread is anything to go by). > Assuming that the nullable foreign key problem could be solved Offhand I can think of a few ways to solve that but I also think it’s one of the instances where NULL makes the most sense > I think that NULL is valuable enough (and not that rare) that it ought to be the database owner's choice whether to allow them or not, as opposed to the superuser (not sure which way you're suggesting). I completely agree the DBA should still have the power to enable it. My point was just that columns shouldn’t default to being nullable during a CREATE. To be honest, this could just as easily be “fixed” in graphical database management tools. That would at least enable seasoned DBAs to utilise NULL while preventing others from some of the pitfalls that nullable fields can introduce in some languages. However I still stand by my point that it’s not really a sane default for the way most people architect databases these days (or even “should” be architecting).
- deleted 7y ago[deleted]