'违反 PRIMARY KEY 约束' SQL 错误

#39;Violation of PRIMARY KEY constraint#39; SQL Error(违反 PRIMARY KEY 约束 SQL 错误)
本文介绍了'违反 PRIMARY KEY 约束' SQL 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个很常见的问题,但是到目前为止我尝试过的所有事情都没有解决方案.我正在为我的 PK 使用 ID 字段,并且自动增量已打开.这发生在数据已与当前数据合并的 DEV 环境中.

This seems to be a pretty common question that is asked but, everything I have tried so far has left with with no solution. I am using an ID field for my PK and auto increment has been turned on. This is occurring in a DEV environment where data has been merged with the current data.

任何帮助将不胜感激.

我正在使用 SQL Server.我还运行了 DBCC CHECKIDENT([ceschema.ce_attendeeCredit]) 并且身份似乎正确排列.我认为可能存在更深层次的问题.

I am using a SQL Server. I have also ran DBCC CHECKIDENT([ceschema.ce_attendeeCredit]) and the identity seems to line up properly. I think there may be a deeper issue in play.

INSERT INTO tblpersonCredit
                (
                personID,
                CreditID,
                Amount,
                ReferenceNo,
                CreatedBy
                )
            VALUES
                (
                <cfqueryparam value="#arguments.AttendeeCredit.getAttendeeID()#" CFSQLType="cf_sql_integer" />,
                <cfqueryparam value="#arguments.AttendeeCredit.getCreditID()#" CFSQLType="cf_sql_integer" />,
                <cfqueryparam value="#arguments.AttendeeCredit.getAmount()#" CFSQLType="cf_sql_float" null="#not len(arguments.AttendeeCredit.getAmount())#" />,
                <cfqueryparam value="#arguments.AttendeeCredit.getReferenceNo()#" CFSQLType="cf_sql_varchar" null="#not len(arguments.AttendeeCredit.getReferenceNo())#" />,
                <cfqueryparam value="#arguments.AttendeeCredit.getCreatedBy()#" CFSQLType="cf_sql_integer" />
                )

推荐答案

我在你的问题中注意到了这一行:

I noticed this specific line in your question:

这发生在数据已与当前数据合并的 DEV 环境中.

This is occurring in a DEV environment where data has been merged with the current data.

手动将数据插入自动增量字段后,您可能需要将自动增量计数器重置为您插入的最大 id 之后的值.

After manually inserting data into an autoincrement field you may need to reset the autoincrement counter to something after the maximum id that you inserted.

如何执行此操作(以及是否需要)取决于数据库.例如在 MySQL 中,您可以使用 ALTER TABLE 语句:

How you do this (and whether you need to) is database dependent. For example in MySQL you can use an ALTER TABLE statement:

ALTER TABLE tbl AUTO_INCREMENT = 10000

重置 SQL Server IDENTITY 列的种子的等效命令是:

The equivalent command to reset the seed for an SQL Server IDENTITY column is:

DBCC CHECKIDENT(tbl, RESEED, 9999)

这篇关于'违反 PRIMARY KEY 约束' SQL 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)