SQL Server timestamp data usage

Source: Internet
Author: User
Tags time in milliseconds

In SQL Server, the following is the connection:
The SQL Server timestamp data type is independent of the time and date. SQL Server timestamp is a binary number that indicates the relative sequence of data modifications in the database. The timestamp data type was initially implemented to support the SQL Server restoration algorithm. Each time a page is modified, the current @ DBTS value is used to mark it once, and then @ DBTS is added with 1. This is enough to help the recovery process determine the relative order of page modifications, but the timestamp value has nothing to do with time.
I will explain it with saliva for ease of understanding:
We know that the numeric type has an "ID" attribute. After "id seed" and "id increment" are set, each time a record is added, the value of this field will be automatically added based on the latest id value, so that we can know which records are first added and which records are then added, however, we cannot know which records have been modified. For a timestamp field, each time a record is added, it is automatically added based on the latest timestamp. When a record is modified, it will also be automatically added based on the latest timestamp, so we will know which records have been modified.

Timestamp indicates the number of binary values automatically generated, which is unique in the database. Timestamp is generally used to add version stamps to table rows. The storage size is 8 bytes.
Note
The Transact-SQL timestamp data type is different from the timestamp data type defined in the SQL-92 standard. The SQL-92 timestamp data type is equivalent to the Transact-SQL datetime data type.
Microsoft®SQL Server™In future versions, the behavior of the Transact-SQL timestamp data type may be modified to make it consistent with the behavior defined in the standard. By that time, the current timestamp data type will be replaced by the rowversion data type.
Microsoft®SQL Server™2000 introduced the rowversion synonym of the timestamp data type. Try to use rowversion instead of timestamp in DDL statements. Rowversion is restricted by the behavior of Data Type synonyms. For more information, see Data Type synonyms.
In the create table or alter table statement, you do not need to provide a column name for the timestamp data type:
Create table ExampleTable (PriKey int primary key, timestamp) if the column name is not provided, SQL Server generates the timestamp column name. Rowversion data type synonyms do not have such behavior. The column name must be provided when rowversion is specified.
A table can have only one timestamp column. Values in the timestamp column are updated each time a row containing the timestamp column is inserted or updated. This attribute makes the timestamp column not suitable for use as a key, especially not as a primary key. Any update to the row changes the timestamp value to change the key value. If the column belongs to the primary key, the old key value is invalid, and the foreign key that references the old value is no longer valid. If the table is referenced in a dynamic cursor, all updates change the row position of the cursor. If this column belongs to the index key, all updates to the data row will also lead to index updates.
The timestamp column that cannot be null is semantically equivalent to the binary (8) column. The empty timestamp column can be semantically equivalent to the varbinary (8) column.

Microsoft SQL Server uses two 4-byte Integers to store values of the datetime data type. The number of days before or after the first 4-byte storage base date (January 1, 1900. The base date is the system reference date. Datetime values earlier than January 1, January 1, 1753 are not allowed. The other 4-byte storage represents the daily time in milliseconds after midnight.

Copy codeThe Code is as follows:
@ DBTS (SELECT @ DBTS)
Returns the value of the current timestamp data type for the current database. This timestamp value must be unique in the database.
Syntax
@ DBTS
Return type
Varbinary
Note
@ DBTS

Returns the timestamp value used at the end of the current database. When a row with a timestamp column is inserted or updated, a new timestamp value is generated.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.