After the transaction is rolled back, the auto-increment ID still increases

Source: Internet
Author: User

After the rollback, the auto-increment ID still increases.

For example, if the current ID is 7, a data entry is inserted and then rolled back.
Then you insert another piece of data. At this time, the data is successfully inserted. At this time, your ID is not 8, but 9.
Because although you inserted rollback before, the ID is still auto-incrementing.


If you think that the auto-increment ID should not be transcoded, other transactions have to wait and check whether the auto-increment ID is used or rolled back, which leads to blocking. In the following example, table A uses an auto-incremental ID.
User 1
------------
Begin transaction
Insert into...
Insert into B...
Update C...
Insert into D...
Commit


User 2
-----------
Begin transaction
Insert into...
Insert into B...
Commit


Looking at the above example code, if the auto-increment ID is to be transcoded, assume that the transaction of user 2 is executed 1 millisecond after execution of user 1, then he has to wait until the end of the transaction of User 1 and check whether the First Auto-increment ID is used. This causes blocking.

Auto-increment IDs are designed to be non-transactional, not a bug. If you need a closely continuous auto-increment sequence, we recommend that you use other methods to generate it.

-- It doesn't matter if you don't need it. A column is generated when necessary.
If object_id (tb) is not null drop table tb
Go
Create table tb (ID int)
Insert tb select 1
Insert tb select 2
Insert tb select 5
Insert tb select 18
Insert tb select 13
Select id, [newid] = (select count (*) from tb where id <= t. id) from tb t order by [newid]
/* Id newid
----------------------
1 1
2 2
5 3
13 4
18 5

(5 Data columns)
*/

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.