Test Oracle SQL _not exists and null

Source: Internet
Author: User

During the Oracle storage test, it is found that the values of many columns in the product table may be null, resulting in not exists results inconsistent with
The test is as follows:
-- Construct two tables with the same table structure
SQL> desc t_null_column;
Name Type Nullable Default Comments
----------------------------------
A INTEGER Y
B INTEGER Y
C INTEGER Y

SQL> desc t_null_column_2;
Name Type Nullable Default Comments
----------------------------------
A INTEGER Y
B INTEGER Y
C INTEGER Y
 
-- Column B is null.
SQL> select * from t_null_column;

A B C
---------------------------------------------------------------------------------------------------------------------
1 2

SQL> select * from t_null_column_2;

A B C
---------------------------------------------------------------------------------------------------------------------
1 2

-- The following SQL statement is intended to query records that are not in the t_null_column_2 table at all. What I want is that records are not returned and records are returned.
SQL> select tc. * from t_null_column tc where not exists (select 1 from t_null_column_2 tc2 where tc. a = tc2.a and tc. B = tc2. B and tc. c = tc2.c );

A B C
---------------------------------------------------------------------------------------------------------------------
1 2

-- Modify the preceding SQL statement and nvl the null Column B. The result is as expected.
SQL> select tc. * from t_null_column tc where not exists (select 1 from t_null_column_2 tc2 where tc. a = tc2.a and nvl (tc. b, 1) = nvl (tc2. B, 1) and tc. c = tc2.c );

A B C
---------------------------------------------------------------------------------------------------------------------

Conclusion: 1. null is terrible and must be thoroughly understood. Otherwise, a big mistake will be made.
2. null and null are not equal, that is, the result of null = null is false rather than true.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.