0

次のような3つのテーブルがFK関係にあるDBがあります。

表Aには、2つの(関連する)フィールドがあります
。TypeId(int)
LinkId(int)

表BとCにはそれぞれ、表AのLinkIdにマップされる主キーがあります。表AのTypeIdが1の場合、LinkIdは表Bの主キーにマップされます。2の場合、表Aの主キーにマップされます。 C。

この不適切に設計されたDBには、これらのテーブル間の参照整合性を強制する方法がありますか?つまり、対応するレコードがテーブルBまたはCに存在しない場合、SQL ServerがテーブルAにレコードを挿入するのを防ぐことはできますか?

4

1 に答える 1

1

You could use two new link tables and drop LinkId from table A

AB (Aid, Bid)
AC (Aid, Cid)

Depends on your app though and the control you have over the inserting...

You don't have to use built in constraints - you can write custom ones with triggers of course if you wanted to keep that structure.

something like:

   CREATE TRIGGER trigger_name ON A
      FOR INSERT, UPDATE

      AS

     declare @err varchar(50)

     select @err = case 
       when inserted.typeid = 1 and not exists(select 0 from B where id=inserted.linkid) then
            @err = 'No link record exists in B.'
       when inserted.typeid = 2 and not exists(select 0 from C where id=inserted.linkid) then
            @err = 'No link record exists in C.'
       else @err = null     
      end
      from inserted

     if @err is not null RAISERROR (@err, 16, 1 )
于 2012-05-06T17:44:21.153 に答える