22,210
社区成员
发帖
与我相关
我的任务
分享
/*
查看你的主鍵在哪列
一個表不可能出現兩個主鍵
SQL查询主键信息
*/
EXEC sp_pkeys @table_name='表名'
--or
SELECT TABLE_NAME,COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME='表名'
--查詢主鍵信息
SELECT TABLE_NAME,COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME='表名'
查询一下你插入的语句。。。
create table tb(id int identity(1,1) primary key, c int)
go
insert tb values(100)
insert tb values(200)
select * from tb
/*
id c
----------- -----------
1 100
2 200
*/
--重置种子为1
dbcc checkident('tb',reseed,1)
go
--此时再插入数据,id从1自增为2,与表中已有标识列重复,于是报错
insert tb values(300)
/*
消息 2627,级别 14,状态 1,第 1 行
违反了 PRIMARY KEY 约束 'PK__tb__70C8B53F'。不能在对象 'dbo.tb' 中插入重复键。
语句已终止。
*/
--重置种子为2,插入正常
dbcc checkident('tb',reseed,2)
go
insert tb values(300)
select * from tb
/*
id c
----------- -----------
1 100
2 200
3 300
*/
drop table tb