Use of null in Oracle databases

Source: Internet
Author: User

Are you very worried about the actual Oracle database null operations? If this is the case, the following articles will give you corresponding solutions. The following articles mainly introduce the specific use of null in Oracle databases, the following is a detailed description of the relevant content.

Q: What is NULL?

A: When we do not know the specific data, it is also unknown. NULL can be used,

We call it null. In an Oracle database, the length of a table with null values is zero.

Oracle allows blank fields of any data type, except for the following two cases:

1. primary key field primary key ),

2. fields with the not null restriction added during definition

Note:

1. It is equivalent to no value and is unknown.

2. NULL and 0, empty strings, and spaces are different.

3. add, subtract, multiply, and divide null values. The result is still null.

4. The NVL function is used for NULL processing.

5. Use the keyword "is null" and "is not null" for comparison ".

6. null values cannot be indexed. Therefore, some data that meets the query conditions may not be found,

In count *), use nvl column name, 0) to process and then query.

7. When sorting data is larger than other data, indexes are sorted in descending order by default, small → large ),

Therefore, the NULL value is always at the end.

Usage:

 
 
  1. SQL> select 1 from dual where nullnull=null; 

No records found

 
 
  1. SQL> select 1 from dual where null=''; 

No records found

 
 
  1. SQL> select 1 from dual where ''=''; 

No records found

SQL> select 1 from dual where null is null;
1
---------
1
SQL> select 1 from dual where nvl (null, 0) = nvl (null, 0 );
1
---------
1
 

Add, subtract, multiply, and divide null values. The result is still null.

 
 
  1. SQL> select 1+null from dual;  
  2. SQL> select 1-null from dual;  
  3. SQL> select 1*null from dual;  
  4. SQL> select 1/null from dual; 

A record is queried.

Note: This record is the null value in the SQL statement.

Set null values for some columns

Update table1 set column 1 = NULL where column 1 is not null;

There is a sales table sale with the following structure:

Month char6) -- month

Sellnumber10, 2) -- monthly sales amount

 
 
  1. create table sale (month char(6),sell number);  
  2. insert into sale values('200001',1000);  
  3. insert into sale values('200002',1100);  
  4. insert into sale values('200003',1200);  
  5. insert into sale values('200004',1300);  
  6. insert into sale values('200005',1400);  
  7. insert into sale values('200006',1500);  
  8. insert into sale values('200007',1600);  
  9. insert into sale values('200101',1100);  
  10. insert into sale values('200202',1200);  
  11. insert into sale values('200301',1300);  
  12. insert into sale values('200008',1000);  
  13. insert into sale(month) values('200009'); 

Note: The limit value of this record is null.

 
 
  1. commit; 

12 records in total

 
 
  1. SQL> select * from sale where sell like '%';  
  2. MONTH SELL  
  3. ------ ---------  
  4. 200001 1000  
  5. 200002 1100  
  6. 200003 1200  
  7. 200004 1300  
  8. 200005 1400  
  9. 200006 1500  
  10. 200007 1600  
  11. 200101 1100  
  12. 200202 1200  
  13. 200301 1300  
  14. 200008 1000 

11 records are queried.

Result description:

The query results indicate that this SQL statement does not query fields with a column value of NULL.

In this case, the field must be NULL.

 
 
  1. SQL> select * from sale where sell like '%' or sell is null;  
  2. SQL> select * from sale where nvl(sell,0) like '%';  
  3. MONTH SELL  
  4. ------ ---------  
  5. 200001 1000  
  6. 200002 1100  
  7. 200003 1200  
  8. 200004 1300  
  9. 200005 1400  
  10. 200006 1500  
  11. 200007 1600  
  12. 200101 1100  
  13. 200202 1200  
  14. 200301 1300  
  15. 200008 1000  
  16. 200009 

12 records are queried.

Oracle Database null is used in this way. We 'd better familiarize ourselves with its conventions to prevent incorrect results.

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.