Use of SQL timestamp

Source: Internet
Author: User
Tags microsoft sql server time and date

Use of SQL timestamp

The concept of time stamp has been vague, I believe a lot of friends will be mistaken: Timestamp is a Time field, each time you add data, fill in the current time value. In fact, this misled a lot of friends.

1. Basic Concepts

Timestamp: A unique binary number automatically generated in the database, independent of time and date, usually used as a mechanism for adding a version stamp to a table row. The storage size is 8 bytes.

Each database has a counter that is incremented when an INSERT or update operation is performed on a table that contains timestamp columns in the database. The counter is a database timestamp. This can track the relative time within the database, not the actual time associated with the clock. A table can have only one timestamp column. The incremental database timestamp value is inserted in the timestamp column each time the row that contains the timestamp column is modified or inserted. This property makes the timestamp column unsuitable for use as a key, especially if it is not used as a primary key. Any update to the row changes the timestamp value, thereby changing the key value. If the column belongs to a primary key, the old key value will be invalid, and the foreign key referencing the old value will no longer be valid. If the table is referenced in a dynamic cursor, all updates change the position of the row in the cursor. If the column belongs to an index key, all updates to the data row will also cause the index to update.

Using the timestamp column in a row makes it easy to determine whether any value in the row has changed since the last time it was read. If a change is made to the row, the timestamp value is updated. If no changes are made to the row, the timestamp value is consistent with the timestamp value of the row that was previously read. to return the current timestamp value for the database, use @ @DBTS.

2. The role of timestamps

Play a role when controlling concurrency:
User A/b open a record at the same time to start editing, save is able to determine the timestamp, because the record is updated each time the system will automatically maintain the timestamp, so if the save time to find the timestamp is not equal to the timestamp in the database, indicating that the record was updated in this process, This will prevent others from being overwritten by the update.

3. Application of Timestamps

To put it simply, timestamp mainly records the last modification timestamp of the line, noting that the timestamp is not converted to time, only that the line has been modified.

What's the use of it? It is usually used for data incremental updates, for example, I copy data from this table to another table, but if I want to copy only the updated, then the maximum timestamp value is recorded from the last update, and then the current update, as long as the where condition finds the one that is greater than the last update. Timestamp the value of all rows. It then extracts the updated data and replicates it to another server, which is the incremental update.

4. Examples of applications in SQL

( Microsoft's recommendation-timestamp syntax has been superseded, in DDL statements, try to use rowversion instead of timestamp.) Future versions of Microsoft SQL Server will remove this feature .

See http://msdn.microsoft.com/zh-cn/library/ms182776.aspx)

rowversion is the timestamp missing update concept: When the user modifies a row of data at the same time, they first read the data and put it on the front end to modify it. When modified, the data is then submitted so
that the last submitted data overwrites the previously submitted data,
This results in missing updates. Long story short, describes ways to prevent missing updates: Using the rowversion timestamp. Each time the update, MSSQL will automatically update the value of the rowversion, if the line before the read and before the update value inconsistent, it means that there are other transactions updated this column, so that the column can not be updated,
This prevents the loss of updates. Example: Create a table first:
varchar (ten), b rowsversion)'ABC     
Transaction A: (new query executes the following code)
Declare @rv  Rowversionselect Span style= "COLOR: #008000" > @rv =b from tmp where a="  Abc "waitfor delay " 00:00:05update tmp set a= " xyz "where b= @rv 
Go
  < Span style= "COLOR: #808080" > transaction B: < Span style= "COLOR: #000000" >< Span style= "COLOR: #ff0000" >
@rv rowversion@rvwhere a='ABC'set a='aaawhere b=@ RVGo              
< Span style= "COLOR: #0000ff" > < Span style= "COLOR: #0000ff" > < Span style= "COLOR: #0000ff" > transaction A will find no ' aaa< when finished executing Span style= "COLOR: #ff0000" > "

Use of SQL timestamp

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.