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

╄→尐↘猪︶ㄣ 提交于 2019-12-24 02:23:29

问题


"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;

回答1:


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.




回答2:


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;



回答3:


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.



来源:https://stackoverflow.com/questions/20817280/ora-01400-and-ora-02296-cannot-insert-null-or-modify-added-column-properties-t

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!