Analysis based on the difference between unique and primary constraints

  • 2020-05-17 06:42:11
  • OfStack

Defines the constraints UNIQUE field cannot contain duplicates, one or more fields to define UNIQUE constraints, therefore, UNIQUE namely at the field level can also be defined in table level, on the field UNIQUE constraint can contain null values. Will automatically with PRIMARY ORACLE KEY constrained fields (main code field) to establish index of 1 to 1 and 1 NOT NULL constraints, define PRIMARY KEY constraints for its index; UNIQUED can be null and can be defined in one or more fields in one table;

PRIMARY KEY is non-null and non-repeatable, and a joint primary key can be defined in one table. Simply put, primary key = unique + not null
For example, if you have an person_Info table and there is an column id in the table, you can specify the unique field.
From a technical point of view, Primary Key and Unique Key have many similarities. But there are similarities and differences:
Same: they are both entity integrity constraints.

Difference:

(1) null values are allowed in the column where the uniqueness constraint is located, but null values are not allowed in the column where the primary key constraint is located.

(2) a singleton constraint can be placed on one or more columns, and these columns or combinations of columns must have singleton. However, the column of the singleton constraint is not the primary key column of the table.

(3) the singleton constraint forces the creation of a singleton index on a specified column. By default, a unique non-clustered index is created, but it is also possible to specify that the index created is a clustered index.

(4) the primary key is established for foreign keys to refer to.

(5) a table can have at most one primary key, but can have many one-only keys


Related articles: