Talking about the operation method of SQL Server identity column

Source: Internet
Author: User
Tags reset

The Identity identity column is often used in SQL Server, and this growing field is indeed convenient to operate. But sometimes it can bring some trouble.

Example one: When some data is deleted from the table, the number from the growth column is no longer a line sequence. This time we can use the following solutions to solve.

SET IDENTITY_INSERT [TABLE] [ON|OFF]

Allows explicit values to be inserted into the identity column of the table, and when set to on, the number inserted into the identity column may be manually specified at the time of the insert operation, and the identity_insert must be restored to off after the operation completes, otherwise the number must be specified the next time it is inserted. Otherwise, you cannot complete the insert operation.

Example two: When the records in the table are all deleted, but when the value of the identity column becomes larger, it will grow endlessly if it is not reset. This time we are going to use:

DBCC CHECKIDENT(TABLE, [RESEED|NORESEED], [1])

The seed value of the specified table will be forcibly reset to 1. However, you may not want to reset the seed to 1, in which case you can replace the third parameter with the seed value you want to use. Sometimes you may want to know the current seed, not to reset the seed, then you need to use the noreseed, without having to worry 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.