Manage retention of historical data in system-versioned temporal tables yazılarına bakmak lazım
Ne İşe Yarar
Elimizde şöyle bir sql olsun
UPDATE Employee SET Salary = 6000 WHERE Id = 1;
SQL server şunları yapar
SQL Server automatically:1. Copies the old row into EmployeeHistory.2. Updates the current row in Employee.3. Maintains the validity timestamps.
İki Tane Sütun Lazım
SQL Server uses two special columns:- ValidFrom: When this version became valid.- ValidTo: When this version stopped being valid.These timestamps are managed automatically by SQL Server.
Mevcut Tabloya Eklemek
Şöyle yaparız
ALTER TABLE dbo.EmployeeADDValidFrom DATETIME2 GENERATED ALWAYS AS ROW STARTHIDDEN NOT NULLDEFAULT SYSUTCDATETIME(),ValidTo DATETIME2 GENERATED ALWAYS AS ROW ENDHIDDEN NOT NULLDEFAULT CONVERT(DATETIME2, '9999-12-31 23:59:59.9999999'),PERIOD FOR SYSTEM_TIME (ValidFrom, ValidTo);
Daha sonra system versioning etkinleştirilir
ALTER TABLE dbo.EmployeeSET(SYSTEM_VERSIONING = ON(HISTORY_TABLE = dbo.EmployeeHistory));
Ne olur
What happens to existing records?- Existing records remain in Employee.- They receive ValidFrom and ValidTo values.- SQL Server does not create historical versions of changes that happened before conversion.- From the moment versioning is enabled, updates and deletes automatically generate history.