Difference between SQLServer primary key and unique constraint, sqlserver primary key constraint
First of all, the primary key is also called a primary key constraint. It is also a constraint. Let's look at the creation Syntax of it and the unique constraint:
Alter table Person add constraint PK_Id primary key (Id) alter table Person add constraint UQ_Name unique (Name)
Both primary keys and unique constraints require that the field values be unique. In addition, they have the following differences:
· One table can have only one primary key, but multiple unique constraints;
· The primary key field value cannot be NULL. The unique constraint field value can be NULL;
· The primary key field can be used as the foreign key of other tables, and the unique constraint field cannot be used as the foreign key of other tables;
· SQLServer creates clustered indexes for primary key fields by default and non-clustered indexes for unique constraint fields;
Primary Key, unique, but not empty; unique, unique, but can be empty