去掉表的identity属性

在我们的业务中,我们经常碰到去掉表的identity属性的需求。但是sql并没有提供可以直接去掉该属性的sql语句。但是其实我们自己可以通过三种方式来实现。

第一种:通过界面操作

将以上图中的yes改为no ,然后点击保存,这里需要注意一个问题,sql server好像默认是不允许这样改,这里我们就需要修改一个选项

 

需要将以上红色框内的选项去掉,这样就可以保存修改,并且保存修改所使用的脚本。

第二种方式,其实就跟上面类似,只是我们可以按照它的原理通过脚本实现。

 1 use Test
 2 go
 3 if OBJECT_ID('dbo.RemoveIdentiyTest') is not null
 4    drop table dbo.RemoveIdentiyTest
 5 go
 6 --新建一个测试表
 7 create table dbo.RemoveIdentiyTest
 8 (
 9  id int identity not null,
10  col1 varchar(10) not null constraint DF_RemoveIdentiyTest_col1 default 'aaa',
11  col2 varchar(10) null,
12  col3 int null,
13  col4 int null,
14  col5 char(10) null
15  constraint PK_RemoveIdentiyTest primary key clustered
16  (
17    id asc
18  )
19 ) ON [PRIMARY]
20 go
21 create nonclustered index IX_RemoveIdentiyTest_col3_col4 on dbo.RemoveIdentiyTest
22 (
23  col3,col4
24 )with(fillfactor=80) on [primary]
25 go
26 create  nonclustered index IX_RemoveIdentiyTest_col5 on dbo.RemoveIdentiyTest
27 (
28  col5
29 )with(fillfactor=80)on [primary]
30 
31 --method 2:insert date into a tempoprary table
32 BEGIN TRANSACTION
33 GO
34 --为了保证临时表中的数据与原表保持一致,我们要在插入数据前给临时表加对应的默认值约束,但是同一个数据库不能建名称一样的约束,
--所以这里我们先
删除原表的约束,这个操作并不影响现存表中的数据 35 ALTER TABLE dbo.RemoveIdentiyTest 36 DROP CONSTRAINT DF_RemoveIdentiyTest_col1 37 GO 38 --新建一个临时表 39 CREATE TABLE dbo.Tmp_RemoveIdentiyTest 40 ( 41 id int NOT NULL, 42 col1 varchar(10) NOT NULL, 43 col2 varchar(10) NULL, 44 col3 int NULL, 45 col4 int NULL, 46 col5 char(10) NULL 47 ) ON [PRIMARY] 48 GO 49 --给临时表加锁 50 ALTER TABLE dbo.Tmp_RemoveIdentiyTest SET (LOCK_ESCALATION = TABLE) 51 GO 52 --为了保证临时表中的数据与原表保持一致,我们要在插入数据前给临时表加对应的默认值约束 53 ALTER TABLE dbo.Tmp_RemoveIdentiyTest ADD CONSTRAINT 54 DF_RemoveIdentiyTest_col1 DEFAULT ('aaa') FOR col1 55 GO 56 --导数据 57 IF EXISTS(SELECT * FROM dbo.RemoveIdentiyTest) 58 EXEC('INSERT INTO dbo.Tmp_RemoveIdentiyTest (id, col1, col2, col3, col4, col5) 59 SELECT id, col1, col2, col3, col4, col5 FROM dbo.RemoveIdentiyTest WITH (HOLDLOCK TABLOCKX)') 60 GO 61 --删除原表 62 DROP TABLE dbo.RemoveIdentiyTest 63 GO 64 --将临时表更名为正式表 65 EXECUTE sp_rename N'dbo.Tmp_RemoveIdentiyTest', N'RemoveIdentiyTest', 'OBJECT' 66 GO 67 --添加相应的索引 68 ALTER TABLE dbo.RemoveIdentiyTest ADD CONSTRAINT 69 PK_RemoveIdentiyTest PRIMARY KEY CLUSTERED 70 ( 71 id 72 ) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 73 74 GO 75 CREATE NONCLUSTERED INDEX IX_RemoveIdentiyTest_col3_col4 ON dbo.RemoveIdentiyTest 76 ( 77 col3, 78 col4 79 ) WITH( PAD_INDEX = OFF, FILLFACTOR = 80, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 80 GO 81 CREATE NONCLUSTERED INDEX IX_RemoveIdentiyTest_col5 ON dbo.RemoveIdentiyTest 82 ( 83 col5 84 ) WITH( PAD_INDEX = OFF, FILLFACTOR = 80, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 85 GO 86 COMMIT

方法三:也是通过脚本实现,但是这里我们使用switch to的方式导数据(这是从分区表借鉴过来的),当然这个两个表switch to有一些限制条件,比如两个表有相同的表结构,两个表必须在同一个数据库等。

 1 use Test
 2 go
 3 if OBJECT_ID('dbo.RemoveIdentiyTest') is not null
 4    drop table dbo.RemoveIdentiyTest
 5 go
 6 --新建一个测试表
 7 create table dbo.RemoveIdentiyTest
 8 (
 9  id int identity not null,
10  col1 varchar(10) not null constraint DF_RemoveIdentiyTest_col1 default 'aaa',
11  col2 varchar(10) null,
12  col3 int null,
13  col4 int null,
14  col5 char(10) null
15  constraint PK_RemoveIdentiyTest primary key clustered
16  (
17    id asc
18  )
19 ) ON [PRIMARY]
20 go
21 create nonclustered index IX_RemoveIdentiyTest_col3_col4 on dbo.RemoveIdentiyTest
22 (
23  col3,col4
24 )with(fillfactor=80) on [primary]
25 go
26 create  nonclustered index IX_RemoveIdentiyTest_col5 on dbo.RemoveIdentiyTest
27 (
28  col5
29 )with(fillfactor=80)on [primary]
30 
31 go
32 
33 --插入测试数据
34 declare @i  int,@a int,@b int
35 set @i=0
36 set @a=10000
37 set @b=0
38 while(@i<100000)
39     begin
40         insert into dbo.RemoveIdentiyTest
41         select 'aa'+cast(@i as varchar)+'ff','bb'+cast(@i as varchar)+'dd',@a,@b,'A'+cast(@i as varchar)
42         set @i=@i+1;
43         set @a=@a-1;
44         set @b=@b+2;
45     end
46 go
47 
48 begin try
49     begin transaction 
50 
51     alter table dbo.RemoveIdentiyTest drop constraint DF_RemoveIdentiyTest_col1
52     if OBJECT_ID('dbo.tmp_RemoveIdentiyTest') is not null
53          drop table dbo.tmp_RemoveIdentiyTest
54     create table dbo.tmp_RemoveIdentiyTest
55     (
56      id int  not null,
57      col1 varchar(10) not null,
58      col2 varchar(10) null,
59      col3 int null,
60      col4 int null,
61      col5 char(10) null
62      constraint PK_tmp_RemoveIdentiyTest primary key clustered
63      (
64        id asc
65      )
66     ) ON [PRIMARY]
67 
68     create nonclustered index IX_tmp_RemoveIdentiyTest_col3_col4 on dbo.tmp_RemoveIdentiyTest
69     (
70      col3,col4
71     )with(fillfactor=80) on [primary]
72 
73     create  nonclustered index IX_tmp_RemoveIdentiyTest_col5 on dbo.tmp_RemoveIdentiyTest
74     (
75      col5
76     )with(fillfactor=80)on [primary]
77      --这里使用switch to的方式,比insert 导数据的效率要高很多
78     alter table dbo.RemoveIdentiyTest SWITCH  to dbo.tmp_RemoveIdentiyTest
79     alter table dbo.tmp_RemoveIdentiyTest add constraint DF_RemoveIdentiyTest_col1  DEFAULT ('aaa') FOR col1
80     drop table dbo.RemoveIdentiyTest
81     exec sp_rename  N'Test.dbo.tmp_RemoveIdentiyTest.IX_tmp_RemoveIdentiyTest_col3_col4',N'IX_RemoveIdentiyTest_col3_col4',N'index'
82     exec sp_rename  N'Test.dbo.tmp_RemoveIdentiyTest.IX_tmp_RemoveIdentiyTest_col5',N'IX_RemoveIdentiyTest_col5',N'INDEX'
83     exec sp_rename  N'PK_tmp_RemoveIdentiyTest',N'PK_RemoveIdentiyTest',N'object'
84     exec sp_rename 'Tmp_RemoveIdentiyTest', 'RemoveIdentiyTest','object'
85    commit
86 end    try
87 begin catch
88     print 'error'
89     rollback tran
90 end catch

 

对于以上三种方式,我个人认为如果数据量不是很大,我们可以直接采用第一种或者第二种,但是如果数据量比较大,我们没有足够的磁盘空间或者修改该表所需时间紧迫的情况下,我们可以采用第三种方式。

 

posted @ 2014-07-01 15:39  shihuai355  阅读(1315)  评论(0编辑  收藏  举报