Differences between SQL Server primary keys and UNIQUE constraints _mssql

Source: Internet
Author: User

First of all, the primary key is also called the PRIMARY KEY constraint, which is also a constraint, look at it and the creation syntax of 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 field values to be unique, except in addition to the following differences:

• The same table can have only one primary key, but can have multiple unique constraints;
• The primary key field value cannot be null, and the unique constraint field value can be null;
• Primary key fields can be foreign keys to other tables, and unique constraint fields cannot be foreign keys to other tables;
· SQL Server creates a clustered index on the default primary key field, creating a nonclustered index for the unique constraint field;

Primary key, unique, but cannot be null; Unique constraint, unique, but nullable

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.