ORA-01439: column to be modified must be empty to change datatype
ORA-01439 is one of the most common errors DBAs encounter during schema modification tasks in Oracle. It occurs when you attempt to change the data type of a column that already contains data using the ALTER TABLE ... MODIFY statement. Oracle enforces this restriction to protect existing data integrity, as it cannot automatically and safely convert existing values to a different data type without risk of data loss.
Top 3 Causes
1. Changing Data Type on a Populated Column
The most frequent cause — trying to modify a column's data type when the table contains rows with non-NULL values in that column.
-- This will throw ORA-01439 if salary column has data
ALTER TABLE employees MODIFY salary VARCHAR2(20);
-- ORA-01439: column to be modified must be empty to change datatype
2. Incompatible Type Conversion on Non-Empty Column
Attempting to convert between incompatible types (e.g., VARCHAR2 to NUMBER, or CHAR to DATE) when the column is not empty triggers this error.
-- Attempting to convert VARCHAR2 to DATE with existing data
ALTER TABLE orders MODIFY order_date DATE;
-- Fails if order_date (VARCHAR2) already has values
3. Reducing Column Size Beyond Existing Data Length
Trying to decrease the size of a column when existing data exceeds the new defined length.
-- Will fail if any first_name value is longer than 5 characters
ALTER TABLE employees MODIFY first_name VARCHAR2(5);
-- ORA-01439 or ORA-01441 depending on the situation
Quick Fix Solutions
Fix 1: Use a Temporary Column (Recommended for Production)
-- Step 1: Add a new temp column with the desired type
ALTER TABLE employees ADD (salary_new NUMBER(15,2));
-- Step 2: Copy and convert data
UPDATE employees SET salary_new = TO_NUMBER(salary);
COMMIT;
-- Step 3: Drop the original column
ALTER TABLE employees DROP COLUMN salary;
-- Step 4: Rename temp column to original name
ALTER TABLE employees RENAME COLUMN salary_new TO salary;
Fix 2: Null Out the Column First (Dev/Test Only)
-- Only use this if data loss is acceptable
UPDATE employees SET salary = NULL;
COMMIT;
ALTER TABLE employees MODIFY salary NUMBER(15,2);
Fix 3: Rebuild the Table Using CTAS
-- Create new table with correct structure
CREATE TABLE employees_new AS
SELECT
employee_id,
first_name,
TO_NUMBER(salary) AS salary
FROM employees;
-- Swap table names
ALTER TABLE employees RENAME TO employees_old;
ALTER TABLE employees_new RENAME TO employees;
-- Recreate constraints and indexes
ALTER TABLE employees ADD CONSTRAINT pk_emp PRIMARY KEY (employee_id);
Prevention Tips
1. Always verify column data before running DDL changes.
Add a pre-flight check to your deployment scripts to confirm whether the target column contains data before attempting a type change.
-- Pre-check: confirm column is empty before type change
SELECT COUNT(*) FROM employees WHERE salary IS NOT NULL;
-- Also check current column definition
SELECT column_name, data_type, data_length
FROM user_tab_columns
WHERE table_name = 'EMPLOYEES'
AND column_name = 'SALARY';
2. Test all schema change scripts in a staging environment loaded with production-like data.
Development environments often have little or no data, which means ORA-01439 won't appear until production deployment. Always maintain a staging environment populated with masked production data, and include a rollback script alongside every DDL change script to ensure fast recovery in case of failure.
📖 Want a more detailed guide?
Check out the full in-depth version (Korean) on oraerror.com — includes detailed analysis, additional SQL examples, and prevention tips.
Top comments (0)