Sub topic 4
Requiring Values with `NOT NULL`
This chapter assumes that you know basic SELECT, INSERT, UPDATE, and DELETE, and that you understand tables, rows, and columns. You should also have access to Oracle Database 12c or later.
A NOT NULL constraint is a rule that requires a column to contain a value. It prevents both inserted and updated rows from storing NULL in that column. NULL means that a value is missing or unknown. It is different from zero, an empty string, or a false value.
Add `NOT NULL` in `CREATE TABLE`
Place NOT NULL after the column's data type. This is an inline constraint because it appears in the column definition.
CREATE TABLE employees (
employee_id NUMBER,
email VARCHAR2(255)
CONSTRAINT employees_email_nn NOT NULL,
hire_date DATE DEFAULT SYSDATE
CONSTRAINT employees_hire_date_nn NOT NULL
);The constraint names make errors and maintenance easier. If you omit a name, Oracle generates one, such as SYS_C.... Generated names are harder to recognize in error messages and data dictionary queries.
The following inserts show which values are accepted:
INSERT INTO employees (employee_id, email)
VALUES (1, '[email protected]');
-- The default supplies the current date.
INSERT INTO employees (employee_id, email, hire_date)
VALUES (2, '[email protected]', NULL);The second statement fails. A regular column default is used when the column is omitted, but an explicitly supplied NULL remains NULL. The hire_date constraint therefore rejects it.
Use this statement when you want the default to apply to an explicit NULL as well:
CREATE TABLE contractors (
contractor_id NUMBER GENERATED BY DEFAULT AS IDENTITY,
start_date DATE DEFAULT ON NULL SYSDATE NOT NULL
);Oracle treats a zero-length character literal as NULL for VARCHAR2 and CHAR values. Therefore, this statement also violates employees_email_nn:
INSERT INTO employees (employee_id, email)
VALUES (3, '');Understand how Oracle checks the rule
Oracle checks NOT NULL when a row is inserted or when a column is updated. Other columns in the same row can remain nullable.
flowchart TD
A[INSERT or UPDATE] --> B{Required column has a value?}
B -->|Yes| C[Statement can continue]
B -->|No: NULL or empty string| D[Oracle raises an error]A NOT NULL column does not need a value supplied by every statement. A default can provide the value when the column is omitted.
| Statement | Result |
|---|---|
| Omit a column with a regular default | Default value is stored |
| Supply `NULL` to a regular default column | `NULL` is checked and rejected |
| Omit a `NOT NULL` column without a usable default | Insert fails |
| Update a `NOT NULL` column to `NULL` | Update fails |
Add `NOT NULL` to an existing table
Use ALTER TABLE ... MODIFY to change an existing column:
ALTER TABLE employees
MODIFY (email CONSTRAINT employees_email_nn NOT NULL);This succeeds only if every existing row already has a non-NULL email. If rows contain NULL, correct them first:
UPDATE employees
SET email = '[email protected]'
WHERE email IS NULL;
ALTER TABLE employees
MODIFY (email CONSTRAINT employees_email_nn NOT NULL);Choose a replacement that is meaningful for the application. A placeholder can satisfy the database rule while still creating poor data.
Inspect `NOT NULL` constraints
Oracle commonly represents NOT NULL constraints as check constraints in USER_CONSTRAINTS. The SEARCH_CONDITION column shows the rule.
SELECT constraint_name,
constraint_type,
status,
search_condition
FROM user_constraints
WHERE table_name = 'EMPLOYEES'
AND constraint_name = 'EMPLOYEES_EMAIL_NN';Name conventions such as _nn for NOT NULL constraints make these queries easier to read.
Exercise
What happens when an INSERT omits a NOT NULL column that has a regular DEFAULT, and what happens when the INSERT explicitly supplies NULL?
Show answer
Omitting the column stores its default value. Explicitly supplying NULL stores NULL for checking, so the NOT NULL constraint rejects the statement.