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