Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any reason to use both primary key and unique key together on the same field?

Tags:

oracle

I am analyzing an Oracle database design and I am perplexed at seeing both unique keys and primary keys on the same fields. These unique-primary key pairs are consistently created on all tables. I see no reason to do this.

If I have a primary key anyway, is there a good reason to create an additional unique key on the same field?

like image 768
Acamboos Acambo Avatar asked Aug 28 '11 22:08

Acamboos Acambo


People also ask

Can we use primary key and unique key in same table?

A table can have only one primary key whereas there can be multiple unique key on a table.

Do you need a primary key if you have a unique key?

Unique keys are used when we want to identify specific items, and do not want any duplicate values. Primary keys are also used to uniquely identify items, but every row must have a primary key, while it is possible for the row to be lacking a unique key.


1 Answers

For a table resolving a many-to-many it would be common to have a two part key (as indicated by Quassnoi). It is also quite likely to need indexes supporting access through either parent.

If you have, for example, PERSON, ADDRESS and PERSON_ADDRESS tables, your PERSON_ADDRESS table may have the primary key of (PERSON_ID, ADDRESS_ID) and a supporting index. You would also have another index on (ADDRESS_ID,PERSON_ID), and you would likely make this a UNIQUE index (as it is a unique combination of fields).

It is also possible that your DBA has some particular way of generating tables that starts with a UNIQUE index on the primary key fields followed by the creation of the PRIMARY KEY constraint. That may show up in some GUI tools in the way you suggest.

like image 77
Gary Myers Avatar answered Oct 13 '22 12:10

Gary Myers