SQL 更新:无法更改第一个记录的复合键值之一

发布于 2024-11-04 12:52:47 字数 973 浏览 5 评论 0原文

MSSQL Server中,我有一个表StudentCourse,带有复合主键StudentIDCourseID< /em>)。我正在尝试将所选学生更改为另一门课程。每个课程组的一个学生记录阻止我进行UPDATE操作。

StudentID CourseID

   1          1
   1          2
   1          3
   2          2
   2          3
   2          4

我可以将 (1, 2)(1, 3) 记录的 CourseID 更新为 5,但我无法将(1, 1)记录的CourseID更新为5。类似地,我可以将 (2, 2)(2, 3) 记录的 CourseID 更新为 5,但我无法将(2,4)记录的CourseID更新为5

此类 CourseID 组中只有一条记录阻止我更改其 CourseID 字段。我收到以下错误。

违反主键约束 “PK_学生课程”。无法插入 对象中的重复键 “学生课程”。声明中有 已被终止。

我不知道每个组的第一个或最后一个记录禁止我更改CourseID。我确信在 StudentCourse 表中没有 CourseID = 5 的记录,并且我有 CourseID5< 的课程记录/em> 在课程表中。

任何帮助将不胜感激。

In MSSQL Server, I have a table StudentCourse with a Composite Primary Key (StudentID, CourseID). I am trying to change the selected student to another course. One student record of each course group is preventing me to do UPDATE operation.

StudentID CourseID

   1          1
   1          2
   1          3
   2          2
   2          3
   2          4

I can update (1, 2), (1, 3) records' CourseID to 5, but I can't update (1, 1) record's CourseID to 5. Similary, I can update (2, 2), (2, 3) records' CourseID to 5, but I can't update (2,4) record's CourseID to 5.

Only one record of such CourseID group is preventing me to change its CourseID field. I am getting the following error.

Violation of PRIMARY KEY constraint
'PK_StudentCourse'. Cannot insert
duplicate key in object
'StudentCourse'. The statement has
been terminated.

I don't know it is first or last record of each group prohibits me to change CourseID. I am sure there is no record with CourseID = 5 in StudentCourse table, and I have a course record with CourseID of 5 in Course table.

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光病人 2024-11-11 12:52:47

您看到的错误意味着您正在尝试创建与另一个现有记录具有相同主键值的记录。您在这里犯了一个错误,但您没有提供足够的信息来了解您的错误是什么。

当我遇到问题时,我发现创建一个小重现很有用,它可以说明问题,以便我可以将其展示给其他用户。有时,当我尝试创建一个简单的复制品时,该复制品实际上可以正常工作。这让我知道这个工作“重现”和我的问题案例有一些不同。对我来说,下一步是弥合它们之间的差距,修改它们中的任何一个,使它们更接近,直到行为差异消失。成功的步骤通常会揭示正在调查的行为的罪魁祸首。

在您的情况下,我可以执行以下简单步骤来证明 SQL Server 正在按预期运行:

我创建一个表:

CREATE TABLE [dbo].[StudentCourse](
    [StudentID] [int] NOT NULL,
    [CourseID] [int] NOT NULL,
 CONSTRAINT [PK_StudentCourse] PRIMARY KEY CLUSTERED 
(
    [StudentID] ASC,
    [CourseID] ASC
))

我添加测试数据:

INSERT INTO [dbo].[StudentCourse] values (1,1)
INSERT INTO [dbo].[StudentCourse] values (1,2)
INSERT INTO [dbo].[StudentCourse] values (1,3)
INSERT INTO [dbo].[StudentCourse] values (2,2)
INSERT INTO [dbo].[StudentCourse] values (2,3)
INSERT INTO [dbo].[StudentCourse] values (2,4)

我执行您所描述的更新:

UPDATE [dbo].[StudentCourse] SET CourseID = 5 where StudentId = 2 and CourseID = 4
UPDATE [dbo].[StudentCourse] SET CourseID = 5 where StudentId = 1 and CourseID = 1

我可以看到这些工作正常。

尝试了解您正在做的不同事情,您就会找到问题的原因。

The error you are seeing means that you are trying to create a record with the same value of primary key as another existing record. You are making a mistake here, but you are not giving enough information, to understand what your mistake is.

When I have a problem, I find it useful to create a small repro, that can illustrate the problem, so that I can show it to other users. Sometimes, when I try to create a simple repro, the repro actually works without a problem. This lets me know that there is something different in this working "repro" and my problem case. Next step for me would be to bridge the gap between them, modify either of them to make them closer until the difference in behaviour disappears. The step that made it, usually reveals the culprit of the behaviour being investigated.

In your case I can make following simple steps, to prove, that SQL Server is operating as expected:

I create a table:

CREATE TABLE [dbo].[StudentCourse](
    [StudentID] [int] NOT NULL,
    [CourseID] [int] NOT NULL,
 CONSTRAINT [PK_StudentCourse] PRIMARY KEY CLUSTERED 
(
    [StudentID] ASC,
    [CourseID] ASC
))

I add test data in:

INSERT INTO [dbo].[StudentCourse] values (1,1)
INSERT INTO [dbo].[StudentCourse] values (1,2)
INSERT INTO [dbo].[StudentCourse] values (1,3)
INSERT INTO [dbo].[StudentCourse] values (2,2)
INSERT INTO [dbo].[StudentCourse] values (2,3)
INSERT INTO [dbo].[StudentCourse] values (2,4)

I perform the updates you have described:

UPDATE [dbo].[StudentCourse] SET CourseID = 5 where StudentId = 2 and CourseID = 4
UPDATE [dbo].[StudentCourse] SET CourseID = 5 where StudentId = 1 and CourseID = 1

I can see that these work just as they should.

Try to understand what you are doing differently, and you'll find the cause of your problem.

余罪 2024-11-11 12:52:47

我发现了问题。当我为查询字符串构建条件时,其中一个条件不是添加 GroupID 标准。当该记录包含在查询字符串中时,查询字符串恰好错过了该 GroupID 条件。事情发生如下。

UPDATE StudentCourse SET CourseID = 5 WHERE CourseID = 1 AND StudentID IN(2,3)
UPDATE StudentCourse SET CourseID = 5 WHERE StudentID IN(1,2,3)

UPDATE StudentCourse SET CourseID = 6 WHERE CourseID = 2 AND StudentID IN(2,3)
UPDATE StudentCourse SET CourseID = 6 WHERE StudentID IN(2,3,4)

当然,我的查询违反了没有 CourseID 标准的主键规则。感谢您抽出时间,伙计们。

I found the problem. When I was building Conditions for query string, one condition wasn't adding GroupID criteria. The query string happens to miss that GroupID crieria when that record was included in query string. It was happening as follow.

UPDATE StudentCourse SET CourseID = 5 WHERE CourseID = 1 AND StudentID IN(2,3)
UPDATE StudentCourse SET CourseID = 5 WHERE StudentID IN(1,2,3)

UPDATE StudentCourse SET CourseID = 6 WHERE CourseID = 2 AND StudentID IN(2,3)
UPDATE StudentCourse SET CourseID = 6 WHERE StudentID IN(2,3,4)

Of course, my query was violating primary key rule without CourseID criteria. Thanks for your time, mates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文