Skip to content
Advertisement

Oracle SQL Unique Constraint Violated

I need to create tables and input values. All but two rows when inserted give me an error, Unique Constraint Violated. The last row in DEPT_LOCATIONS table and the second to the last row in DEPENDENT table. I’m not sure why these rows aren’t being added because other rows in the same table are.

Here’s my code so far:

The tables should look like this: enter image description here

enter image description here

Could someone please help me understand what is going on? Thank you

Advertisement

Answer

DLOCATION varchar(30) PRIMARY KEY

insert into DEPT_LOCATIONS values(‘1’, ‘Houston’);

insert into DEPT_LOCATIONS values(‘5’, ‘Houston’);

The DLOCATION column is a primary key, but you are trying to insert duplicate values 'Houston' into the table. Therefore it throws unique constraint violated error. To overcome this, you could create it as a composite primary key with DNUMBER and DLOCATION together as:

DDL & inserts:

Output:

Similarly, you need to do the same for rest of the tables which are failing due to unique constraint error.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement