重新使用自动递增的主键

重新使用自动递增的主键

本文介绍了重新使用自动递增的主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为customer的表,如下所示

Suppose i have a table called customer which is as follows

create table customer(
customerid int identity(1,1) primary key,
customername varchar(50),
emailid varchar(30) unique,
address varchar(max),
stateid int foreign key references state(stateid)
)



现在我有一个名为deletecustom的表,就像是



now i have a table called deletecustom which is like

create table deletecustom(
deletionid int identity(1,1) primary key,
customerid int
)



i需要删除customer表中插入的值的条目deletecustom表

所以我使用像


i need to delete the entry in the customer table for the value inserted in the deletecustom table
so i use a trigger like

create trigger deletecustomer on deletecustom
for insert
as
declare @cid int;
select @cid=customerid from inserted
delete from customer where customer.customerid=@customerid



这将删除customer表中的记录。有没有办法我可以重新使用已删除的customerid字段创建另一个记录?我的意思是,如果我删除customerid为1的客户,我是否可以为其他客户使用customer id = 1才能正常使用?


This deletes the records from the customer table. Is there a way wherein i can re use the deleted customerid field for creation of one more record? What i mean is if i delete a customer whose customerid is 1, can i use customer id=1 for some other customer just for proper utilization?

推荐答案



这篇关于重新使用自动递增的主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 17:09