ORA 01400 and ORA 02296 : Cannot insert null or modify added column properties to NOT NULL

StackOverflow https://stackoverflow.com/questions/20817280

  •  22-09-2022
  •  | 
  •  

Question

"Modify your query to add a column that subtracts the old salary from the new salary. Label the column Increase. Run the revised query."

Well, as per my interpretation, I first attempted to add the column by scripting:

ALTER TABLE EMPLOYEES ADD (
    INCREASE2   NUMBER(6));

Then:

INSERT INTO EMPLOYEES(INCREASE2)
SELECT (salary*1.155) - salary FROM EMPLOYEES;

Error de SQL: ORA-01400: não é possível inserir NULL em ("HR"."EMPLOYEES"."EMPLOYEE_ID") 01400. 00000 - "cannot insert NULL into (%s)"

"HR"."EMPLOYEES"."EMPLOYEE_ID" is the primary key.

  1. I'm not trying to insert a NULL value;
  2. I don't know why oracle isn't accepting my entries. I tried to check whether there was any syntax errors in my expression by performing basic insert:

    INSERT INTO EMPLOYEES(INCREASE2)
    VALUES ('whatever');
    

And still I got the error.

I tried then modifying the column to not null

ALTER TABLE EMPLOYEES
MODIFY
(INCREASE2  NUMBER(6) NOT NULL);

And:

02296 00000 - "cannot enable (%s.%s) - null values found"
*Cause: an alter table enable constraint failed because the table
contains values that do not satisfy the constraint.
*Action: Obvious

I found a simple solution for the exercise, but still I am curious about why my code didn't succeed.

Resolution:

SELECT employee_id, last_name, salary,
    ROUND(salary * 1.155, 0) "New Salary",
    ROUND(salary * 1.155, 0) - salary "Increase"
FROM employees;
Was it helpful?

Solution

Your code didn't succeed because the column employees.employee_id is a non-null field without a default value. When you run:

INSERT INTO EMPLOYEES(INCREASE2)
    VALUES ('whatever');

The values of all the other fields in Employees are assigned the default, or NULL if no default value exists. Because this violates a constraint, you get an error.

Normally, a field like employee_id would be assigned to a sequence. This would automatically insert an auto-incremented value for each new record.

OTHER TIPS

Wouldn't this be an update, not an insert? Inserting new records makes no sense to me in the context of populating a new column that you have just added.

update employees
set increase2 = ROUND(salary * 1.155, 0)  - salary;

I assume you missunderstood the task: "Modify your query to add a column that subtracts the old salary from the new salary. Label the column Increase. Run the revised query."

So your last resolution should be fine, you don't have to alter the actual table.

Using NOVALIDATE clause you can overcome this error ORA-02296 I am trying to modify table data type NULL to NOT NULL its gives error after using NOVALIDATE the table is altered.see code for more information

alter table emp11 modify eid not null novalidate;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top