使用SQL Server 2008
创建表customerinfo脚本
CREATE TABLE [dbo].[customerinfo](
[customer#] [int] IDENTITY(1,1) NOT NULL,
[acc#] [varchar](10) NOT NULL,
CONSTRAINT [PK_unc_customerinfo] PRIMARY KEY NONCLUSTERED
(
[customer#] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [idx_u_customerinfo] UNIQUE NONCLUSTERED
(
[acct#] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
使用重置0重置标识字段,然后我尝试在表中插入行。将错误视为
Violation of UNIQUE KEY constraint 'idx_u_customerinfo'. Cannot insert duplicate key in object 'dbo.customerinfo'. The duplicate key value is (50950/01950).
如何纠正这个问题。
答案 0 :(得分:0)
首先:您的查询错误。
idx_u_customerinfo
是acc#
或acct#
上唯一的非聚集索引。 (acct#
不是你桌子上的一个字段。)
第二:检查插入的数据,当然你有多行具有相同的acc#
。
如果您使用Insert Into [dbo].[customerinfo] Select ...
查询向表中插入数据,请使用以下查询以查找重复记录:
Select acc#, count(*)
from ..
group by acc#
having count(*)<>1