Search This Blog & Web

Tuesday, May 8, 2012

Advantages and Disadvantages of Timestamp in SQL SERVER

I have gone through a different experience today. One of my colleague ask me to get latest users data from a table. When I looked into table structure, it does not have any date or time data type. When I suggest to return data on Id basis the answer is in negative but he has defined a timestamp column that I can use it by converting into date time conversion. When I tried to convert timestamp into date time, there is an error message that database engine is unable to convert timestamp into date time. Most of the times people think that timestamp is related database type of date and time and can easily return date.

In this blog we will look into timestamp datatype, its usage and what is the difference between timestamp and rowversion. From microsoft fourms I have got following details.


Timestamp is a data type that exposes automatically generated, unique binary numbers within a database. Timestamp is generally used as a mechanism for version-stamping table rows like replication. 

Timestamp as DateTime;
The timestamp data type is just an incrementing number and does not preserve a date or a time. To record a date or time, use a datetime data type.

------------------------ Sample Code ---------------------------------------------
Declare @tbl table (id int identity(1,1),date datetime,stamp timestamp)
Insert into @tbl values (getdate(),default)
select * from @tbl

Update @tbl set date = getdate() + 1
select * from @tbl


Look at the following timestamp behavior.

Timestamp as Database timestamp:
Each database has a counter that is incremented for each insert or update operation that is performed on a table that contains a timestamp column within the database. This counter is the database timestamp. This tracks a relative time within a database, not an actual time that can be associated with a clock. Database timestamp can be returned as

select @@DBTS;

Table can have one timestamp column;
A table can have only one timestamp column. Every time that a row with a timestamp column is modified or inserted, the incremented database timestamp value is inserted in the timestamp column. 

Timestamp as Primary key;
Should not consider timestamp column as a PK because of changing value every time. Any update made to the row changes the timestamp value and, therefore, changes the key value. If the column is in a primary key, the old key value is no longer valid, and foreign keys referencing the old value are no longer valid. 

Timestamp as Dynamic Cursor;
If the table is referenced in a dynamic cursor, all updates change the position of the rows in the cursor. 

Timestamp as Index key;
If the column is in an index key, all updates to the data row also generate updates of the index.

Timestamp can be good in following scenarios

Timpstamp as value changes identification;
You can use the timestamp column of a row to easily determine whether any value in the row has changed since the last time it was read. If any change is made to the row, the timestamp value is updated. If no change is made to the row, the timestamp value is the same as when it was previously read. To return the current timestamp value for a database 

Timestamp as rowversion;
rowversion is the synonym for the timestamp data type and is subject to the behavior of data type synonyms. In DDL statements, use rowversion instead of timestamp wherever possible. 

Timpstamp with default column name;
In a CREATE TABLE or ALTER TABLE statement, you do not have to specify a column name for the timestamp data type, for 


CREATE TABLE #Table (PriKey int PRIMARY KEY, timestamp);
select * from #Table

If you do not specify a column name, the Microsoft SQL Server 2005 Database Engine generates the timestamp column name. 

however, the rowversion synonym does not follow this behavior. When you use rowversion, you must specify a column name.

Timestamp can be duplicate;
Duplicate timestamp values can be generated by using the SELECT INTO statement in which a timestamp column is in the SELECT list. 

Microsoft would not recommend using timestamp in this manner.

Timestamp as Null and Not Null;
  • A nonnullable timestamp column is semantically equivalent to a binary(8) column. 
  • A nullable timestamp column is semantically equivalent to a varbinary(8) column.

From this blog you can get idea when a timestamp can be usefull and when we need to consider date and time instead of timestamp. SQL Server use timestamp column to find synchronization between databases in replication and snapshot techniques.
Post a Comment