Looks like SQL Server 2008 and later uses the concept of "temporal tables" to manage table data history:
https://learn.microsoft.com/en-us/sql/relational-databases/tables/temporal-table-usage-scenarios
Looks like the following clause is used to accomplish this:
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.MyTableHistory));
Let's assume that a data model has tables TableX and a TableXHistory and I select the following context menu path to generate a DDL script of TableX:
Script Table as > CREATE to > New Query Editor Window
If the generated SQL script does not have a text reference to "HISTORY_TABLE" then can I say 100% that the history table is not managed as a temporal table? Also, would a temporal table be explicitly displayed in the standard tables directory for the data model? Is there any reason not to use temporal tables in 2018 as opposed to manually created history tables? My first impression is that anyone who creates manual history tables in 2018 is most likely out of date with SQL Server capabilities.
Temporal tables available only from 2016. Technology is not mature yet.
Temporal tables have their own Pros & Cons. Other options should be considered (classic triggers and history table, change data capture, replication, etc.)
The main disadvantages of temporal tables for me:
multiple changes made at the same time are invisible (only one row is returned)
history tables must be located at the same DB
limitation for transactional replication, merge replication is not supported
issues when system time has been changed - no way to know which update was first w/o implementing additional logic (version)
history tables can'be updated w/o disabling versioning
to get net changes you need to query the base table (which is not good).
how to detect which columns are changed? (CDC & triggers can detect that naturally, with temporal it may be very expensive)
...
In a SQL Server 2017 or Azure SQL Database database, is there a way to identify rows in the history table that were deleted from the current table, without specifically executing a query that finds all primary key values in the history table that do not exist in the current table? I suspect there isn't but wanted to make sure I'm not overlooking anything.
I have a lot of views and tables connected in Microsoft SQL Server. I want to check all the useless columns I have in the native tables. Is there a way to perform an automatic check if a column in a table is used or not in other tables?
Create a database diagram in SQL Server Management Studio. From here you can analyze how the tables/columns are related or not. Info here
Do a business model analysis and see which values are used, which are deprecated and start from there.
If you do any changes on the database, these changes have to be projected in any code connecting to that database.
Do not remove columns in tables just by looking at a database diagram. You would destroy any object-relational mapper.
I am looking for information about my table history,
I have to know the time that specific row was inserted
is there someway to know it?
thanks.
Using Triggers
A Datetime column in that table and an After Insert,Update Trigger
which updates that column to GETDATE().
This will only give you the details about the very last
change(update/Insert).
CDC
Change Data Capture (CDC) was introduced in SQL Server 2008. Change
Data Capture records INSERTs, UPDATEs, and DELETEs applied to SQL
Server tables, and makes a record available of what changed, where,
and when, in simple relational ‘change tables’.
These "Change Tables" contain columns that reflect the column
structure of the source table you have chosen to track, along with the
metadata needed to understand the changes that have been made.
Read here more about CDC
Also it is only supported in Datacenter & Enterprise edition.
Think that says it all?
None. SQL Server does not have an equivalent feature.
UPDATE: From SQL Server 2016 on, this information is outdated. See the comments and answers below.
I know this question is quite old, but with SQL Server 2016, Temporal Tables is a feature:
https://learn.microsoft.com/en-us/sql/relational-databases/tables/temporal-tables
Maybe it can help others in case they come to this topic (Searching for something similar to Oracle Flashback feature)
With temporal tables enabled, you can query table AS OF a specific timestamp and retrieve rows as they were in that specific timestamp, just like you were used to do in Oracle:
(SELECT * FROM EMPLOYEE AS OF TIMESTAMP ('13-SEP-04 8:50:58','DD-MON-YY HH24: MI: SS')
Equivalent query in SQL Server for a table with SYSTEM_VERSIONING=ON will be:
SELECT * FROM EMPLOYEE FOR SYSTEM_TIME AS OF '2004-09-01 08:50:58'
To enable SYSTEM_VERSIONING for an existing table with rows you may use the following script:
ALTER TABLE [dbo].[TABLE] ADD [SysStartTime] datetime2(0) GENERATED ALWAYS AS ROW START HIDDEN NOT NULL CONSTRAINT DF_Inventory_SysStartTime DEFAULT '1900-01-01 00:00:00', [SysEndTime] datetime2(0) GENERATED ALWAYS AS ROW END HIDDEN NOT NULL CONSTRAINT DF_Inventory_SysEndTime DEFAULT '9999-12-31 23:59:59', PERIOD FOR SYSTEM_TIME ([SysStartTime], [SysEndTime])
ALTER TABLE [dbo].[TABLE] SET (SYSTEM_VERSIONING = ON);
After enabling SYSTEM_VERSIONING, the History table will show under the table where you enabled versioning:
To Remove SYSTEM_VERSIONING from a table:
ALTER TABLE [dbo].[TABLE] SET (SYSTEM_VERSIONING = OFF);
ALTER TABLE [dbo].[TABLE] DROP PERIOD FOR SYSTEM_TIME;
ALTER TABLE [dbo].[TABLE] DROP COLUMN [SysStartTime], [SysEndTime];
For more info you can visit the following link (or official Microsoft documentation referenced before):
http://www.sqlservercentral.com/articles/SQL+Server+2016/147087/
Closest equivalent is probably Database Snapshots. You can create a database snapshot at the moment of interest and then report against the snapshot. Unlike flashbacks, the moments at which the SQL Server snapshots are taken has to be pre-determined.
On SQL server 2008 you can use Change Data Capture, by this feature you can do a lot more than oracle flash back. (There is a store procedure to revert database on SQL server 2008 if you want i can provide that for you)
Yes, we can use Change Data Capture and Change Tracking
features which are Built in mechanisms in SQL Server and very much
similar to Flashback in Oracle.
When you apply Change Data Capture features on a database table, a mirror of the tracked table is created with the same column structure of the original table, but with additional columns that include the metadata used to summarize the nature of the change in the database table row. The SQL Server DBA can then easily monitor the activity for the logged table using these new audit tables .
Change tracking is a lightweight solution that provides an efficient change tracking mechanism for applications. Typically, to enable applications to query for changes to data in a database and access information that is related to the changes, application developers had to implement custom change tracking mechanisms. Creating these mechanisms usually involved a lot of work and frequently involved using a combination of triggers, timestamp columns, new tables to store tracking information, and custom cleanup processes.
Different types of applications have different requirements for how much information they need about the changes. Applications can use change tracking to answer the following questions about the changes that have been made to a user table:
What rows have changed for a user table?
Only the fact that a row has changed is required, not how many times the row has changed or the values of any intermediate changes.
The latest data can be obtained directly from the table that is being tracked.
Has a row changed?
The fact that a row has changed and information about the change must be available and recorded at the time that the change was made in the same transaction.
For more information on how to use Change Data Capture (CDC) and Change Tracking in SQL Server; please check out Pinal Dave's Post.
Change Tracking
SQL Server 2016 introduced temporal tables aka history tables which enables developers to query data stored in a database table in the past.
I mean developers can provide applications which enables users to display a table's historical data, or the view of a table at a certain time in the past