If you come to Oracle from MySQL, PostgreSQL or SQL Server, this one bites sooner or later. A customer has no phone number, so the app sends an empty string '', and the INSERT fails with ORA-01400: cannot insert NULL into.
In Oracle, a zero-length VARCHAR2 string is NULL. I reproduced it on Oracle AI Database 26ai Free (23.26.3) and ran the check and the fix. All output below is real SQL*Plus output.
Reproduce
A table where phone is NOT NULL:
CREATE TABLE customers (
cust_id NUMBER PRIMARY KEY,
name VARCHAR2(20) NOT NULL,
phone VARCHAR2(20) NOT NULL
);
The app sends '' to mean "no phone":
INSERT INTO customers (cust_id, name, phone) VALUES (1, 'KIM', '');
INSERT INTO customers (cust_id, name, phone) VALUES (1, 'KIM', '')
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("LAB_A"."CUSTOMERS"."PHONE")
You never passed NULL, yet Oracle says NULL.
Check: is '' really NULL?
SELECT CASE WHEN '' IS NULL THEN 'NULL' ELSE 'NOT NULL' END AS empty_string,
NVL(LENGTH(''), -1) AS len
FROM dual;
EMPTY_ST LEN
-------- ----------
NULL -1
'' IS NULL is true, and LENGTH('') is NULL too (shown as -1 through NVL). Oracle does not distinguish '' from NULL.
Fix: decide whether the column really needs a value
'' will never pass a NOT NULL constraint, so there are two options:
- If the value is optional, allow NULL
- If it is required, make the app send a real value
Here the phone is optional, so the column now allows NULL:
ALTER TABLE customers MODIFY phone NULL;
Table altered.
Same INSERT again:
INSERT INTO customers (cust_id, name, phone) VALUES (1, 'KIM', '');
SELECT cust_id, name, NVL(phone, '(null)') AS phone FROM customers;
1 row created.
CUST_ID NAME PHONE
---------- ------------------------------ --------------------
1 KIM (null)
It went in, but what is stored is NULL, not an empty string.
Summary
- In Oracle,
''(a zero-length VARCHAR2) is NULL -
''into a NOT NULL column raises ORA-01400 -
'' IS NULLconfirms it - Optional value: allow NULL. Required value: send a real one
I keep the other ORA-01400 causes (column left out, explicit NULL despite a DEFAULT, NULL into an identity column, and more), each reproduced and fixed on a real database, here:
https://errorfixlab.96ssg.com/en/ora-01400/
This post was written with the help of generative AI, based on output actually run in a test environment.
Top comments (0)