我使用的是Microsoft SQL Server 2012(版本= 11.00.5388),我收到了异常
SQLState:23000,错误消息:
违反PRIMARY KEY约束' PK__MM_DISK ___ C9EB385C43CE19EE'。无法在对象' dbo.MM_DISKDATA'中插入重复的密钥。重复键值为(1484539200000,40020675,1,40611)..]
尝试将记录插入表MM_DISKDATA
时,即使条目在获取异常时也是唯一的。
示例条目:
insert into MM_DISKDATA
values('1484539200000','40020675','1','40611','79','79','948','12','-1','-1','-1','-1','-1','-1','-1','-1','-1','-1','-1','-1');
insert into MM_DISKDATA
values('1484539200000','40020676','1','40611','79','79','948','12','-1','-1','-1','-1','-1','-1','-1','-1','-1','-1','-1','-1');
MM_DISKDATA
前四列为唯一约束。
请帮忙解决这个问题。
答案 0 :(得分:0)
在数据库中运行以下查询以找出哪些列是表MM_DiskData的PK列:
SELECT C.Name
FROM sys.index_Columns IC
INNER JOIN sys.indexes I
ON IC.index_id = I.index_id
AND IC.object_id = I.object_id
INNER JOIN sys.Columns C
ON C.object_id = I.object_id
AND C.column_id = IC.column_id
WHERE OBJECT_NAME(IC.object_id) = 'MM_DISKDATA'
AND I.is_primary_key = 1
获得该列表后,您将能够看到哪些列可能导致您的问题。
为了澄清一下,您可能希望在INSERT
语句中列出接收数据的列,如下所示:
INSERT INTO MM_DiskData ([Col1],[Col2],[Col3],[Col4],...[Colx])
VALUES ('1484539200000','40020675','1','40611',...'-1')
如果将您尝试INSERT
的值列表与上面发布的查询返回的值列表进行比较,则应该指出错误被抛出的原因。