tabMain:
A,B,C,主键是A+B
tabOne:
D,E,F,D的数据类型与A相同
要求:
D列数据必须全部来源于A列,A改变tabOne的D列相应改变,A删除时,如tabOne中D列有A列数据则禁止。

解决方案 »

  1.   

    A删除时,如tabOne中D列有A列数据则禁止。禁止啥概念?
    理论上这需求肯定是用触发器来搞定
      

  2.   

    参考 http://technet.microsoft.com/zh-cn/library/ms175464.aspx
      

  3.   

    建外键不行啊,因为tabMain的主键是A+B,A只是主键的组成之一
      

  4.   

    触发器在tabmain 中建更新触发器,在更新a列时,同步更新tableone的d列数据,
    create trigger tupdate_tabmain
    on tabmain
    for update
    as 
    begin
    if update(a)
    begin
    update tableone
    set d= i.a
    from inserted i,deleted d
    where i.id = d.id
    and i.a <> d.a
    end
    end在tabone建新增及更新触发器,
    新增及更新时,检查d存在于tabmain的a列中
    create trigger tiupdate_tabone
    on tabone
    for insert,update
    as 
    begin
    if exists(select 1 from inserted  i
    where i.d is not null
    and not exists(select 1 from tabmain t where t.a = i.d))
    begin
    raiserror('d不存在于tabmain的a列中',16,1) 
    rollback tran
    end
    end
      

  5.   


    触发器在tabmain 中建更新触发器,在更新a列时,同步更新tableone的d列数据,
    create trigger tupdate_tabmain
    on tabmain
    for update
    as 
    begin
    if update(a)
    begin
    update tableone
    set d= i.a
    from inserted i,deleted d
    where i.id = d.id
    and i.a <> d.a
    and tableone.d = d.a
    end
    end在tabone建新增及更新触发器,
    新增及更新时,检查d存在于tabmain的a列中
    create trigger tupdate_tabmain
    on tabmain
    for insert,update
    as 
    begin
    if exists(select 1 from inserted  i
    where i.d is not null
    and not exists(select 1 from tabmain t where t.a = i.d))
    begin
    raiserror('d不存在于tabmain的a列中',16,1) 
    rollback tran
    end
    end