#39;Violation of PRIMARY KEY constraint#39; SQL Error(违反 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 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:'违反 PRIMARY KEY 约束' SQL 错误


- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- SQL 临时表问题 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01