I have a big PHP application, developed by an external company, which is being completed right now; I cannot ask for changes to it.
It uses a database Microsoft SQL Server 2016, which is accessed by some users via SSMS, including me, and by some scheduled tasks at specific times of day.
Sometimes I find unexpected content in some tables, and it is frustrating because I don't know when it got there.
I just discovered about temporal tables: knowing the exact date and time when a record changes would be extremely useful.
Is it possible to add temporal tables to 2-3 key tables in the application, without changing the PHP code? Are there risks of compatibility or performances?
Best Answer
You should be able to achieve that goal (assuming the application is written in a way that adding additional columns to the table doesn't cause a problem). Of course, test, test, test.
Using the example code provided in Greg Larsen's post Altering an Existing Table to Support Temporal Data, let's work through an example.
Here is our current table.
We now want to track versions.
There is some very good information in Creating a System-Versioned Temporal Table that addresses Altering Non-Temporal Table to be System-Versioned Temporal Table.
Now the
Product
table looks like thisYou also find that a new
ProductHistory
table has been created that looks just like theProduct
table. TheProductHistory
table is currently empty because we haven't updated or deleted anything from the Product tableNow, let's run an update to generate some history
The
product
table has the current dataAnd now the
ProductHistory
table has a versioned rowYou can even add a column to the
Product
table and theProductHistory
table automatically gets the new column added as wellProduct
ProductHistory
You would certainly need to test this in a development environment first. You may have unique situations that have an impact on your decision to use Temporal tables.
Here are a few important links with additional information regarding Temporal tables
Getting Started with System-Versioned Temporal Tables
Temporal Table Considerations and Limitations
Manage Retention of Historical Data in System-Versioned Temporal Tables
Stopping System-Versioning on a System-Versioned Temporal Table