UNIQUE Key and Primary key difference

Source: Internet
Author: User
Tags one table

A field that defines a unique constraint cannot contain duplicate values, and you can define a unique constraint for one or more fields, so unique can be defined at the field level or at the table level.
You can include null values on fields that are uniqued constrained. Note that you can include null values


Oracle automatically establishes a unique index and a NOT NULL constraint for a field with a PRIMARY KEY constraint (the primary Code field), which can be indexed when the primary KEY constraint is defined

Uniqued nullable, one or more fields that can be defined in one table
PRIMARY key is not empty and cannot be duplicated, in a table you can define a joint primary key

Simply put, the primary key = unique + NOT NULL
Unique is unique when you need to qualify one of your table fields for each value that is unique and has no duplicate values.
For example, if you have a person table and you have an ID column in the table, you can specify that the field is unique.

When you define a foreign key, the field referenced can only be a field that has (primary key or unique), and the primary key differs from the index of the unique field

Unique can contain null values, and a table can have multiple unique constraint

And PrimaryKey can only have one in a table. and cannot have null values.

From a technical point of view, there are many similarities between the Primary key and the unique key. But there are still the following differences:
A domain/domain group that is a primary key cannot be null. And the unique key can.
There can be only one primary key in a table, and multiple unique keys can exist at the same time.
The larger difference is in the logical design. Primary key is typically used as a record identifier in the logical design, which is also the setting
The original intention of Primary key. The unique key is only to guarantee the uniqueness of the domain/domain group.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.