r/Database • u/Zardotab • 2d ago
Rant: "Null" versus "Not Null" is not solving what most think it solves. RDBMSs needs an enhancement.
For the vast majority of business and administrative apps I've worked on, what designers really want is what I'll call "empty" versus "not empty" as working terms. "Not Null" allows zero length strings, one space, 7 spaces, tabs, etc. I'll call such strings WSO strings: "white-space-only".
The vast majority of the time allowing WSO when Not Null is specified is not what's desired, as WSO is a way to cheat around "not null" (inadvertently or intentional). Thus, "Not Null" is not a thorough bouncer for typical needs.
One way to solve this would be to add a "Not Empty" keyword to SQL table specs (DDL) that would reject WSO entries. Another approach is something akin to "Pre-Trim" that would apply white-space trimming before saving it. Pre-Trim could convert any WSO to a null value, which would trigger an error if Not Null is set for a given column.
But I'd like to generalize the idea rather than add overly-specific keywords to SQL. Have the ability to add user-defined tags that can clean and normalize how a shop wants it, and also trigger errors if desired. This improves DRY since one doesn't have to repeat code handlers or call blocks for each column.
-- Pseudo-code for defining a tag.
-- This tests for being empty, and normalizes the value
DEFINE TAG NotEmpty(@context) AS
BEGIN
@context.value = COALESCE(TRIM(@context.value),'');
-- shop function to rid duplicate adjacent spaces & tabs
@context.value = func_removeDupSpaces(@context.value);
IF LENGTH(@context.value) = 0 THEN
BEGIN
RAISE_ERROR(
'Value not allowed to be blank. Column: [@context.columnName]'
);
END
COMMIT @context /* return results */
END
.............................
-- Usage in SQL DDL:
CREATE TABLE users (
user_id INT PRIMARY KEY,
username VARCHAR(50) UNIQUE, TAGS: NotEmpty,
email VARCHAR(100) TAGS: NotEmpty,
admin_note VARCHAR(500) TAGS: Normalize, foo, bar /* see note */
);
Here we don't need "NOT NULL" for user-name and email because our new tag handles that for us. Note that maybe we can factor the implementation of NotEmpty to call the "Normalize" tag so we don't have to reinvent cleaning. ("Normalize" implementation is not shown.)
Anyhow, tags are not the only way to solve this problem, I'm just trying to think of handling it with a more general concept that can better fit each shop's conventions. Common shop conventions such as "all caps", "no caps", "zero pad", etc. can also be implemented once as tags. It would make a DBA's and architect's job easier. Thank You for consider it.
Note that commas may get confusing when there are multiple tags per column in the DDL. Perhaps require parentheses when multiple: "...TAGS: (Normalize, foo, bar)". And perhaps do away with the colon.
19
u/agk23 2d ago
I disagree - sounds like an app-level error. You can implement your own function to do this, if you prefer.
11
u/AdvisedWang 2d ago
Seems like a CHECK constraint can enforce formatting requirements.
6
u/Imaginary__Bar 2d ago
An unorthodox abbreviation but if it works for you...
11
u/ComicOzzy 2d ago
NULL doesn't mean "empty", it means "unknown" or "inapplicable".
It can confuse people a bit that Oracle treats empty strings and NULL as the same thing, but don't take that as the model for common behavior.
5
u/flashman1986 1d ago
Sorry, why are you solving this at the database level? This is something that is properly solved at the business application level. Think about what you are asking
4
u/GreyHairedDWGuy 2d ago
There is nothing wrong with the general SQL implementation for null/not null. Your queries/code just need to work around that fact that a 0 length string will not be considered null. Also, the apps that generate the data should not allow for 0 length strings or n spaces if it will cause issues downstream.
nothing new is required.
4
u/IMarvinTPA 1d ago
A proper null is lack of a piece of paper.
An empty string is a blank sheet of paper.
Otherwise, you are doing it wrong...
3
u/_choam_ 1d ago
I do think it is good to make a type which represents exactly the allowed values (think NonEmptylist which is a list type guaranteed to have at least one element)
2
u/squadette23 2d ago
Yeah, NULLs are only one kind of sentinel values. For most data types you can sneak in non-NULL sentinel value.
I wrote about this a couple of years ago: https://minimalmodeling.substack.com/p/sentinel-free-schemas-a-thought-experiment
You can design a schema in such a way that it requires no sentinel values, see the second half of that post.
2
u/da_chicken 2d ago
You think NULL is an overly specific keyword? I don't even know what that means since it sounds like you just want to use the "empty string is null" idea that Oracle spent a couple decades killing off.
I also don't see how
NOT NULLisn't already a tag or how it's longer thanTAGS: NotEmpty.I also don't see how you're going to make outer joins work. Even if you're normalized to the Nth degree, you will have queries that need to return null values.
If anything, I think the consensus has been that the only problem with the NOT NULL constraint is that it should have been the default. You should have had to explicitly permit null values to encourage normalization. That and the FROM clause should have been first are the two major faults in SQL.
Don't get me wrong, I think a better way to define custom data types or to implement a domain definition for a field would be great. But RDBMSs are 60 years old at this point. If null were actually not useful it would have been eliminated decades ago.
2
u/t3lnet 1d ago
Don’t forget their red headed step sibling isNull()
2
u/BobDogGo 1d ago
sometimes spaces are a valid value and distinct from NULL
and there’s no equivalent for dates and numbers.
this is a domain specific rule and is easy to address locally
2
u/data-artist 1d ago
No - Validate your input before it goes into your database.
1
u/dbrownems 2d ago
I think it was a mistake to allow empty strings in string columns, and I prefer Oracle's behavior to convert them to NULL. But the standards are what they are, so a CHECK constraint is a pretty easy fix.
1
u/_choam_ 1d ago
That's javascript lol.
Anyway, all these things that make guesses on what you actually mean, are always massive footguns.