Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"GENERATED ALWAYS AS ROW START/END" and "PERIOD FOR SYSTEM_TIME"

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)
);
like image 204
Thor Avatar asked Dec 14 '16 00:12

Thor


People also ask

What is generated always as row start?

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.

What is period for System_time?

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.


1 Answers

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 ?

  • Introduced in 2011by ANSI/ISO for SQL
  • IBM DB2 & Oracle 10g,11g and 12c were early adopters.
  • Designed to keep a full history of data changes and allow easy point in time analysis.
  • Every temporal table has two explicitly defined columns, each with a datetime2data 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:

  • Temporal Tables
  • Creating a System-Versioned Temporal Table
  • Temporal in SQL Server 2016 - Video.
like image 134
ahmed abdelqader Avatar answered Sep 28 '22 06:09

ahmed abdelqader