while updating a row: Cannot insert duplicate key

2020-07-17 14:42发布

问题:

when UPDATEing a row I get the above error. idxRelatiesoortRelatiecode consists of fldRelatieSoort and fldRelatieSoort and the combination of both exists only once in the table.

So what could be the reason for this error?

Update Here is the update script

UPDATE [SQL].[MyDatabase].dbo.tblRelatie SET 
fldNaam = 'De heer A. Removed', 
fldAdres = 'Removed 12', 
fldPostcode = '1234 AA', fldPlaats = 'Removed', 
fldCorrespondentieAdres = 'Removed 12', 
fldCorrespondentieAdresPostcode = '1234 AA', 
fldCorrespondentieAdresPlaats = 'Removed', 
fldRelatieSoort = 1,
fldRelatiecode = 907534, 
fldCorrespondentieAdresLandID = 1, fldMobieleTelefoon = '', fldTelefoon = '', fldFax = '',  fldEmail = '', fldWebsiteUrl = '', fldBankrekeningnummer = '', fldNaamRekeninghouder = '', 
fldPlaatsRekeninghouder = '', fldKrediettermijn = 30, fldLandID = 1 
WHERE fldRelatieID =1507;

and this is the idxRelatiesoortRelatiecode:

CREATE UNIQUE NONCLUSTERED INDEX [idxRelatiesoortRelatiecode] 
ON [dbo].[tblRelatie] 
(
    [fldRelatieSoort] ASC,
    [fldRelatiecode] 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, FILLFACTOR = 90) ON [PRIMARY]

回答1:

There isn't much ambiguity in the error message: you are setting a duplicate somewhere

  • Either: the combination already exists and you are trying to insert it again
  • Or: it doesn't exist and you are updating multiple rows with the same combination

Or the overlap:

  • the combination already exists and you are updating multiple rows with the same combination

Unless the index or such is wrong...