Home Database Mysql Tutorial Sql与oracle中null值

Sql与oracle中null值

Jun 07, 2016 pm 04:21 PM
null oracle

1 null值的介绍 NULL 是数据库中特有的数据类型,当一条记录的某个列为 NULL ,则表示这个列的值是未知的、是不确定的。既然是未知的,就有无数种的可能性。因此, NULL 并不是一个确定的值。 这是 NULL 的由来、也是 NULL 的基础,所有和 NULL 相关的操作的

   1 null值的介绍

  NULL 是数据库中特有的数据类型,当一条记录的某个列为 NULL ,则表示这个列的值是未知的、是不确定的。既然是未知的,就有无数种的可能性。因此, NULL 并不是一个确定的值。 这是 NULL 的由来、也是 NULL 的基础,所有和 NULL 相关的操作的结果都可以从 NULL 的概念推导出来。

  2 oracle中的null值介绍

  在不知道具体有什么数据的时候,即未知,可以用NULL, 称它为空,ORACLE中,含有空值的表列长度为零。允许任何一种数据类型的字段为空,除了以下两种情况:

  a、主键字段(primary key);

  b、定义时已经加了NOT NULL限制条件的字段

  3 Oracle中null值说明:

  a、等价于没有任何值、是未知数。

  b、NULL与0、空字符串、空格都不同。

  c、对空值做加、减、乘、除等运算操作,结果 仍为空。

  d、NULL的处理使用NVL函数。

  e、比较时使用关键字用“is null”和“is not null”。

  f、空值不能被索引,所以查询时有些符合条件的数据可能查不出来, count(expr)中,用nvl(列名,0)处理后再查。

  g、排序时比其他数据都大(索引默认是降序排列,小→大), 所以NULL值总是排在最后。

  IS NULL 和IS NOT NULL 是不可分割的整体,改为IS 或IS NOT都是错误的,从上面我们看到了NULL 和空字符串的区别。

  任何和NULL 的比较操作,如、=、

  判断和比较规则总结表

Sql与oracle中null值    三联

  例如:使用方法:

  SQL> select 1 from dual where null=null;

  没有查到记录

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

  没有查到记录

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

  没有查到记录

  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

  4、null做一些算术运算,比如+,-,*,/等,结果 还是null,但是对于连接操作符||,null忽略,concat函数也忽略null

  SQL> select null || 'abc' from dual;

  NUL

  ---

  abc

  SQL> select concat(null,'abc') from dual;

  CON

  ---

  abc

  SQL> select null+10 from dual;

  NULL+10

  ----------

  5、null相关函数规则

  Oracle有nvl、nvl2、nullif、coalesce等函数专门处理null

  5.1 nvl(expr1,expr2)

  描述:如果expr1是null,那么用expr2作为返回值,不是null则返回expr1.expr1与expr2一般是类型相同的,如果类型不同则会采用自动转换,转换失败则报错。

  SQL*PLUS中数值类型右对齐,字符左对齐;通过第三条语句可以看出null和‘’还是有区别的。

  SQL> select nvl(null,0) from dual;

  NVL(NULL,0)

  -----------

  0

  SQL> select nvl(to_char(null),0) from dual;

  N

  -

  0

  SQL> select nvl('',0) from dual;

  N

  -

  0

  5.2 nvl2函数

  语法:nvl2(expr1,expr2,expr3)

  描述:expr1如果是null,则返回expr3,否则返回expr2

  expr2和expr3类型不同,expr3类型转换为expr2类型

  SQL> select nvl2(null,'1',2) from dual;

  N

  -

  2

  expr2为null,返回值类型和expr3一致

  SQL> select nvl2(null,null,2) from dual;

  NVL2(NULL,NULL,2)

  -----------------

  2

  SQL> select nvl2(null,null,'2') from dual;

  N

  -

  2

  不同类型之间的转换,自动转换不成功,则报错

  SQL> select nvl2(null,1,'b') from dual;

  select nvl2(null,1,'b') from dual

  *

  ERROR at line 1:

  ORA-01722: invalid number(无效数字)

  5.3 nullif函数

  语法:nullif(expr1,expr2)

  描述:判断expr1和expr2是否相等,若相等则返回null,否则返回expr1.要求expr1与expr2类型必须相同

  SQL> select nullif(1,3) from dual;

  NULLIF(1,3)

  -----------

  1

  SQL> select nullif(1,1) from dual;

  NULLIF(1,1)

  -----------

  SQL> select nullif('1',1) from dual;

  select nullif('1',1) from dual

  *

  ERROR at line 1:

  ORA-00932: inconsistent datatypes: expected CHAR got NUMBER

  SQL> select nullif('ab','ab ') from dual;

  NU

  --

  SQL> select nullif(null,1) from dual;

  select nullif(null,1) from dual

  *

  ERROR at line 1:

  ORA-00932: inconsistent datatypes: expected - got CHAR

  SQL> select nullif(to_char(null),'1') from dual;

  N

  -

  5.4 coalesce函数

  语法:coalesce(expr1,expr2,…,exprn)

  描述:从左到右返回第一个为非null的值,若所有的列表元素都为null,则返回null。要求所有都必须为同一类型。

  SQL> select coalesce(null,null,null) from dual;

  C

  -

  SQL> select coalesce(null,1,2) from dual;

  COALESCE(NULL,1,2)

  ------------------

  1

  SQL> select coalesce(1,'1',1) from dual;

  select coalesce(1,'1',1) from dual

  *

  ERROR at line 1:

  ORA-00932: inconsistent datatypes: expected NUMBER got CHAR

  6 null与索引

  Oracle中的B*Tree索引,并不存储全为null的列,

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

How to delete all data from oracle How to delete all data from oracle Apr 11, 2025 pm 08:36 PM

Deleting all data in Oracle requires the following steps: 1. Establish a connection; 2. Disable foreign key constraints; 3. Delete table data; 4. Submit transactions; 5. Enable foreign key constraints (optional). Be sure to back up the database before execution to prevent data loss.

How to paginate oracle database How to paginate oracle database Apr 11, 2025 pm 08:42 PM

Oracle database paging uses ROWNUM pseudo-columns or FETCH statements to implement: ROWNUM pseudo-columns are used to filter results by row numbers and are suitable for complex queries. The FETCH statement is used to get the specified number of first rows and is suitable for simple queries.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

How to stop oracle database How to stop oracle database Apr 12, 2025 am 06:12 AM

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.

How to create oracle dynamic sql How to create oracle dynamic sql Apr 12, 2025 am 06:06 AM

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values ​​to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.

What steps are required to configure CentOS in HDFS What steps are required to configure CentOS in HDFS Apr 14, 2025 pm 06:42 PM

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

See all articles