Oracle NULL Statement

在Oracle数据库中,关于“NULL”的语句处理涉及到如何处理数据库中的空值。NULL在数据库中表示一个未知、缺失或不适用的值。它与零、空字符串或其他任何值都不同,并且有其独特的处理方式。

Oracle提供了多种方式来处理包含NULL值的语句,包括查询(SELECT)、更新(UPDATE)、插入(INSERT)以及条件判断等。以下是一些常见的使用NULL的Oracle语句示例:

The NULL statement only passes control to the next statement. Some languages refer to such an instruction as a no-op (no operation).

Some uses for the NULL statement are:

  • To provide a target for a GOTO statement

  • To improve readability by making the meaning and action of conditional statements clear

  • To create placeholders and stub subprograms

  • To show that you are aware of a possibility, but that no action is necessary

Note:Using the NULL statement might raise an unreachable code warning if warnings are enabled.
如果启用了警告,使用NULL语句可能会引发无法访问的代码警告。

1. 查询包含NULL值的列

当你想要查询某列中所有为NULL的记录时,你可以使用IS NULL条件。

SELECT * FROM employees WHERE manager_id IS NULL;

这个查询会返回所有manager_id为NULL的employees记录,即那些没有指定经理的员工。

2. 查询不包含NULL值的列

相反,如果你想要查询某列中所有非NULL的记录,你可以使用IS NOT NULL条件。

SELECT * FROM employees WHERE manager_id IS NOT NULL;

这个查询会返回所有manager_id不为NULL的employees记录,即那些有指定经理的员工。

3. 使用NVL函数处理NULL值

NVL函数是Oracle提供的一个非常有用的函数,用于将NULL值替换为另一个指定的值。

SELECT employee_id, NVL(commission_pct, 0) AS commission_pct FROM employees;

这个查询会返回所有员工的employee_idcommission_pct,但如果commission_pct是NULL,则将其替换为0。

4. 使用CASE语句处理NULL值

CASE语句也常用于根据条件对NULL值进行更复杂的处理。

SELECT employee_id,
       CASE WHEN commission_pct IS NULL THEN 'No Commission'
            ELSE 'Commission Exists'
       END AS commission_status
FROM employees;

这个查询会根据commission_pct是否为NULL来为每个员工返回一个commission_status

5. 插入NULL值

在插入数据时,你可以直接将某些列的值设置为NULL,表示这些列的值未知或未提供。

INSERT INTO employees (employee_id, first_name, last_name, manager_id)
VALUES (100, 'John', 'Doe', NULL);

这个语句创建了一个新的员工记录,其manager_id为NULL,表示该员工目前没有指定经理。

6. NULL Statement Showing No Action

The NULL statement emphasizes that only salespersons receive commissions.

DECLARE
  v_job_id  VARCHAR2(10);
   v_emp_id  NUMBER(6) := 110;
BEGIN
  SELECT job_id INTO v_job_id
  FROM employees
  WHERE employee_id = v_emp_id;
  
  IF v_job_id = 'SA_REP' THEN
    UPDATE employees
    SET commission_pct = commission_pct * 1.2;
  ELSE
    NULL;  -- Employee is not a sales rep
  END IF;
END;
/

7. NULL Statement as Placeholder During Subprogram Creation

The NULL statement lets you compile this subprogram and fill in the real body later.
NULL语句允许您编译此子程序,并在以后填充实际主体。

CREATE OR REPLACE PROCEDURE award_bonus (
  emp_id NUMBER,
  bonus NUMBER
) AUTHID DEFINER AS
BEGIN    -- Executable part starts here
  NULL;  -- Placeholder
  -- (raises "unreachable code" if warnings enabled)
END award_bonus;
/

8. NULL Statement in ELSE Clause of Simple CASE Statement

The NULL statement shows that you have chosen to take no action for grades other than A, B, C, D, and F.

CREATE OR REPLACE PROCEDURE print_grade (
  grade CHAR
) AUTHID DEFINER AS
BEGIN
  CASE grade
    WHEN 'A' THEN DBMS_OUTPUT.PUT_LINE('Excellent');
    WHEN 'B' THEN DBMS_OUTPUT.PUT_LINE('Very Good');
    WHEN 'C' THEN DBMS_OUTPUT.PUT_LINE('Good');
    WHEN 'D' THEN DBMS_OUTPUT.PUT_LINE('Fair');
    WHEN 'F' THEN DBMS_OUTPUT.PUT_LINE('Poor');
    ELSE NULL;
  END CASE;
END;
/
BEGIN
  print_grade('A');
  print_grade('S');
END;
/

Result:

Excellent

总结

Oracle提供了多种工具和函数来处理NULL值,使得在数据库设计和查询中能够灵活地处理缺失或未知的数据。理解并正确使用这些工具对于开发健壮、可靠的数据库应用程序至关重要。

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中读取Oracle数据库中的BLOB数据时,如果BLOB列的值为NULL,则在获取BLOB列时会返回null。因此,在对BLOB列进行操作之前,需要检查它是否为null。 以下是一个检查BLOB列是否为null的例子: ``` import java.sql.*; public class ReadBlobExample { public static void main(String[] args) { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { // 导入Oracle JDBC驱动程序 Class.forName("oracle.jdbc.driver.OracleDriver"); // 使用JDBC API连接Oracle数据库 conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "username", "password"); // 使用SELECT语句查询包含BLOB数据的表 pstmt = conn.prepareStatement("SELECT blob_column FROM table_name WHERE id = ?"); pstmt.setInt(1, 1); // 执行查询操作 rs = pstmt.executeQuery(); // 获取查询结果中的BLOB列 if (rs.next()) { Blob blob = rs.getBlob("blob_column"); if (blob != null) { // 获取BLOB数据的输入流 InputStream in = blob.getBinaryStream(); // 处理BLOB数据 // ... in.close(); } else { System.out.println("BLOB column is null"); } } } catch (Exception e) { e.printStackTrace(); } finally { // 关闭数据库连接和相关资源 try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ``` 在此代码示例中,我们首先获取查询结果中的BLOB列,然后检查它是否为null。如果BLOB列的值为null,则输出一条消息。如果BLOB列的值不为null,则获取BLOB数据的输入流并进行处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值