I have a logging Db with a hard requirement for having a Primary Key be defined as such very specifically:
[Id] int IDENTITY(1,1) NOT NULL,
And no other requirements, no Foreign Keys, etc..
Records older than X will periodically be deleted.
At some point I will have to handle running out of key-space.
Is it possible to have an atomic operation that changes all the PKs to be contiguous starting at 0?
For example, given a table with these 2 records:
- Pk = 100 ..other columns.,
- Pk = 1234
The resulting table would have
- Pk = 0 ..same original data, new PK..,
- Pk = 1
Update 1: I am considering a solution such as this:
CREATE TRIGGER rollOutLogs ON Logs
AFTER INSERT
AS
BEGIN
if IDENT_CURRENT( 'Logs' ) >= 10
DBCC CHECKIDENT ('[Logs]', RESEED, -10);
DELETE FROM Logs WHERE Id > IDENT_CURRENT( 'Logs' ) and Id < IDENT_CURRENT( 'Logs' ) + 5;
END
GO