0

In my project i'm using 3 primary keys in a table which will be entries of 3 drop downs in a form and other fields. The other fields will be different but the entries of 3 drop downs will be same for multiple records. But i'm getting exception "duplicate key value". Is there any way to add duplicate entries to primary keys in sql server.

Joy
  • 61
  • 1
  • 2
  • 8
  • 3
    By definition, you cannot have duplicate primary key values. Why have you set up the composite key like this? What's the business requirement in your app -- could you use a surrogate key (sequence/UUID) instead? – Mick Mnemonic May 29 '16 at 09:20
  • An RDBMS table can have many UNIQUE KEYs that can be referenced by FOREIGN KEYs. One of those unique keys can be specified as being the PRIMARY KEY. Implicit in that is that it is **unique**, i.e. doesn't allow duplicate values. Thus is the definition, so of course you get a "duplicate key value" error if you try to insert a duplicate value. That's the way it's supposed to work. Try googling [`what is a primary key`](https://www.google.com/search?q=what+is+a+primary+key). All the articles uses the word "unique". – Andreas May 29 '16 at 09:31

1 Answers1

0

You can only use one primary key, not 3 as you state. That primary key can be a composite and contain 3 fields, but it's still one key. See the link below for confirmation;

Can I have multiple primary keys in a single table?

You can have multiple unique keys which stand alone, see the details below;

http://www.w3schools.com/sql/sql_unique.asp

You'd have to script your table to see which of these you have. If it is the composite primary key then the answer is no, you cannot have duplicates, that's the point of the primary key.

Community
  • 1
  • 1
Rich Benner
  • 7,873
  • 9
  • 33
  • 39