2

I am trying to add some values in a table with a Select procedure for 1 value like this.

INSERT INTO product(productname, productprice, productcategorynumber) 
VALUES ('12', '12', (SELECT productcategorynumber 
                     FROM product_category 
                     WHERE productcategoryname = 'DRINKS'));

I Receive the following error:

duplicate key value violates unique constraint

thanx for help

3
  • Which fields have a unique constraint? Commented Jun 23, 2013 at 16:27
  • Your syntax should work fine. The error is about the values you're trying to insert. Perhaps the productname is unqiue, and there already is a row with productname = '12' Commented Jun 23, 2013 at 16:31
  • Are you using oracle ? Commented Jun 23, 2013 at 16:35

2 Answers 2

1

No need for values:

INSERT INTO product(productname, productprice, productcategorynumber) 
SELECT '12', '12', productcategorynumber 
FROM product_category 
WHERE productcategoryname = 'DRINKS';

I'm presuming these are not the actually values you want, but it'll hopefully put you on the right track. (Is there a unique constraint on productname? If so, you need to adjust the query accordingly.)

Sign up to request clarification or add additional context in comments.

Comments

0

There is a constraint on one of the fields :(productname, productprice, productcategorynumber). Check your table to see which field has a unique constraint on it and then you need insert distinct value for that field.

Here is a statement which will list the constraints for you in PostgreSQL:

SELECT  constraint_name, constraint_type FROM information_schema.table_constraints
WHERE table_name = 'product'

you should also be able use the below command to see the constraints for this product table

\d product 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.