SQL NOT NULL Constraint in Oracle: What Happens When You Insert a NULL Value?

When designing a database table, not every column should accept missing information.
For example, think about an employee database. Every employee must have an employee ID and a name. If the employee name is missing, the record may not be useful for the application or business.
This is where the NOT NULL constraint becomes important.
In this tutorial, we will understand how the NOT NULL constraint works in Oracle SQL, how to create a table using it, and what happens when we try to insert a NULL value into a mandatory column.
- Understanding the NOT NULL Constraint
The NOT NULL constraint prevents a column from storing NULL values.
It is useful when a particular column must always contain a value.
Some common examples include:
- Employee name
- Customer name
- Order date
- Product name
- Department name
For example, an employee table should not allow an employee record without a name if the name is mandatory for the application.
- Create an Employee Table
Let's create an employee table with three columns:
CREATE TABLE employees (
employee_id NUMBER PRIMARY KEY,
employee_name VARCHAR2(50) NOT NULL,
salary NUMBER
);
Here is what each constraint does:
- "PRIMARY KEY" ensures that every employee has a unique employee ID and does not allow NULL.
- "NOT NULL" ensures that employee_name cannot contain NULL values.
- "salary" does not have a NOT NULL constraint, so it can store NULL.
- Insert Some Employee Records
Let's insert three employees into the table.
INSERT INTO employees
VALUES (101, 'Rahul', 45000);
INSERT INTO employees
VALUES (102, 'Priya', 55000);
INSERT INTO employees
VALUES (103, 'Amit', 60000);
Now, let's check the records:
SELECT * FROM employees;
Expected Output:
EMPLOYEE_ID| EMPLOYEE_NAME| SALARY
101| Rahul| 45000
102| Priya| 55000
103| Amit| 60000
All three records are inserted successfully because every employee has a name.
- What Happens When You Insert NULL?
Now, let's try inserting another employee.
INSERT INTO employees
VALUES (104, NULL, 50000);
Look carefully at the INSERT statement.
- Employee ID is 104.
- Employee name is NULL.
- Salary is 50000.
The employee ID is new, and the salary is provided, but the employee name is missing.
Will Oracle insert this record?
No. The INSERT statement will fail.
Oracle returns an error similar to:
ORA-01400: cannot insert NULL into
("YOUR_SCHEMA"."EMPLOYEES"."EMPLOYEE_NAME")
Why does this happen?
Because the employee_name column was created with the NOT NULL constraint.
Oracle does not allow NULL values in this column.
- What Happens If You Insert an Empty String?
There is one more important point in Oracle SQL.
Consider this INSERT statement:
INSERT INTO employees
VALUES (105, '', 50000);
In Oracle, an empty string in a VARCHAR2 column is treated as NULL.
Therefore, this INSERT statement also fails because employee_name does not allow NULL values.
This is worth remembering when working with text columns in Oracle databases.
- NOT NULL vs PRIMARY KEY
Both constraints prevent NULL values, but they serve different purposes.
PRIMARY KEY| NOT NULL
Uniquely identifies a record| Ensures a value is provided
Does not allow NULL| Does not allow NULL
Prevents duplicate key values| Allows duplicate values
Only one primary key constraint per table| Multiple NOT NULL columns are allowed
Example:
- "employee_id" → PRIMARY KEY
- "employee_name" → NOT NULL
Two employees can have the same name, but they cannot have the same employee ID.
- Key Takeaways
Remember these points when designing database tables:
- NOT NULL prevents NULL values in a column.
- It is useful for mandatory information.
- A table can have multiple NOT NULL columns.
- A NOT NULL column can contain duplicate values.
- In Oracle, an empty string in a VARCHAR2 column is treated as NULL.
An INSERT statement that violates a NOT NULL constraint is rejected.
Test Your SQL Knowledge
Imagine you have created this table:
CREATE TABLE employees (
employee_id NUMBER PRIMARY KEY,
employee_name VARCHAR2(50) NOT NULL,
salary NUMBER
);
Now, you execute:
INSERT INTO employees
VALUES (104, NULL, 50000);
What will happen?
A. The record will be inserted with a NULL employee name.
B. Oracle will automatically assign a default employee name.
C. The INSERT will fail because employee_name does not allow NULL values.
D. Oracle will accept the record and store an empty string instead of NULL.
Correct Answer: C
The employee_name column has a NOT NULL constraint, so Oracle rejects the INSERT statement.
Final Thoughts
The NOT NULL constraint is a small but important part of SQL table design.
It helps ensure that mandatory information is available whenever a new record is inserted or an existing record is updated.
When designing employee, customer, order or product tables, think carefully about which columns must always contain a value.
Good database design starts with maintaining good data quality.
Your Turn: Have you used NOT NULL constraints in your Oracle database projects? What other columns do you think should always have a NOT NULL constraint?
Top comments (0)