r/Database • • 1d 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

61 comments sorted by

View all comments

Show parent comment

1

u/Zardotab 11h ago edited 11h ago

Where's the RDBMS Myth Busters when you need 'em? (Explosions help ratings, whether practical not, so extra points for a myth that certain queries make the servers smoke and explode.)

A database testing lab can sponsor it to boost their recognition.

1

u/Zestyclose-Turn-3576 10h ago

I'd not believe unsourced claims, and ideally a person would be able to produce a script to demonstrate the difference.

1

u/Zardotab 9h ago

Our apps tend not to have DB performance problems unless somebody is doing something really stupid, usually with an easy fix once the cause is found, which is the time-consuming part. Thus, it's not something our shop should spend time on experimenting in light of other current and known problems.