How to operate the SQL Server identity column

Source: Internet
Author: User

In SQL Server, the identity column is often used. This self-increasing field operation is indeed more convenient. But it sometimes brings some trouble.

Example 1: when some data is deleted from a table, the number of the auto-increment column is no longer a series of connections. In this case, we can use the following solutions.

Set identity_insert [Table] [ON | Off]

Explicit values can be inserted into the table's ID column. When it is set to on, the number inserted to the ID column may be manually specified during the insert operation. After the operation is complete, restore identity_insert to off. Otherwise, the number must be specified during the next insert, otherwise the insert operation cannot be completed.

Example 2: When all the records in the table are deleted, but the value of the ID column is getting bigger and bigger, if it is not reset, it will continue to grow endlessly. At this time, we will use:

DBCC checkident (table, [reseed | noreseed], [1])

The seed value of the specified table is forcibly reset to 1. However, you may not want to reset the seed to 1. In this case, you can replace the third parameter with the seed value you want. Sometimes you may want to know the current seed, instead of resetting the seed. You need to use noreseed instead of worrying about the third parameter.

 

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.