Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unique Constrain violation

CREATE TABLE dept (dept_id NUMBER PRIMARY KEY, dept_name VARCHAR2(50) );

CREATE TABLE emp (emp_id NUMBER PRIMARY KEY, emp_name VARCHAR2(50), dept_id NUMBER,
CONSTRAINT emp_fk FOREIGN KEY (dept_id)
REFERENCES dept (dept_id) );

CREATE SEQUENCE myseq1 NOCACHE;
Now examine this statement:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

INSERT ALL INTO emp (emp_id, emp_name)
VALUES (myseq1.nextVal, ‘name1’) — name1 insertion
INTO dept (dept_id, dept_name)
VALUES (10, ‘dept1’) — dept1 insertion
INTO emp (emp_id, emp_name, dept_id)
VALUES (myseq1.nextVal, ‘name2’, 10) — name2 insertion

SELECT * FROM dual;

Question: when ran above, giving me error msg and unique constraint violated, can someone please explain me where and how unique key is violated?

Thanks in advance!

I tried reading oracle support document, tried running query in Oracle SQl developer.
Now I’m not getting the reason behind that error.

>Solution :

sequence.NEXTVAL in INSERT ALL is evaluated only once.
Either you change to multi INSERT, either you wrap the sequence.NEXTVAL in a function.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading