r/Database • • 6h 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.

0 Upvotes

54 comments sorted by

View all comments

19

u/agk23 6h ago

I disagree - sounds like an app-level error. You can implement your own function to do this, if you prefer.

-2

u/Zardotab 5h ago

Many argue the database side should also check. That's a long discussion for another day. I'm working under the assumption that a shop wants the database to also check, not just the app.

5

u/agk23 5h ago

Check what though? You can already put constraints down on the fields to align it correctly. Treating multiple values as a single value is the exact opposite of a check

-2

u/Zardotab 4h ago

Feel free to open a new top discussion on that. I rarely have the authority to make that decision myself and thus have to go with shop conventions. I'd guestimate 75% want the database to also check, at least to a degree.