What is the difference between UNIQUE and PRIMARY KEY in Oracle?

Asked

Viewed 10,183 times

2

I’d like to know the difference between UNIQUE and PRIMARY KEY in the Oracle, whereas UNIQUE defines values that cannot be repeated, and PRIMARY KEY in thesis does the same thing.

  • UNIQUE it means that the attribute will not be repeated, so far it looks like a PK, but for example, I have a table where CNPJ "would be" PK, but not, because there are companies with the same cnpj but with different state registration, this field i.e would be unique, but if not necessarily the PK would be that field

3 answers

10


PRIMARY KEY defines the primary key of the table. That is, what set of columns should identify a tuple uniquely and unambiguously. In addition, the table is usually organized internally according to how the primary key is defined. Also, what is exported to other tables as foreign key is the primary key.

However, there may be more than one way to identify a tuple in a unique way, and that’s where the UNIQUE enters.

For example, let’s assume that a table usuario has the following fields: name, id, CPF, RG, e-mail and date of birth.

Let’s see, the id would be the primary key. However, we cannot have two users with the same email, so the email is UNIQUE. Nor can we have two users with the same RG or CPF, which are UNIQUE also.

Also, once in a while a person can change their ID. If the RG was the primary key, you would have to cascade the changes in all foreign keys, problem that does not exist in columns UNIQUE other than PRIMARY KEY.

Another difference is that columns UNIQUE may be NULL, while columns PRIMARY KEY, cannot. For example, in this case I gave above, this would be useful if you are registering a foreigner who has no CPF and no ID. If one of these were primary key, you’d be screwed.

  • Very good answer.

  • Thank you so much for your help Victor.

2

As said at that link of SOEN

The primary key:

  • There may be only one for table
  • In Oracle it cannot be NULL
  • It is unique and is the main means of relating Tables desired.

The key Unica:

  • There may be more than one in one table

  • Can receive values NULL

  • How can it be NULL she may not be "single"

In addition, the use of primary and foreign key guarantees, guarantee the compliance and integrity of the data and the Tables EX: You cannot delete a table or one data that is dependent on another, which prevents a possible non-conformity.

  • unique there may be more than one attribute of this type per table, but there can be no more than one record with the same value ... that is precisely why it is called so

  • @Sneepsninja The NULL value can be repeated

  • the concept of NULL is no value, so there is no "value" NULL (proof of this you cannot make a select * from tab where atributo=null, for this reason a PK cannot be NULL, as PK has an obligation to have a "value" so it cannot be NULL

  • what I mean is that the answer is not wrong, only lacked argue why the unique allows null, and does not allow repeated "values"

1

Primary Key:

  • There can be only one in a table
  • Primary Key is a unique identifier of a record in a table

Unique Key:

  • Are unique by table record
  • There may be more than one Unike Key in a table
  • Allow null values
  • Is a candidate to become a Primary Key
  • One Unique Key can be null and ,in case of null, non-exclusive to the registration

source: https://stackoverflow.com/a/13349176/4166090

  • unique there may be more than one attribute of this type per table, but there can be no more than one record with the same value ... that’s precisely why it’s called...

  • @Sneepsninja that’s right, I squeezed me wrong

Browser other questions tagged

You are not signed in. Login or sign up in order to post.