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

1

I would like to know the difference between UNIQUE and PRIMARY KEY in Oracle , where UNIQUE defines values that can not be repeated, and PRIMARY KEY in thesis does the same thing.

    
asked by anonymous 16.01.2017 / 16:50

3 answers

7

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

However, there may be more than one way to uniquely identify a tuple, and that is where UNIQUE enters.

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

Let's see, id would be the primary key. However, we can not have two users with the same email, so the email is UNIQUE . Also we can not have two users with the same RG or the same CPF, which are UNIQUE as well.

In addition, from time to time a person can change RG. If the RG was the primary key, you would have to come out cascading the changes on all the foreign keys, a problem that does not exist in columns UNIQUE that are not PRIMARY KEY .

Another difference is that UNIQUE columns can be NULL , whereas PRIMARY KEY columns can not. For example, in that case I gave up, this would be useful if you are to register a foreigner who does not have a CPF and RG. If one of these was a primary key, you would get screwed.

    
16.01.2017 / 16:58
1

As the SOEN

The primary key:

  • There can be only one per table
  • Oracle can not be NULL
  • Is unique and is the primary means of relating the tables .

The unique key:

  • There may be more than one in a table

  • You can get NULL values

In addition, the use of primary and foreign guarantors ensure compliance and integrity of the data and the structure of tables EX: You can not delete a table or a data that is dependent on another, which prevents any nonconformity.

    
16.01.2017 / 17:08
1

Primary Key :

  • There can only be 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 Unike Key in a table
  • Allow null values
  • You are a candidate to become a Primary Key
  • A Unique Key can be null and, in the case of null, not unique to the registry

source: link

    
16.01.2017 / 17:04