When a column in a table is PRIMARY KEY as well as IDENTITY KEY and another column in the same table acts as Foreign key. Then on Inserting any value in the Foreign key column, that does not exists in the Reference table column, the editor throws error due to Foreign Key Constraint which is understandable. But inspite of that value not getting inserted in the table, the IDENTITY COLUMN GETS INCREMENTED resulting in an behavior where, on Inserting valid values the IDENTITY COLUMN skips one increment.
For Ex.
Lets take a REFERENCE TABLE tbl__Gender
And a tbl__Customer TABLE :
CREATE A FOREIGN KEY REFERENCE:
ALTER TABLE tbl__Customer ADD CONSTRAINT FK_tbl__Customer_Gender_ID FOREIGN KEY (Gender_ID) REFERENCES tbl__Gender(ID)
TRY TO INSERT ANY INVALID VALUE IN GENDER_ID:
INSERT INTO tbl__Customer VALUES ('ana', 2)
INSERT INTO tbl__Customer VALUES ('erik',200)
ERROR IS DISPLAYED: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tbl__Customer_Gender_ID".
INSERTING A VALID VALUE:
INSERT INTO tbl__Customer VALUES ('ana', 1)
AFTER EXECUTION THE OUTCOME IS(WITH ID = 4):
WHY IS IT SO? WHY DOES IT MISS OUT 2 AND 3 IN ID COLUMN?