I have a child table associated to the parent by the parent's primary key. Currently, the child table has a standard ID of int IDENTITY(1,1)
. This is fine for my purposes and that will not change but to the user, they'll be seeing lists of disjointed IDs when they view the child rows associated to the parent.
I would like to have a display ID that starts at 1 and increments (like an identity column) for each group of records.
My child table would look something like this;
ChildID-PK ParentID-FK DisplayID
1 1 1
2 1 2
3 2 1
4 3 1
5 1 3
Possible solutions;
- When inserting a record,
SELECT MAX(DisplayID) + 1 FROM tbl WHERE ParentID = @ParentID
to use as the new records DisplayID. I could see this being problematic if many people are editing a group of records at once. - Use
ROW_NUMBER()
to get the DisplayID but, in this case, if a record were to be deleted the DisplayID for existing records could change. This can't happen because users may be referring to specific DisplayIDs when entering data.