去掉identity列属性范例

当我们需要去掉一个列上的identity属性时。我们需要做的就是:创建一个完全一样的新表,再将数据转移仅新表中,删除旧表。重命名新表。下面就是一个此过程的范例,值得注意的是,当旧表数据量很大时,为了避免大批量的数据转移我们采用了关键字swich to这大大节约了性能消耗成本 alter table dbo.RemoveIdentiyTest SWITCH to dbo.tmp_Rem
摘要由CSDN通过智能技术生成

当我们需要去掉一个列上的identity属性时。

我们需要做的就是:创建一个完全一样的新表,再将数据转移仅新表中,删除旧表。重命名新表。

下面就是一个此过程的范例,值得注意的是,当旧表数据量很大时,为了避免大批量的数据转移我们采用了关键字swich to这大大节约了性能消耗成本

	alter table dbo.RemoveIdentiyTest SWITCH  to dbo.tmp_RemoveIdentiyTest

下面为范例脚本全文

--更改输出->option->Designers->prevent saving changes that require table re-creation
use Test
go
if OBJECT_ID('dbo.RemoveIdentiyTest') is not null
   drop table dbo.RemoveIdentiyTest
go
create table dbo.RemoveIdentiyTest
(
 id int identity not null,
 col1 varchar(10) not null constraint DF_RemoveIdentiyTest_col1 default 'aaa',
 col2 varchar(10) null,
 col3 int null,
 col4 int null,
 col5 char(10) null
 constraint PK_RemoveIdentiyTest primary key clustered
 (
   id asc
 )
) ON [PRIMARY]
go
create nonclustered index IX_RemoveIdentiyTest_col3_col4 on dbo.RemoveIdentiyTest
(
 col3,col4
)with(fillfactor=80) on [primary]
go
create  nonclustered index IX_RemoveIdentiyTest_col5 on dbo.RemoveIdentiyTest
(
 col5
)with(fillfactor=80)on [primary]

go
declare @i  int,@a int,@b int
set @i=0
set @a=10000
set @b=0
while(@i<100000)
	begin
		insert into dbo.RemoveIdentiyTest
		select 'aa'+cast(@i as varchar)+'ff','bb'+cast(@i as varchar)+'dd',@a,@b,'A'+cast(@i as varchar)
		set @i=@i+1;
		set @a=@a-1;
		set @b=@b+2;
	end
go

set statistics time on
set statistics io  on

--method 1:alter table switch to 
begin try
    begin transaction 
    alter table dbo.RemoveIdentiyTest drop constraint DF_RemoveIdentiyTes
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值