删除数据第一行,为什么ID从2开始,怎么解决?
用delete删除数据时,删除第一行,第一行的信息没有了,但ID是从2开始的,怎么解决?在不删除其他数据的情况下怎么让ID从一开始?
[解决办法]
利用触发器实现标识列连续。(支持批量插入) if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ttt]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[ttt]GO/****** Object: Table [dbo].[ttt] Script Date: 2008-12-15 17:11:26 ******/CREATE TABLE [dbo].[ttt] ( [id] [int] IDENTITY (1, 1) NOT NULL , [name] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL , [time] [datetime] NULL ) ON [PRIMARY]GOALTER TABLE [dbo].[ttt] ADD CONSTRAINT [PK_ttt] PRIMARY KEY CLUSTERED ( [id] ) ON [PRIMARY] GOinsert into ttt(name,time) values('logan',getdate());insert into ttt(name,time) values('peter',getdate());insert into ttt(name,time) values('man',getdate());insert into ttt(name,time) values('lida',getdate());insert into ttt(name,time) values('fcuandy',getdate());select * from ttt/*1 logan 2008-12-15 17:36:37.7802 peter 2008-12-15 17:36:37.7803 man 2008-12-15 17:36:37.7804 lida 2008-12-15 17:36:37.7805 fcuandy 2008-12-15 17:36:37.793*/GOCREATE TRIGGER tr ON tttINSTEAD OF INSERTAS SET IDENTITY_INSERT ttt ON DECLARE @n INT SELECT @n=MAX(id) FROM ttt ;WITH fc AS ( SELECT n=1 UNION ALL SELECT nn=n+1 FROM fc WHERE n<@n ),fc1 AS ( SELECT n FROM fc a LEFT JOIN ttt b ON a.n = b.id WHERE b.id IS NULL ) INSERT ttt(id,name,time) SELECT n,name,time FROM (SELECT ROW_NUMBER() OVER (ORDER BY GETDATE()) idx,n FROM fc1) a INNER JOIN ( SELECT idx=ROW_NUMBER() OVER (ORDER BY GETDATE()),name,time FROM inserted ) b ON a.idx=b.idx DECLARE @r INT SELECT @r=@@ROWCOUNT SET IDENTITY_INSERT ttt OFF INSERT ttt(name,time) SELECT name,time FROM ( SELECT idx=ROW_NUMBER() OVER (ORDER BY GETDATE()),name,time FROM inserted ) x WHERE idx>@r GODELETE FROM ttt WHERE name = 'peter' OR name='lida'GOINSERT ttt SELECT 'xxx',getdate()INSERT ttt SELECT 'yyy',GETDATE()GOSELECT * FROM ttt/*1 logan 2008-12-15 17:37:20.9672 xxx 2008-12-15 17:37:21.0133 man 2008-12-15 17:37:20.9674 yyy 2008-12-15 17:37:21.0305 fcuandy 2008-12-15 17:37:20.967*/DELETE FROM ttt WHERE name ='xxx' OR name='yyy'INSERT ttt SELECT 'roy_88',GETDATE() UNION ALL SELECT 'limpire',GETDATE() UNION ALL SELECT '熊',GETDATE()SELECT * FROM ttt/*1 logan 2008-12-15 17:38:29.4502 roy_88 2008-12-15 17:38:29.5303 man 2008-12-15 17:38:29.4674 limpire 2008-12-15 17:38:29.5305 fcuandy 2008-12-15 17:38:29.4676 熊 2008-12-15 17:38:29.530*/本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fcuandy/archive/2008/12/15/3522876.aspx
[解决办法]
--使用dbcc checkident检查和设置表的标识值create table tb( id int primary key identity, name varchar(50)) insert into tb select 'a' union all select 'b' union all select 'c' union all select 'd'go dbcc checkident(tb,noreseed)godelete from tb where id>2go--删除记录后,表tb只剩下两条记录了,但是此时表tb的标识值仍为4,可以用下面的语句重置标识值为2dbcc checkident(tb,reseed,2)godbcc checkident(tb,noreseed)go
[解决办法]
取消自增长,手动填入ID
[解决办法]
使用truncate即可,LZ试试吧。
[解决办法]
楼主,提供下面两种方法,希望可以解决你的问题。
方法一:
你想要重置标识值(不删除数据)采用下面方法即可:但是存在问题:
1.SELECT IDENT_SEED ('表名') (查询你的种子值)
2.SELECT IDENT_CURRENT('表名') (查询当前标示值)
3.DBCC CHECKIDENT ('表名', RESEED, new_value)(重置新的标识值,new_value为新值)
4.问题:如dbcc checkident ('表名',reseed,1)即可,但如果表内有数据,则重设的值如果小于当前表的标识最大值,再插入数据时未指定插入的标识值,这样会导致标识冲突问题,如果你的标识设置成自增的。此外,你也可以用 dbcc checkident('表名',reseed),即可自动重设值,最后生成值。
方法二:
使用用Truncate
TRUNCATE TABLE name 可以删除表内所有值并重置标识值(表内的数据会丢失)
[解决办法]
id 对数据查询没影响啊
[解决办法]
CREATE TABLE dbo.T_TEST(id int);
INSERT INTO dbo.T_TEST
( id )
VALUES ( 1 -- id - int
);
SELECT * FROM dbo.T_TEST;
/*手动控制序列id*/
DECLARE @id INT;
UPDATE dbo.T_TEST SET @id=id=id+1;
SELECT @id;