We have a table with three columns, StudentId
and SubjectId
and Active
(and a few other columns but not related to this question).
Active
column indicates whether the record is active or not(we set this Active
column to zero if someone deletes the records from UI)
Definition of the index on columns StudentId
and SubjectId
is as below:
CREATE UNIQUE NONCLUSTERED INDEX [UQ_StudentSubject_SubjectId_StudentId]
ON [dbo].[StudentSubject]
(
[StudentId] ASC,
[SubjectId] ASC
)WITH(
PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF,
IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON
) ON [PRIMARY]
GO
From our application, if we try to insert another record with the combination of this SubjectId
and studentId
the insert is failing and Java is throwing below error:
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert duplicate key row in object 'dbo.StudentSubject' with unique index 'UQ_StudentSubject_SubjectId_StudentId'. The duplicate key value is (113460, 182).
The record with combination 113460, 182 is having Active as zero so, we are trying to insert new record instead of setting Active flag to 1.
Is there any way we can ignore this index while inserting if we insert a new record with the existing combination of subjectId and studentId have Active
column as zero?
EDIT Sorry for the confusion, It is an index NOT a constraint.