There was an interesting question recently about checking whether a table had been created with DATA_CONSISTENCY_CHECK
, if the SYSTEM_VERSION
option was set, and how to find the name of the historical tables.
First up, DATA_CONSISTENCY_CHECK
.
I have finished studying A LOT on temporal tables for my 70-761 and I knew most of the answers off the top of my head. The question regarding DATA_CONSISTENCY_CHECK
and seeing if that value persisted anywhere was a head scratcher though. It seemed no one had asked a question in that way before and I did not find any links or articles that talked about that aspect.
I had to create a temporal table to see the results for myself. In short, DATA_CONSISTENCY_CHECK
is a one time action flag when you create or alter a table. It is telling SQL Server to run DBCC CHECKCONSTRAINT
. This is validating that the data between the table you want to turn into a temporal table and the historical table receiving records from the temporal table match up. It will not allow a SYSTEM_VERSION
of a table if the SysEndTime > SysStartTime
.
If you try and look for the value DATA_CONSISTENCY_CHECK
is set to, you won’t find it. It’s not persisted after run time, it is just a flag to say “perform a check please”. It is also ran by default without you specifying, so I would say you can leave it out of your typical syntax unless you wanted to declare no check on that constraint.
If you have temporal tables, you can see that it does not exist for yourself:
select * from sys.tables order by name; select * from sys.key_constraints; select * from sys.check_constraints; select * from sys.sysconstraints; select definition,name from sys.check_constraints; select definition,name from sys.default_constraints;
If you would like to run the check yourself, you can with this base command. You will want to alter the after portion of DBCC CHECKCONSTRAINTS
.
DBCC CHECKCONSTRAINTS WITH ALL_CONSTRAINTS
Here is how Microsoft officially describes it:
When creating a link to an existing history table, you can choose to perform a data consistency check. This data consistency check ensures that existing records do not overlap. Performing the data consistency check is the default. Use this argument in conjunction with the PERIOD FOR SYSTEM_TIME and GENERATED ALWAYS AS ROW { START | END } arguments to enable system versioning on a table.
Generally, performing the data consistency is recommended whenever the data between the current and history tables may be out of sync, such as when incorporating an existing history table that is populated with history data.
Now, is SYSTEM_VERSION
currently set? This effectively means you are in temporal table mode. Here’s a quick way to check this:
SELECT name, object_id,temporal_type,temporal_type_desc FROM sys.tables where temporal_type in (1,2) order by name
Our last question was “How can I find the name of my history table?” We’re going to use our good friend the sys.tables
object once more for this.
SELECT NAME ,object_id ,temporal_type ,temporal_type_desc FROM sys.tables WHERE temporal_type_desc = 'History_Table' ORDER BY NAME