SQL Server Identity Seed

Source: Internet
Author: User

Background:

Modifying a column with an identity can make it grow automatically

Examples are:

CREATE TABLE T (ID int NOT null identity (),
Data nvarchar (32));

Inserting data

DECLARE @i as int = 1;
while (@i<10)
Begin
INSERT into T values (replicate (CAST (@i as NCHAR (1)), 10))
Set @i = @i +1;
End

Use DBCC CHECKIDENT (' table_name '); View the seed value of the table.

Delete data:

Delete from T
where ID >1;
Go

DBCC CHECKIDENT (' table_name ');--this time the seed value is still 9

Insert the data again:

Insert into T (Data) VALUES (' AAAA ');

View data:

Solutions,

Reset seed Value:

DBCC CHECKIDENT (' T ', reseed,2);--to this point the problem has been solved

Insert data:

INSERT into T values (' BBB ');

Output:

SELECT * FOM T;

Focus:

This seed value is 2 and the table is a value of 10 what happens if you keep going long? Let's insert a few more data to see

Insert data:

DECLARE @i as int = 1;
while (@i<10)
Begin
INSERT into T values (replicate (' NNN ', 10))
Set @i = @i +1;
End

Output:

Focus 2,

If the ID is the primary key what to do.

The first step:

Delete data

Delete from T
where Data like '%N% '
Go

Step Two:

Add primary Key

ALTER TABLE T
Add constraint pk_id primary key (ID);
Go

SQL Server Identity Seed

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.