关于数据不重复,索引类的问题,请教

yjhack 2006-06-30 11:21:17
一张表里有6个列,如下
A | B | C | D | E | F

A,B,C,D列中的任意一列数据与E,F组合不能有重复的数据,比如表中已有如下数据
A | B | C | D | E | F
1 0 0 0 7 8
0 2 3 4 11 12

那么,如果A:1,E:7
D:0,F:8
B:2,E:11
C:3,F:12
这样的数据不能插入,也就是表中不能有这样的数据存在.

请各位高手帮忙解答,应当建什么样的索引或者用什么方法能够实现?




...全文
143 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
78984598 2006-06-30
  • 打赏
  • 举报
回复
create trigger trg_b
on b
INSTEAD OF insert
as
declare @ae int, @af int
declare @be int, @bf int
declare @ce int ,@cf int
declare @de int, @df int
declare @exist int

select @ae=checksum(a,e),@af=checksum(a,f),
@be=checksum(b,e),@bf=checksum(b,f),
@ce=checksum(c,e),@cf=checksum(c,f),
@de=checksum(d,e),@df=checksum(d,f)
from inserted

select @exist =count(1) from [table] where checksum(a,e)=@ae or checksum(b,e)=@be or checksum(c,e)=@ce or checksum(d,e)=@de or checksum(a,f)=@af or
checksum(b,f)=@bf or checksum(c,f)=@cf or checksum(d,f)=@df

if(@exist>0)
raiserror('数据已经存在!',16,1)
else begin
insert into [table] select a,b,c,d,e,f from inserted
end
LouisXIV 2006-06-30
  • 打赏
  • 举报
回复
类似于这样的

create trigger tr_test on testtable
for insert
as
if exists
(
select 1
from testtable a,inserted b
where
(
a.A=b.A or
a.B=b.B or
a.C=b.C or
a.D=b.D
)
and
(
a.E=b.E or
a.F=b.F
)
)
rollback transaction
LouisXIV 2006-06-30
  • 打赏
  • 举报
回复
逻辑复杂了点

用触发器好了
marine8086 2006-06-30
  • 打赏
  • 举报
回复
难道建四个?
A,E,F
B,E,F
C,E,F
D,E,F
yjhack 2006-06-30
  • 打赏
  • 举报
回复
78984598(贺鑫) :你的方法对正要插入的数据不能够实现判断,对已插入的重复数据可以判断.但是如果是A,B,C,D 任意一列,插入如与其中有一列相同的,也不能插入.

LouisXIV(夜游神) :效果是实现了,有点不明白where ... and...那句的意思

还是要万分感谢你.

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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