0

Here, there are a great answer about this question but i don't found clear examples about what's next, for example, queries (select).

I'm going to describe an example and I want to know if i do correctly so:

We have a base class about payments:

Payments( code_payment (PK), description )

And then, we have 3 sub-class (3 differents types of payments on inheritance ):

Cash( code_payment (FK) )
CreditCard( creditcard_account , code_payment(FK) )
PromissoryNote( pay_date , code_payment(FK) )

For example: for insert statements, first, insert on Payments table and second, depending of type of payments ( I think in code you use if/else clause to separate the types of payments and do the correct "insert statement"), insert where belongs. And what happens with select statements?

Imagine that i want to know what type of payment had a specific document assuming that I have a table called Document that it is connected with Payments table ( so Document table have a foreign key to Payments (code_payment) ).

First i should do is to get the "description" of the payments by making a query on Document and Payments table (basically an inner join) and then, depending of the result (cash, credic card or promissory note) make a query on the table that belongs.

Is this what I suppose to do? am I on the correct way? Maybe it can works but it looks like a little bit... you know.. no elegant solution. I am a little confuse about that.

Thanks in advance.

4

1 に答える 1

1

通常、「サブクラス」ごとに 1 つの更新可能なビューを作成します。各ビューは、「基本クラス」テーブルを「サブクラス」テーブルに結合します。

アプリケーション コードは、ベース テーブルではなく、ビューを使用します。

支払いタイプは排他的であるため (1 回の支払いで現金とクレジット カードの両方を使用することはできません)、重複する制約を使用する必要があります。

create table payments (
  code_payment integer not null, -- I hope this is some kind of payment identifier.
  payment_type char(2) not null
    check (payment_type in ('CA', 'CC', 'PN')),
  amount decimal(14, 2) not null check (amount > 0),
  other_columns char(1) not null default 'X',
  primary key (code_payment),
  unique (code_payment, payment_type)
);

create table cash_payments (
  code_payment integer not null,
  payment_type char(2) not null default 'CA'
    check (payment_type = 'CA'),
  other_columns char(1) not null default 'X',
  primary key (code_payment),
  unique (code_payment, payment_type),
  foreign key (code_payment, payment_type) 
    references payments (code_payment, payment_type) 
);

クレジット カード支払いと約束手形支払いのテーブルは似ています。

の一意制約payments (code_payment, payment_type)により、これらの列を外部キー制約のターゲットにできます。テーブル「cash_payments」のチェック制約と外部キー制約により、「cash_payments」の行が常に支払いテーブルの現金行と一致することが保証されます。他の種類の行と一致することはありません。"cash_payments" の一意の制約により、"payments" の一意の制約と同様に、指定された列をさらに外部キー制約のターゲットにすることができます。

Payments テーブルに接続されている Document というテーブルがあると仮定して、どのタイプの支払いに特定のドキュメントがあったかを知りたいと想像してください (したがって、Document テーブルには Payments *(code_payment)* への外部キーがあります)。

ドキュメントは、次のいずれかを参照する外部キーを使用して支払いに関連付けることができます

  • 「code_payment」列、または
  • 列「code_payment」と「payment_type」のペア。

「ドキュメント」テーブルが列のペアを参照する場合、支払いのタイプが何であるかがわかるため、どのテーブルに参加する必要があるかがわかります。

于 2013-05-15T12:17:48.467 に答える