2017-04-02 1 views
1

私はasp.net mvc5を使用してサイトを開発しており、私はこの分野で長年の経験を持つdbaと一緒に作業しています。 次の表の間に以下の関係について矛盾がありました。ジャンクションテーブルを使用する必要がありますか?

Award table 
-ID 
-.. the table fields 
-supplierID 
-employeeID 
-otherID 

Supplier 
-ID 
-.. the table fields 

employee table 
-ID 
-.. the table fields 

other table 
-ID 
-.. the table fields 

賞表の最後の3行は、賞の受益者のタイプです。あなたは複数の受益者を持つことはできません。

私の提案は、さまざまな "受益者"と "賞"の表をリンクするためのリンクテーブル "受益者"を使用することでした。

どう思いますか?

+0

申し訳ありませんが、私はその質問を理解していません。 –

+0

@ZoharPele私はそれが間違ってfrstの時間を持っていたスキーマを編集しました。 – madlan

答えて

0

あなたは「1対1」の関係にあります。これはSQLではやりにくく、3つの列を使用する方法はそれを実行する良い方法です。どうして?適切な外部キー宣言が可能です。

私はこのようなテーブル定義を探します:それは受益者の別のタイプが追加されていることは本当に珍しい場合

create table Awards (
    awardsId int identity primary key, 
    . . . 
    supplierId int, 
    employeeID int, 
    otherID int, 
    beneficiaryType as (case when supplierId is not null then 'S' 
          when employeeId is not null then 'E' 
          when otherId is not null then 'O' 
         end), 
    constraint chk_beneficiary 
     check ((supplierId is not null and employeeId is null and otherid is null) or 
       (supplierId is null and employeeId is not null and otherid is null) or 
       (supplierId is null and employeeId is null and otherid is not null) 
      ),  
    constraint fk_awards_supplierId foreign key (supplierId) references Suppliers(supplierId), 
    constraint fk_awards_employeeId foreign key (employeeId) references Employees(employeeId), 
    constraint fk_awards_supplierId foreign key (otherId) references Others(otherId) 
); 

、その後、データベースのメンテナンスは大きな問題ではありません。

私はあなたにも「汎用」の列で上記を行うと、計算カラムを持続できることに注意してください:

create table Awards (
    awardsId int identity primary key, 
    . . . 
    beneficiaryType char(1) not null, 
    beneficiaryId int not null, 
    constraint chk_awards_beneficiaryType check (beneficiaryType in ('E', 'S', 'O')), 
    supplierId as (case when beneficiaryType = 'S' then beneficiaryId end) persisted, 
    employeeID as (case when beneficiaryType = 'E' then beneficiaryId end) persisted, 
    otherID as (case when beneficiaryType = 'O' then beneficiaryId end) persisted, 
    constraint fk_awards_supplierId foreign key (supplierId) references Suppliers(supplierId), 
    constraint fk_awards_employeeId foreign key (employeeId) references Employees(employeeId), 
    constraint fk_awards_supplierId foreign key (otherId) references Others(otherId) 
); 

計算カラムは、これがない、外部キーとして使用するために永続化する必要があるためスペースを節約しません。

  • をこれが行う

新しい外部キー制約を追加する新しい計算列

  • を追加beneficiaryType
  • ためcheck制約を変更する:しかし、新しいタイプを追加するだけでやっての問題ですすべてのテーブルのすべてのIDが同じタイプである必要があります。しかし、私は(ほぼ)IDカラムを主キーとして常に使用しています。したがって、これは私が設計したどのデータベースでも真です。

  • +0

    この回答は正しいです。明確な説明のためにThnx。そして、私は、テーブルの3つの列をチェックして、受益者タイプが何であるかを調べるのが難しい構造であると不平を言っていました。しかし、beneficiaryTypeカラムと制約を使うと、それはよさそうです。再びThnx – madlan

    関連する問題