There are many new features in SQL Server 2016, but the one we will focus on in this post is:

SQL Server 2016中有许多新功能,但本文中我们将重点介绍的功能是:

  • Temporal Database Tables时态数据库表

One of the many new features in SQL server 2016 is the built-in support for temporal data tables. This is a feature that many developers have been urging to have as a “out of the box feature” – and therefore many have developed their own ways of implementing the capability to store information about how data looked in a certain period of time.

SQL Server 2016中的许多新功能之一是对临时数据表的内置支持。 许多开发人员一直呼吁将此功能作为“开箱即用”功能-因此,许多开发人员已经开发出自己的实现存储有关特定时期数据外观信息的功能的方法。

什么是时间数据表? (What is a Temporal Data Table?)

The concept of a temporal data table is that the table will hold information about a records value in any point in time – thus meaning that we easily can read from the table and get values that are not current, but merely a historical view of the record.

时态数据表的概念是,该表将在任何时间点保存有关记录值的信息-这意味着我们可以轻松地从表中读取并获得非当前值,而仅仅是记录的历史视图。

The table that is to hold temporal data, needs to contain system columns of datatype Datetime2, one holding the startdate and one holding the enddate, this will allow any application or a user to query the data and get values for a specific point in time.

用于保存时态数据的表需要包含数据类型为Datetime2的系统列,其中一列包含开始日期,一列包含结束日期,这将允许任何应用程序或用户查询数据并获取特定时间点的值。

Microsoft has decided to call temporal data tables “System-Versioned” tables when implementing them in SQL Server 2016.

Microsoft已决定在SQL Server 2016中实现时态数据表时将其称为“系统版本”表。

谁需要时间数据表? (Who needs Temporal Data Tables?)

There is no such thing as data that are not dynamic, if data is not dynamic, the system/application is dead and we have other issues. All other applications or business users consuming data or creating data will know that they eventually will need to look at the historical data, some use cases for the temporal data tables would include:

没有数据不是动态的,如果数据不是动态的,则系统/应用程序已死,我们还有其他问题。 所有其他使用数据或创建数据的应用程序或业务用户都将知道他们最终将需要查看历史数据,时态数据表的一些用例包括:

  • Understanding how the business changes over time了解业务随着时间的变化
  • Tracking data changes over time跟踪数据随时间的变化
  • Auditing all changes to data审核所有数据更改
  • Maintaining a slowly changing dimension for decision support applications维持决策支持应用程序缓慢变化的维度
  • Recovering from accidental data changes and application errors从意外的数据更改和应用程序错误中恢复
  • Only allowing part of the organization to look at updated records, whilst another look at the old version.仅允许组织的一部分查看更新的记录,而另一部分查看旧的记录。

商业情报 (Business Intelligence)

From a Business Intelligence developer viewpoint, this could solve some issues on collection historical data in i.e. Dimension Tables where many business analysts have a valid need to know how data looked at a point-in-time. This is especially an issue in regards to i.e. Organizational Dimensions and their hierarchy, which is bound to change over time. With the implementation of temporal data tables we get the feature out-of-the box, all we need to do is to configure the table.

从商业智能开发人员的角度来看,这可以解决有关收集历史数据(即维度表)中的一些问题,在这些表中,许多业务分析人员确实需要了解数据在某个时间点的外观。 就组织维度及其层次结构而言,这尤其是一个问题,随着时间的推移,这些问题必然会发生变化。 通过实现时态数据表,我们可以立即使用该功能,只需配置表即可。

人力资源 (Human Resource)

Human resource data is by default Temporal, as an employee tends not to stay in the exact same state from day one, until either he/she is terminated or leaving for another job position. This is another clear use case for this feature.

默认情况下,人力资源数据是临时的,因为从第一天起,员工往往不会保持完全相同的状态,直到他/她被解雇或离开另一个工作岗位为止。 这是此功能的另一个明确用例。

它是如何工作的? (How does it Work?)

The way Microsoft have chosen to implement this design in SQL server 2016, is to have a new table nested under the table that we enable for system-versioning. So actually when you look at the table in SSMS, there will be a little icon on the left indicating that there is more to this table, as you can see in the figure 1.

Microsoft选择在SQL Server 2016中实现此设计的方式是在我们支持系统版本化的表下嵌套一个新表。 因此,实际上,当您查看SSMS中的表格时,在左侧会出现一个小图标,指示该表格还有更多内容,如图1所示。

Figure 1: Table indicating that system-versioning is enabled

图1:表示启用系统版本控制的表

Once we expand the table information, we discover that there is a new table under the hood, this table is named history, but can be named whatever you prefer. The history table will hold information on attributes from the main table, as well as a start and end date for the period, where the value in the attribute is valid. This is shown in figure 2.

扩展表信息后,我们发现在后台有一个新表,该表名为history,但可以随便命名。 历史记录表将保存有关主表中属性的信息,以及该属性值有效的时间段的开始和结束日期。 如图2所示。

Figure 2:The table information is now expanded

图2:表信息现在已展开

This means that whenever there is a change to any attributes in the main table, a copy of the old values paired with a start and an end date will be inserted into the history table. Thus giving you or the application the ability to look at data from the past.

这意味着只要主表中的任何属性发生变化,就会将旧值的副本与开始和结束日期配对,并将其插入历史记录表。 从而使您或应用程序能够查看过去的数据。

插 (Insert)

When inserting new data in a table that is system-versioned, rows will be given the default enddate “9999-12-31 23:59:59” upon insertion, this will allow the server to maintain the data and always have the current dataset in the table at all times. Basically means that the record is open and valid.

在系统版本化的表中插入新数据时,在插入行时,行的默认终止日期为“ 9999-12-31 23:59:59”,这将允许服务器维护数据并始终拥有当前数据集始终在表中。 基本上意味着记录是开放且有效的。

更新 (Updates)

When updating a record on a system-versioned table, the startdate will be updated to the UTC time from the transaction running the update statement. The enddate will be “9999-12-31 23:59:59” and the old attribute values will be inserted into the history table, with a enddate set to UTC time from the transaction running the update statement and the startdate will remain.

当更新系统版本表上的记录时,开始日期将从运行update语句的事务更新为UTC时间。 结束日期将为“ 9999-12-31 23:59:59”,并将旧属性值插入历史记录表,将结束日期设置为运行更新语句的事务的UTC时间,并且开始日期将保留。

删除项 (Deletes)

When deleting a row in a system-versioned table, the current values will be stored in the history table, and the end date will be set to UTC time from the transaction running the delete statement. In the main table the row will be removed, and thus not show up in the datasets from that table. If we would like to see the data, we need to query the history table.

删除系统版本表中的行时,当前值将存储在历史记录表中,结束日期将设置为运行delete语句的事务的UTC时间。 在主表中,该行将被删除,因此不会显示在该表的数据集中。 如果要查看数据,则需要查询历史记录表。

如何在表上启用临时(系统版本化) (How to enable Temporal (system-versioned) on a table)

How to get a table to be system-versioned is something that we have to script at the moment, and it’s a fairly easy task to script. The table itself need to have a start and end date defined before we can enable the feature on the table.

目前,我们必须编写如何对表进行系统版本控制的脚本,这是编写脚本的一项相当容易的任务。 必须先定义表格的开始日期和结束日期,然后才能在表格上启用该功能。

We will create the Product table that will have system-version enabled from the start:

我们将创建从一开始就启用系统版本的Product表:


-- To create a new Temporal Data Table (System-Versioned)
-- we need to issue the following statement.
-- We asumme that the table is not created and does not exists.CREATE TABLE dbo.Product
(Product_id INT NOT NULL IDENTITY(1,1) CONSTRAINT PK_Product PRIMARY KEY NONCLUSTERED,Category_id INT NULL,Product_Name VARCHAR(255) NOT NULL,startdate DATETIME2(0) GENERATED ALWAYS AS ROW START NOT NULL,enddate DATETIME2(0) GENERATED ALWAYS AS ROW END NOT NULL,PERIOD FOR SYSTEM_TIME (startdate, enddate)
)
WITH ( SYSTEM_VERSIONING = ON ( HISTORY_TABLE = dbo.ProductHistory ) );

Besides the ordinary table creation statement, we have added a few new lines to the create statement, the lines are these, and they will be explained.

除了普通的表创建语句之外,我们还在create语句中添加了几行,这些行是这些行,我们将对其进行说明。

startdate DATETIME2(0) GENERATED ALWAYS AS ROW START NOT NULL

This line declares that startdate attribute, should be of type datetime2 and always generated when data is either inserted, updated or deleted. Also we indicate that it should be of the type ROW START.

此行声明startdate属性应为datetime2类型,并且始终在插入,更新或删除数据时生成。 我们还指出它应该是ROW START类型。

enddate DATETIME2(0) GENERATED ALWAYS AS ROW END NOT NULL

This line declares that enddate attribute, should be of type datetime2 and always generated when data is either inserted, updated or deleted. Also we indicate that it should be of the type ROW END.

该行声明该enddate属性应为datetime2类型,并且始终在插入,更新或删除数据时生成。 我们还指出它应该是ROW END类型。

PERIOD FOR SYSTEM_TIME (startdate, enddate)

Now this last statement is the PERIOD that tells us which attributes are to be handled as a start and an end of the period.

现在,最后一条语句是PERIOD,它告诉我们哪些属性将作为期间的开始和结束。

The table have the startdate (startdate) and enddate (enddate) defined, and upon creation the historytable called [dbo].[Producthistory] is created.

该表定义了开始日期(startdate)和结束日期(enddate),并在创建时创建了名为[dbo]。[Producthistory]的历史记录表。

Now the table is ready for the data let us insert some test data into it:

现在表已准备好用于数据,让我们在其中插入一些测试数据:


--Insert data into dbo.ProductINSERT INTO dbo.Product (Category_id, Product_Name) VALUES(1, 'Toys'),(2, 'Bikes'),(3, 'Clothes'),(4, 'Cars')

Now if we select the data from product, we will have the following dataset at hand:

现在,如果我们从产品中选择数据,那么手边将有以下数据集:

Product_id Category_id Product_Name startdate enddate
1 1 Toys 2015-06-27 14:21:24 9999-12-31 23:59:59
2 2 Bikes 2015-06-27 14:21:24 9999-12-31 23:59:59
3 3 Clothes 2015-06-27 14:21:24 9999-12-31 23:59:59
4 4 Cars 2015-06-27 14:21:24 9999-12-31 23:59:59
产品编号 Category_id 产品名称 开始日期 结束日期
1个 1个 玩具 2015-06-27 14:21:24 9999-12-31 23:59:59
2 2 单车 2015-06-27 14:21:24 9999-12-31 23:59:59
3 3 衣服 2015-06-27 14:21:24 9999-12-31 23:59:59
4 4 汽车 2015-06-27 14:21:24 9999-12-31 23:59:59

As you can see all records is the current ones, indicated by the enddate set to “9999-12-31 23:59:59”

如您所见,所有记录都是当前记录,由设置为“ 9999-12-31 23:59:59”的结束日期表示

If we query the table holding the history, it will give us an empty dataset, because none of the original records has been updated or deleted.

如果我们查询保存历史记录的表,它将给我们一个空的数据集,因为没有原始记录被更新或删除。

Now, let us try to update a record, and see what happens to our data:

现在,让我们尝试更新记录,并查看数据发生了什么:


--Update a row in dbo.productUPDATE dbo.Product SET Product_Name = 'Christmas Accesories' WHERE Product_id = 4

When we again select from our table dbo.Product, we can see that the record in fact have changed. But if we look closely at the record we updated, you can see that the startdate has changed from the initial “2015-06-27 14:21:24” to “2015-06-27 14:31:32” – this means that the value for this exact record is valid in a period of 10 minutes:

当我们再次从表dbo.Product中选择时,我们可以看到实际上记录已更改。 但是,如果我们仔细查看更新的记录,您会发现开始日期已从最初的“ 2015-06-27 14:21:24”更改为“ 2015-06-27 14:31:32” –这意味着该确切记录的值在10分钟内有效:

Product_id Category_id Product_Name startdate enddate
1 1 Toys 2015-06-27 14:21:24 9999-12-31 23:59:59
2 2 Bikes 2015-06-27 14:21:24 9999-12-31 23:59:59
3

3

Clothes 2015-06-27 14:21:24 9999-12-31 23:59:59
4 4 Christmas Accesories 2015-06-27 14:31:32 9999-12-31 23:59:59
产品编号 Category_id 产品名称 开始日期 结束日期
1个 1个 玩具 2015-06-27 14:21:24 9999-12-31 23:59:59
2 2 单车 2015-06-27 14:21:24 9999-12-31 23:59:59
3

3

衣服 2015-06-27 14:21:24 9999-12-31 23:59:59
4 4 圣诞饰品 2015-06-27 14:31:32 9999-12-31 23:59:59

If we look at the dbo.ProductHistory table, we can now see that a new record has been inserted, and here we will see that a value for a record with an id = 4 will be “Cars” in a period of 10 minutes:

如果我们查看dbo.ProductHistory表,现在可以看到已插入一条新记录,在这里我们将看到ID为4的记录的值在10分钟内为“汽车”:

Product_id Category_id Product_Name startdate enddate
4 4 Cars 2015-06-27 14:21:24 2015-06-27 14:31:32
产品编号 Category_id 产品名称 开始日期 结束日期
4 4 汽车 2015-06-27 14:21:24 2015-06-27 14:31:32

This means that we can query our history tables if we need to know an exact value for a specific point in time.

这意味着如果我们需要知道特定时间点的确切值,我们可以查询历史记录表。

翻译自: https://www.sqlshack.com/new-features-in-sql-server-2016-temporal-data-tables/

SQL Server 2016的新功能–临时数据表相关推荐

  1. SQL Server 2016的新功能–动态数据屏蔽

    There are many new features in SQL Server 2016, but the one we will focus on in this post is: SQL Se ...

  2. 今天在webcasts上看sql server 2008的新功能介绍,无意中发现在连接sql server 数据库时,还有一个...

    今天在webcasts上看sql server 2008的新功能介绍,无意中发现在连接sql server 数据库时,还有一个 Failover Partner的选项.以前都没注意过,它的主要功能就是 ...

  3. SQL Server 2012 开发新特性:文件表

    转载于 春哥 一.启用FileTable 1.修改数据库引擎的属性 打开"SQL Server配置管理器",修改SQL Server数据库引擎的属性.使用此页可针对此 Micros ...

  4. 微软私有云测试01-Windows Server 2016虚拟化新功能概述

    Hello,大家好,许久未见,你好,51Cto博客:你好,Superdream,好久没有写博客了,许久未动笔,却不知如何去撰写开头,距离上一篇博客已经过去了一年半的时间,有时候我就在想,自己这一年半工 ...

  5. SQL Server 2012 开发新功能 序列对象(Sequence)

    众所周知,在之前的sqlserver版本中,一般采用GUID或者identity来作为标示符,但是identity是一个表对象,只能保证在一张表里面的序列,当我们遇到以下情况时, 表1 ID 订单类型 ...

  6. SQL Server 2016 SP1中的新功能和增强功能

    SQL Server 2016 SP1 is released as announced by Microsoft. It comes with a bunch of new features and ...

  7. SQL Server - SQL Server 2016新特性之 --- Query Store

    前言 SQL Server 2016引入新的查询语句性能监控.调试和优化工具/功能 -- Query Store.以前我们发现一条查询语句性能突然下降,我们要去找出问题的所在往往需要通过调用一些DMV ...

  8. 微软发布正式版SQL Server 2016

    微软于6.2 在SQL 官方博客上宣布 SQL Server 数据库软件的正式发布版本(GA),历时一年多,微软为该软件发布了多个公共预览版和候选版本,而今天最终版本终于上线了.在博客中,微软数据集团 ...

  9. MySQL8.0与SQL server 2016的技术区别(一)

    MySQL8.0与SQL server 2016都是关系型数据库,两者在原理与技术方面有极高的相似度.本文主要简述笔者在学习过程中发现的两者有区别的部分,如果读者知道还有其他区别的点,欢迎进行评论补充 ...

最新文章

  1. 太强了!阿里技术团队重磅开放 《Java 开发手册》(附下载地址)!
  2. 机器学习+优化问题的种类、如何优化、凸优化、非凸优化、对偶问题、KKT条件
  3. 2012.5.2 学习记录:RadGrid单元格操作
  4. linux系统编程之进程(七):system()函数使用
  5. 【ElasticSearch】Es 源码之 ClusterService 源码解读
  6. Spring MVC学习笔记(七)
  7. dhcp协议_记录一次DHCP协议的学习过程
  8. 来自太阳,爱情的故事,生存和欢乐的影子上升 - 免费节选
  9. linux下安装字体
  10. Linux操作系统引导过程及MBR扇区故障解决
  11. 小程序常用的十个入口
  12. 《位置大数据隐私管理》—— 第1章 位置信息与隐私保护 1.1 位置大数据
  13. 降噪蓝牙耳机推荐什么牌子好?入耳式降噪蓝牙耳机推荐
  14. 东京奥运会将采用人脸识别系统 加强安检
  15. js逻辑训练题_几道javascript练习题
  16. python可以爬取58同城代码_爬取58同城—字体反爬
  17. Java面试——美团
  18. 硬盘低级格式化需谨记
  19. rail铁轨 洛谷 测试数据_你见过什么生成 OI 比赛测试数据的工具?
  20. 弃我去者、昨日之日不可留!乱我心者、今日之日多烦忧!

热门文章

  1. bzoj3456:城市规划
  2. 数据库管理软件的由来
  3. 15_Python模块化编程_Python编程之路
  4. dxComponentPrinter记录
  5. yii2框架随笔19
  6. Android ScrollView嵌套ScrollView滚动的问题解决办法
  7. 数据库---查询操作
  8. codeforce438D The Child and Sequence
  9. 股市三大定律是什么?
  10. 初三学生什么时候上一对一效果最好?