Oracle PL/SQL - Variable NOT NULL Constraint

Introduction

You can add the NOT NULL constraint on a scalar variable or constant.

You can also add NOT NULL constraint on scalar component of a composite variable.

The NOT NULL constraint prevents assigning a null value to the variable.

The variable can have this constraint either implicitly from its data type or explicitly.

A NOT NULL scalar variable declaration must assign an initial value to the variable because the default initial value for a scalar variable is NULL.

In the following code, the variable my_id has the NOT NULL constraint explicitly, and the variables a, b, and c acquire it from their data types.

Demo

SQL>
SQL> DECLARE-- from  ww w . j a  va  2  s.co m
  2    my_id INTEGER(4) NOT NULL := 9999;
  3    a     NATURALN            := 9999;
  4    b     POSITIVEN           := 9999;
  5    c     SIMPLE_INTEGER      := 9999;
  6  BEGIN
  7    DBMS_OUTPUT.PUT_LINE(my_id);
  8    DBMS_OUTPUT.PUT_LINE(a);
  9    DBMS_OUTPUT.PUT_LINE(b);
 10    DBMS_OUTPUT.PUT_LINE(c);
 11    NULL;
 12  END;
 13  /
9999
9999
9999
9999

PL/SQL procedure successfully completed.

SQL>

Related Topic