日期:2014-05-18  浏览次数:20729 次

求助如何让2个字段相同的表的主键之间不能重复?
求助!
2个mssql数据库表   A和B
字段均为id,link
id为int自动增长的
link为varchar   设为主键
要求:A和B的link字段内容之间绝对不能有重复值

请给出具体代码,谢谢!

------解决方案--------------------
看错了,只判断link的值.
修改如下:
create trigger ti_A on A
for insert
as
if exists(select 1 from inserted i,B where i.link=B.link)
begin
raiserror( 'link不能重复! ',16,1)
rollback tran
return
end
GO
create trigger ti_B on B
for insert
as
if exists(select 1 from inserted i,A where i.link=A.link)
begin
raiserror( 'link不能重复! ',16,1)
rollback tran
return
end
GO
create trigger tu_A on A
for update
as
if update(link)
and exists(select 1 from inserted i,deleted d,B where i.id=d.id and i.link <> d.link and i.link=B.link)
begin
raiserror( 'link不能重复! ',16,1)
rollback tran
return
end
GO
create trigger tu_B on B
for update
as
if update(link)
and exists(select 1 from inserted i,deleted d,A where i.id=d.id and i.link <> d.link and i.link=A.link)
begin
raiserror( 'link不能重复! ',16,1)
rollback tran
return
end
GO