Home > Oracle Tips > Database Developer > NULLs in WHERE clauses can be deceptive
Oracle Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

DATABASE DEVELOPER

NULLs in WHERE clauses can be deceptive


Serdar Yegulalp
07.25.2005
Rating: -3.99- (out of 5)


Oracle tips, scripts, and expert advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


NULL values are one of those things that people claim to understand, but they often write code that doesn't reflect how NULL values are actually handled. Because of this, it's all too easy to create queries that don't behave correctly, even when they might outwardly seem to be all right.

Here's an example from my own experience: Let's say we have a table named tb_user_applications, with a char column named pending that can contain a NULL value, a "P" (for "pending") or "Y" (for "yes" or "approved"). If we constructed a query that went like this:

SELECT ITEM_ID FROM
TB_USER_APPLICATIONS
WHERE PENDING IS NOT NULL

we'd end up with everything where pending was set to "P" or "Y." But if we did this:

SELECT ITEM_ID FROM
TB_USER_APPLICATIONS
WHERE PENDING<>'Y'

…then we would get everything only set to "P." The NULL values would be omitted.

This is by design because of the way DBMS regards NULL values. They are not grouped in with other distinct values; they exist in a class by themselves. NULL has to be evaluated on its own terms in all instances. This is something many people think they know, until they run across a living example of it and their code breaks.

If you are making use of NULL in a column, make sure that NULL is evaluated and selected independently of any actual values. Otherwise you'll run into problems like the one described above, where the programmer assumes that NULL values will be selected along with non-NULL values.

In some cases, it might be better to redesign the database to avoid the use of NULLs entirely (especially if you spend most of your time programming around it instead of accommodating it!). Instead of Nulls, use constraints and defaults to keep valid values in the column in question.


Serdar Yegulalp is editor of the Windows Power Users Newsletter. Check it out for the latest advice and musings on the world of Windows network administrators -- and please share your thoughts as well!


Rate this Tip
To rate tips, you must be a member of SearchOracle.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
Database Developer
How do you create a link between two databases inside a stored procedure?
PL/SQL do's and don't's: Five questions with Steven Feuerstein
Mike Ault's Oracle "good practices": Oracle coding
Easy Oracle PL/SQL programming: Assignments, initializations and NULLs
Introduction to BPEL
SQL puzzles and answers: Finding equal sets
Developer and DBA: Working together for greater efficiency
The Top 10 (more or less) J2EE best practices
Build a servlet-based application that executes SQL statements against a database
Joining tables in SQL queries

Oracle Database Administrator
What is the difference between a database engineer, architect and administrator?
Import on one table from dump file
Error during RMAN backup
Can I drop a column in SYS schema?
STATSPACK tool: transaction vs. execution measurement
Should I port from Microsoft Access?
Installing multiple Oracle homes
How can I find statistics on total memory usage and database connections?
Modifying SYS password in a RAC environment
How to create Datafiles in a Data Guard (10g) environment

Oracle SQL
SQL to count values of a status code
Counting NULL columns
Detail rows for accounts that occur three times
Counting a row's NULL columns
Oracle's free SQL Developer adds database migration tool
Latest transaction if no recent prior transactions
Three ways SQL can count rows by type
SQL to select only certain times within a date range
Oracle SQL to test for numerics
Number of rows in multiple tables
Oracle SQL Research

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
autonomous transaction  (SearchOracle.com)
CFML  (SearchOracle.com)
dynamic SQL  (SearchOracle.com)
foreign key  (SearchOracle.com)
Java Database Connectivity  (SearchOracle.com)
Open Database Connectivity  (SearchOracle.com)
Oracle  (SearchOracle.com)
stored procedure  (SearchOracle.com)
The Open Group  (SearchOracle.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts