use TESTDB2
--step1:创建表
create table customers(
id int identity primary key not null,
name varchar(15)
);
--step2:执行插入操作
insert into customers(id,name) values(1,'name1');
--报错:An explicit value for the identity column in table 'customers' can only be specified when a column list is used and IDENTITY_INSERT is ON.
--step3:放开主键列的自增长
SET IDENTITY_INSERT customers ON;
--step4:插入两条记录,主键分别为1和3。插入成功
insert into customers(id,name) values(1,'name1');
insert into customers(id,name) values(3,'name1');
--step5:再次插入一个主键为2的记录。插入成功
insert into customers(id,name) values(2,'name1');
--step6:插入重复主键,
--报错:Violation of PRIMARY KEY constraint 'PK__customer__3213E83F00551192'. Cannot insert duplicate key in object 'dbo.customers'.
insert into customers(id,name) values(3,'name1');
--step7:关闭IDENTITY_INSERT
SET IDENTITY_INSERT customers OFF;
05-10
900
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
10-18
3088
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
10-29
3090
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)