关于主键的问题

cc77077 2003-10-30 11:06:50
我以前建立了一个数据表,没有设置主键,现在我想设置一个字段成主键,但是那个字段的有些记录已经重复了,有没有方法解决?最好给点原代码,还有我想设置的是两个字段联合才成为一个主键,怎么实现?先谢了
...全文
26 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
cc77077 2003-11-04
  • 打赏
  • 举报
回复
谢谢各位的帮忙的,特别感谢大力帮我还解决了上一个提问的,不好意思现在才结贴,呵呵,这几天打游戏!
lvltt 2003-10-30
  • 打赏
  • 举报
回复
alter table t1 add constraint pk_1 primary key (a,b)
lynx1111 2003-10-30
  • 打赏
  • 举报
回复
既然楼主不知道主健是什么,建议你每个表都加上自增字段(identity(1,1))作为主健
txlicenhe 2003-10-30
  • 打赏
  • 举报
回复
一: 需先除去重复值
蚂蚁的:去除重复值
如果有ID字段,就是具有唯一性的字段

delect table where id not in (

select max(id) from table group by col1,col2,col3...
)
group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。



2,如果是判断所有字段也可以这样
select * into #aa from table group by id1,id2,....
delete table
insert into table
select * from #aa



3,没有ID的情况

select identity(int,1,1) as id,* into #temp from tabel
delect # where id not in (
select max(id) from # group by col1,col2,col3...)
delect table
inset into table(...)
select ..... from #temp


col1+','+col2+','...col5 联合主键


select * from table where col1+','+col2+','...col5 in (

select max(col1+','+col2+','...col5) from table
where having count(*)>1
group by col1,col2,col3,col4
)
group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。

2,
select identity(int,1,1) as id,* into #temp from tabel
select * from #temp where id in (
select max(id) from #emp where having count(*)>1 group by col1,col2,col3...)

二:alter table t1 add constraint pk_1 primary key (a,b)


银狐被占用 2003-10-30
  • 打赏
  • 举报
回复
主键不可以重复的!

你可以用ALTER再加一列来当主键!

用法看联机帮助!
lynx1111 2003-10-30
  • 打赏
  • 举报
回复
不能重复,主健是一行的唯一标识
txlicenhe 2003-10-30
  • 打赏
  • 举报
回复
一: 先要删除重复值

蚂蚁的:去除重复值
如果有ID字段,就是具有唯一性的字段

delect table where id not in (

select max(id) from table group by col1,col2,col3...
)
group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。



2,如果是判断所有字段也可以这样
select * into #aa from table group by id1,id2,....
delete table
insert into table
select * from #aa



3,没有ID的情况

select identity(int,1,1) as id,* into #temp from tabel
delect # where id not in (
select max(id) from # group by col1,col2,col3...)
delect table
inset into table(...)
select ..... from #temp


col1+','+col2+','...col5 联合主键


select * from table where col1+','+col2+','...col5 in (

select max(col1+','+col2+','...col5) from table
where having count(*)>1
group by col1,col2,col3,col4
)
group by 子句后跟的字段就是你用来判断重复的条件,如只有col1,那么只要col1字段内容相同即表示记录相同。

2,
select identity(int,1,1) as id,* into #temp from tabel
select * from #temp where id in (
select max(id) from #emp where having count(*)>1 group by col1,col2,col3...)


二:alter table t1 add constraint pk_1 primary key (a,b)
pengdali 2003-10-30
  • 打赏
  • 举报
回复
删除重复的,只留一条:

alter table 表 add newfield int identity(1,1)

delete 表
where newfield not in(
select min(newfield) from 表 group by 你想当主键的列1,你想当主键的列2
)

alter table 表 drop column newfield
yun198183 2003-10-30
  • 打赏
  • 举报
回复
大力,主键可以重复吗?
pengdali 2003-10-30
  • 打赏
  • 举报
回复
建表:
create table t1(a int not null,b int not null,c int not null)
加:
alter table t1 add constraint pk_1 primary key (a,b)
删:
alter table t1 drop constraint pk_1
再加:
alter table t1 add constraint pk_1 primary key (a,b,c)
yun198183 2003-10-30
  • 打赏
  • 举报
回复
这个,有重复的就不好办了,但是如果有重复就体现不出主键的价值了
这个你应该考虑一下
pengdali 2003-10-30
  • 打赏
  • 举报
回复
加:
alter table t1 add constraint pk_1 primary key (a,b)

34,874

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧