1

特定の account_id に対して、SQL 制約 (SQLDeveloper を使用) が必要です。たとえば、添付されたデータなど、regular_id が 1 つしか存在しないか、存在しないことを確認する必要があります。値が異なる場合でも、「6」を含むセルは許可されるべきではありません。

AccountID    RegularID     OpenID
 1            5             null
 1            null          10
 1            null          11
 1            6                             <-- Forbidden
4

1 に答える 1

3

最良の方法はトリガーを使用することです

  Create trigger trig_NoSingleRegId 
  On MyTable For Insert, Update
  As

        if Exists(Select * From MyTable t
                   Where AccountId In (Select AcountId From inserted)
                   Group By AccountId   
                   Having Count(Distinct regularId) > 1)     
        Begin
            RollBack Transaction
            Raiserror('Cannot have more than one RegularId per AccountId', 16, 1)
        End    

注: Where 句はパフォーマンスのみを目的としており、トリガーをトリガーする update または insert によって挿入または更新された accountIds のみにトリガーを制限します。

または、結合を使用して同じ制限を達成することもできます。

  Create trigger trig_NoSingleRegId 
  On MyTable For Insert, Update
  As

        if Exists(Select * From MyTable t
                      join inserted I
                           on i.AccountId = t.AccountId
                   Group By t.AccountId   
                   Having Count(Distinct t.regularId) > 1)     
        Begin
            RollBack Transaction
            Raiserror('Cannot have more than one RegularId per AccountId', 16, 1)
        End           
于 2013-05-27T18:17:54.547 に答える