--表t3(a nvarchar(max)) 40W条数据
--执行语句
alter table t3 add id int identity primary key--报错
消息 1505,级别 16,状态 1,第 1 行
CREATE UNIQUE INDEX 终止,因为发现对象名称 'dbo.t3' 和索引名称 'PK__t3__619B8048' 有重复的键。重复的键值为 (226727)。
消息 1750,级别 16,状态 0,第 1 行
无法创建约束。请参阅前面的错误消息。
语句已终止。
drop掉t3,重新生成一个t3,再执行,有可能完成,也有可能这样失败,再失败时错误参数会略微变化:
'PK__t3__656C112C' 有重复的键。重复的键值为 (225333)
试下来一次可以,一次不可以,然后循环。妖怪了,还有貌似原表数据少的时候不会有问题。环境是SQL 2005这么妖的问题,估计只有高手能解决了
select id,count(id) from t3 group by id having count(id)>1
如果有结果集,则表示有重复值,因此不能创建唯一值约束,无法创建主键。
另外报错信息:重复的键值为 (226727)也已经说的很清楚了,应该是有2个或2个以上的值为226727。
另外t3前面最好加架构名,比如dbo.t3。因为如果有同名表,但是架构名不同,而又没有写清楚架构名,则系统默认dbo.t3。
select identity(int,1,1) id1,* into test from t3
go
alter table test add primary key (id1)
alter table增加一列id,新增加的一列怎么可能有重复值呢,唉,懒得说了
这个样不会有问题,而且速度很快。
请教我提出的那个问题该如何解决?
CREATE UNIQUE INDEX 终止,因为发现对象名称 'dbo.t3' 和索引名称 'PK__t3__123EB7A3' 有重复的键。重复的键值为 (26511)。
消息 1750,级别 16,状态 0,第 1 行
无法创建约束。请参阅前面的错误消息。
语句已终止。
一样呢,还是有这个错误
alter table t3 add id int identity
go
alter table t3 add primary key (id)
结果随机,有时可以,有时不可以消息 1505,级别 16,状态 1,第 1 行
CREATE UNIQUE INDEX 终止,因为发现对象名称 'dbo.t3' 和索引名称 'PK__t3__25518C17' 有重复的键。重复的键值为 (5)。
消息 1750,级别 16,状态 0,第 1 行
无法创建约束。请参阅前面的错误消息。
语句已终止。
go
create table dbo.t3(Num int)
go
declare @i int
set @i=1
while @i<=400001
begin
insert into dbo.t3(Num)
select @i
set @i=@i+1
end
go
alter table dbo.t3 add id int identity primary key
go
这是我后来测试用的语句,运行了3次,第4次出现错误消息 1505,级别 16,状态 1,第 1 行
CREATE UNIQUE INDEX 终止,因为发现对象名称 'dbo.t3' 和索引名称 'PK__t3__164452B1' 有重复的键。重复的键值为 (5)。
消息 1750,级别 16,状态 0,第 1 行
无法创建约束。请参阅前面的错误消息。
语句已终止。
if object_id('t3') is not null drop table t3
go
create table dbo.t3(Num int)
go
declare @max int;
declare @rc int;
set @rc=1;
set @max = 400001insert into t3 values(1);
while @rc*2<=400001
begin
insert into dbo.t3(Num) --帮楼主优化一下插入代码,您的代码效率太低,耗时太长
select Num + @rc from dbo.t3
set @rc = @rc * 2;
endinsert into dbo.t3(Num)
select Num + @rc from dbo.t3 where Num + @rc <= @max
go
alter table dbo.t3 add id int identity primary key
go
go我用这个代码原则上跟你的是一样的,但是运行10次没有出现一次你说的错误。请问你是什么版本的sql server
我的版本是:select @@version
Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86) Oct 14 2005 00:33:37 Copyright (c) 1988-2005 Microsoft Corporation Developer Edition on Windows NT 6.0 (Build 6002: Service Pack 2) select serverproperty('productversion')
,serverproperty('productlevel')
,serverproperty('edition')
9.00.1399.06 RTM Developer Edition
go
create table dbo.t3(Num int)
go
declare @i int
set @i=1
while @i<=400001
begin
insert into dbo.t3(Num)
select @i
set @i=@i+1
end
go
alter table dbo.t3 add id int identity-- primary key
go
select id from t3 group id having count(1)>1
//你不加主键。再查询看看有重复的吗,也每次都运行,你这情况不见到真实情况还以为干嘛了
----------- -----------
200367 19123
306968 19123
307974 24825
202003 24825
202004 24826
270238 24826
81558 24827
81559 24827(8 行受影响)可是为什么这句alter table dbo.t3 add id int identity 也居然会生成重复值呢?
--Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) Nov 24 2008 13:01:59 Copyright (c) 1988-2005 Microsoft Corporation Developer Edition on Windows NT 6.1 (Build 7601: Service Pack 1) select serverproperty('productversion')
,serverproperty('productlevel')
,serverproperty('edition')
--9.00.4035.00 SP3 Developer Edition你这个质疑很有道理,正常情况下identity属性是生成唯一的值,不过identity属性本身并不保证其值是唯一的,这点你一定要理解。if object_id('test_ident') is not null
drop table test_ident;
gocreate table test_ident
(
a int identity,
b int not null
);insert into test_ident(b)
values(1);
insert into test_ident(b)
values(2);
insert into test_ident(b)
values(3);
insert into test_ident(b)
values(4);
insert into test_ident(b)
values(5);set identity_insert test_ident on;
go
insert into test_ident(a, b)
values(1, 1);
go
set identity_insert test_ident off;
goselect *
from test_ident
order by a;要保证identity要唯一就只能创建unique和primary key就想你alter table一样。