Quantcast
Channel: SQL Server Replication forum
Viewing all articles
Browse latest Browse all 4054

Not all entries are unique in UNIQUE non-clustered index ?

$
0
0

Hello,

I was trying to reproduce a issue but got puzzled when I was able to insert duplicate records in a table with UNIQUE non-clustered index, i expected it to fail. Another unique NCI was on a column with PK, insert query with duplicate primary key fails as expected. While I am aware of IGNORE_DUP_KEY option can be set to ON/OFF, but it can't prevent duplicate records. 

How exactly SQL Server does ensure uniqueness of unique index as I don't see any key (see below screenshots) created for same unlike primary key ? 

Script 

CREATE TABLE [dbo].[Test_table103](
[id] [int]  PRIMARY KEY NONCLUSTERED,
[NAME] [varchar](40) ,
        [Address] [varchar](40) )  

/****** Object:  Index [NonClusteredIndex-20161230-173558]    Script Date: 2016-12-30 17:38:01 ******/
CREATE UNIQUE NONCLUSTERED INDEX [NonClusteredIndex-20161230-173558] ON [dbo].[Test_table103]
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, 
ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO

        ---Inserting data
INSERT INTO Test_table103 VALUES (15, 'amit','address1')
INSERT INTO Test_table103 VALUES (11, 'abhi','address1')
INSERT INTO Test_table103 VALUES (12, 'ankit','address1')
INSERT INTO Test_table103 VALUES (13, 'anish','address1')
GO

-- Inserting duplicate record with different id and duplicate name 
INSERT INTO Test_table103 VALUES (16, 'amit','address1')
GO
-- Inserting duplicate record with different name and duplicate id
INSERT INTO Test_table103 VALUES (10, 'anuj','address1')

Appreciate your pointed revert. Thank you.

 


Viewing all articles
Browse latest Browse all 4054

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>