Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top