I'm currently learning the new "System versioned temporal table" introduced in sql server 2016. However, I'm having a bit of a hard time getting my head around the exact meaning and use of "GENERATED ALWAYS AS ROW START/END" and "PERIOD FOR SYSTEM_TIME", both of which are required by system versioned temporal table.
Could someone please explain it to me? Thanks in advance for any help!
CREATE TABLE dbo.Employees
(
empid INT NOT NULL CONSTRAINT PK_Employees PRIMARY KEY NONCLUSTERED,
empname VARCHAR(25) NOT NULL,
department VARCHAR(50) NOT NULL,
salary NUMERIC(10, 2) NOT NULL,
sysstart DATETIME2(0) GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
sysend DATETIME2(0) GENERATED ALWAYS AS ROW END HIDDEN NOT NULL,
PERIOD FOR SYSTEM_TIME(sysstart, sysend),
INDEX ix_Employees CLUSTERED(empid, sysstart, sysend)
)
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.EmployeesHistory)
);
The GENERATED ALWAYS AS ROW START column represents the time when the row data became current, basically on an INSERT/UPDATE of the record in the system-versioned temporal table, the system will set current UTC time based on the system clock where the SQL Server instance runs.
The SYSTEM_TIME period columns for a system-period temporal table indicate when the version of a row is current. The SYSTEM_TIME period contains a pair of TIMESTAMP(12) columns whose values are generated by the database manager.
Temporal Tables in SQL server 2016
This is an amazing new feature in SQL Server 2016
and later.
In Quick points, What is temporal tables ?
datetime2
data type.From MSDN
A system-versioned temporal table must have a primary key defined and have exactly one PERIOD FOR SYSTEM_TIME defined with two datetime2 columns, declared as GENERATED ALWAYS AS ROW START / END
So The primary key for A system-versioned
table (consider this as a nickname of temporal table) defined and have exactly one PERIOD FOR SYSTEM_TIME
defined with two datetime2 columns for allowing easy point in time analysis or Time Travel (As Mr.Borko Novakovic said at Channel 9), declared as GENERATED ALWAYS AS ROW START / END
References:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With