Difference between SQLServer primary key and unique constraint, sqlserver primary key constraint

Source: Internet
Author: User

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

Related Article

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.