Unique constraints and unique indexes

Source: Internet
Author: User

There was no deliberate attempt to understand the difference between a unique constraint and a unique index, except that a unique index is automatically created after the unique constraint has been built, and the corresponding unique index is automatically deleted after the unique constraint is removed.

Today, we have this problem in our work, and the differences are listed as follows:

1. Create a different script:

--Creating a UNIQUE constraint can be specified when the table is built:CREATE TABLE [dbo].[table_nm ]([CODE]CHAR(4) not NULL, CONSTRAINT[Uk_code]UNIQUE nonclustered([CODE]),)--Creating a Unique constraint can also be increased by using ALTER TABLE:ALTER TABLE [dbo].[table_nm] ADD CONSTRAINT [Uk_code] UNIQUE nonclustered (    [CODE] ASC)--To create a unique index script:CREATE UNIQUE nonclustered INDEX [Uk_code]  on [dbo].[table_nm](    [CODE] ASC)

2. After creation, you can see the difference in SSMs:

3. Other differences

For example, a table that participates in transactional replication must establish a primary key or a unique constraint in advance, where only the unique index will be lost.

Unique constraints and unique indexes

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.