为什么我的身份列值从2开始



i使用sql Server创建表并插入数据,但是为什么打开ID而不是1,而是2?

create table test1
(
    id int IDENTITY(1,1),
    name varchar(255),
    age int,
    primary key (id),
    constraint check_age check (age > 0 and age <= 150)
);

以下是显示的结果。

select * from test1;

输出:

+----+--------------+-----+
| id |     name     | age |
+----+--------------+-----+
|  2 | shenmeshenqi |  20 |
|  3 | shenmeshenq  |  21 |
+----+--------------+-----+

谁可以帮助我?谢谢

尝试以下:

--store values from the table in temporary table
select * into #tempTable from test1
--remove all records from the table
delete test1
--reset identity to start from 1
dbcc checkident (test1, reseed, 0);
--insert data back to the table, now the records will be numbered starting with 1
insert into test1
select * from #tempTable

以下命令将使下一个插入具有标识值为1,然后在2之后等等,因此2和3将是重复的。您可能只想暂时将表截断,因为它看起来像测试表。

dbcc checkident (test1, reseed, 0);

另外,您可以打开Indidentity Insert,然后将数据插入值1作为ID,然后在需要时再次关闭身份。

SET IDENTITY_INSERT dbo.Test1 ON
SET IDENTITY_INSERT dbo.Test1 OFF

希望它有帮助。

相关内容

最新更新