Difference Analysis Based on unique and primary constraints

Source: Internet
Author: User

Fields defining the UNIQUE constraint cannot contain duplicate values. You can define a UNIQUE constraint for one or more fields. Therefore, UNIQUE can be defined at the field or table level, fields with UNIQUE constraints can contain null values. ORACLE automatically creates a unique index and a not null constraint for fields with the primary key constraint (PRIMARY code segment). When defining the primary key constraint, it can be its index; UNIQUED can be empty, one or more fields in a table can be defined;

Primary key cannot be empty and cannot be repeated. You can define a joint primary key in a table. In short, primary key = unique + not null
Unique is unique. It is used when you need to limit that each value of a table field is unique and there are no duplicate values. for example, if you have a person_Info table with a column ID card, you can specify this field unique.
From a technical point of view, there are many similarities between Primary Key and Unique Key. However, there are the following similarities and differences:
Same: they all belong to entity integrity constraints.

Differences:

(1) The column where the uniqueness constraint is located allows null values, but the column where the primary key constraint is located does not allow null values.

(2) You can place the uniqueness constraint on one or more columns. The combination of these columns or columns must be unique. However, the column where the uniqueness constraint is located is not the primary key column of the table.

(3) The uniqueness constraint forces the creation of a unique index on the specified column. By default, a unique non-clustered index is created. However, you can also specify that the created index is a clustered index.

(4) The primary key is created so that the foreign key can be referenced.

(5) A table can have at most one primary key, but it can have many unique keys.

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.