0
create table test
(

   id varchar2(10) check( length(trim(id)) > 0),
   primary key(id)
)

insert into test values (' '); -- this works

I expected this check constraint to stop this insert. But, it gets inserted none the less. Why ?

3
  • Something doesn't seem right, separate from your specific question. If you do expect leading or trailing spaces, and you want to make sure the ID contains characters other than spaces (which seems to be the purpose of the constraint), why are you saving the ID with those spaces in the table in the first place? Why don't you store trim(id) as the primary key? Are you going to use trim(id) everywhere in your queries and other statements? Will foreign keys point to id or to trim(id)? Etc. Seems like the spaces should be trimmed before saving in the table.
    – user5683823
    Commented Feb 3, 2021 at 17:21
  • @mathguy even though trim(id) as a primary key looks likely to work and is an exciting lateral solution provided. I would like to keep the primary key expression clean. Best practice would be to validate the data in the constraint and prevent the insertion of blanks. Commented Feb 5, 2021 at 16:36
  • @mathguy alternatively we could use a BEFORE INSERT trigger as well - which would make a simple requirement complex unnecessarily. So, your contention that UNKNOWN is being treated as TRUE, holds true as the root cause Commented Feb 5, 2021 at 16:48

2 Answers 2

1

Problem is TRIM(' ') returns NULL and LENGTH(NULL) also gives NULL (not 0)

Try NVL(LENGTH(TRIM(' ')), 0) > 0 or

Try TRIM(ID) IS NOT NULL

Snippet stating that constraints can return null aka 'unknown' and considered to be NOT VIOLATING the constraint. Only a return of false violates a constraint. At least as per Oracle 12c

See Oracle Concept page about constraints

Snippet stating that constraints can return null aka 'unknown'

7
  • so is "null" in oracle evaluating to true for that constraint !!! i thought that null meant UNKNOWN in oracle. thanks for answering - I used INSTR(ID,' ') > 0 as a check constraint instead Commented Feb 3, 2021 at 16:13
  • Also what is odd is why trim(' ') does not return a zero terminated string '' <=== like this ? Commented Feb 3, 2021 at 16:15
  • 3
    @user1561783 - The explanation is incomplete and it should be expanded. null > 0 evaluates to UNKNOWN. In query conditions, UNKNOWN is treated as "the same as" FALSE. Not so in constraints, however. In constraints, UNKNOWN is treated the same as TRUE. This is a very general rule, not special to the OP's constraint.
    – user5683823
    Commented Feb 3, 2021 at 17:17
  • 3
    @user1561783 - your other question, about trim(' '), suggests that you may be familiar with strings (in C, for example), and with empty strings, and with "empty string" not being the same as null (for example in the SQL Standard, and in some SQL dialects), but you are not aware that - idiotically - in Oracle the empty string is treated the same as null. This will cause a lot of difficulties for you if you are coming from a different dialect; you will have to learn to live with it, and to be surprised many times before you get really used to it.
    – user5683823
    Commented Feb 3, 2021 at 17:24
  • @mathguy - have expanded the explanation above. I just needed an added reference from oracle itself to confirm the treatment of UNKNOWNs (aka null) in constraints Commented Feb 5, 2021 at 18:31
1

Explanation for what you saw:

  1. If str is made up entirely of spaces, then trim(str) is the empty string.

  2. Oracle treats the "empty string" the same as null (of varchar2 data type, when that matters), in flagrant violation of the SQL standard. Oracle is not even consistent in this - there are (very few) exceptions where the empty string is, in fact, seen as "empty string" (for example, in concatenations).

  3. By definition, the length of null is null (in particular, not zero).

  4. In SQL, a condition like null > 0 evaluates to unknown (in the three-valued logic needed to accommodate null in such conditions).

  5. In check constraints unknown is treated the same as true. This is different from the treatment in other conditions (in SQL statements - in where clauses, join conditions etc.), where unknown is treated the same as false. This is documented, for example, here: https://docs.oracle.com/cd/B19306_01/server.102/b14200/clauses002.htm - see the first paragraph in the Check Constraints section.

The correct solution to your problem:

Checking if something is null should be done DIRECTLY, with the is null or is not null conditions. Don't use length for that.

Like this:

check( trim(id) is not null )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.