Oracle treating empty string as NULL problem for a Java / JPA programmer

后端 未结 6 2396
失恋的感觉
失恋的感觉 2021-02-19 17:22

How do you handle this situation where Oracle stores the empty string as a null in the database ?

I would like it to be stored as an empty string as it is not as NULL, s

相关标签:
6条回答
  • 2021-02-19 17:38

    try

    create index idx_myfield on mytable(nvl(myfield,-1));
    
    select * from mytable where nvl(myfield,-1)=-1;
    
    0 讨论(0)
  • 2021-02-19 17:39

    Yup, that's the way Oracle functions. Empty strings are treated as nulls.

    You can of course "fix" this on application level - for example by storing " " values as you suggested - but first consider, what exactly is the difference with your "empty string" values compared to NULL values? Why do you need to treat them differently? I used to run into this dilemma, too, but usually found out that there are very few cases where I really need to tell the difference.

    0 讨论(0)
  • 2021-02-19 17:43

    No, there is no way to treat empty strings as empty strings. Oracle always treats a string of length zero as a NULL value.

    0 讨论(0)
  • 2021-02-19 17:49

    Its early for me, but isn't

    select * from mytable where myfield like '%' or myfield is null
    

    the same as

    select * from mytable
    

    So, Oracle simplifies your life! ;)

    0 讨论(0)
  • 2021-02-19 17:56

    Beleive Oracle is optimizing the database by converting the empty string as NULL. First an empty string still may be need to be stored explicitly in the database storage at data block level (*1). Storing as NULL may reduce data storage footprint. Second if any indexes were defined on the column then the NULL values are not included in index thereby reducing index storage footprint. (*2)

    From a design and development standpoint unless the empty space really changes the semantics of the data from a business perspective storing as NULL should be fine.

    Adding code at framework level ( or parent class ) as suggested above would eliminate the need to type it out at all children classes & objects - that is what Object Oriented and even basic programming strive to do.

    If my code is performing best because my database storage is optimized, then I should be happy. It sucks if my code performance tanks in production just because I wanted to save a little typing or failed to do masterful design / development?

    I would be happy that Oracle is optimizing it for me under the covers.

    All about Oracle NULL:

    NULL is a special value. NULL does not equate to anything including itself i.e NULL is not equal to NULL

    0 讨论(0)
  • 2021-02-19 17:57

    It´s not only the selection with special where condition but also the treating of Java String Objects. If you have a String a="" you can call its length method and get 0. If you have a String a=null you get a nullpointer exception when calling length. So working with an oracle db forces you to always check if your string is null before checking length :(

    0 讨论(0)
提交回复
热议问题