I'm wondering if SQL Server (i.e. the T-SQL language) has a natural way of doing this or if I have to write fancy constraints/triggers.
Suppose I have a table
RebuplicanCandidates
===================================
Id | Name | ByteIndex
===================================
1 | 'Marco Rubio' | 0
2 | 'Jeb Bush' | 1
3 | 'Donald Trump' | 2
4 | 'Ted Cruz' | 3
and I remove JebBush:
DELETE FROM [RepublicanCandidates] WHERE [Id]=2
Then I want the table to be like
RebuplicanCandidates
===================================
Id | Name | ByteIndex
===================================
1 | 'Marco Rubio' | 0
3 | 'Donald Trump' | 1
4 | 'Ted Cruz' | 2
Notice that the ByteIndex
column shuffled.
And then if I insert a candidate
INSERT INTO [RepublicanCandidates] (Name) VALUES ('CarlyFiorina')
the table becomes
RebuplicanCandidates
===================================
Id | Name | ByteIndex
===================================
1 | 'Marco Rubio' | 0
3 | 'Donald Trump' | 1
4 | 'Ted Cruz' | 2
5 | 'Carly Fiorina' | 3