2012-02-16 3 views
0

안녕하세요 내 3 개 지불 유형을 오토넷에 문제가 : 현금, 신용, 은행 그들 각각은 서로 다른 정보를 가지고표준화 3 데이터베이스 테이블

합니다.

  1. 비즈니스 프로세스 : 사용자가 선택합니다 : 세부 사항은 사용자의 신용 카드 결제에 (당신이 입력 신용 카드 정보, 은행 계좌 정보, 현금 세부 사항 (통화 등)해야 예에 대한) 즉 정의 콤보 상자에있는 그의 지불 유형 :
  2. 그런 다음 사용자는 해당 지불 유형의 세부 사항을 입력합니다.

이것은 내가 무엇을 시도했다입니다 :

PaymentType (PaymentType_ID (PK), PaymentTypes)

... ..... ...... .. .......

그 다음 붙어 버렸습니다. 나는 어떻게 해야할지 모르겠다. 도와주세요. 네가 대답 해 주시면 제발 설명해주세요. 나는 여기서 다시 같은 질문을하고 싶지 않다. 비슷한 상황에 직면하면.

*** 다른 열이 있기 때문에 모든 테이블을 1 개의 테이블로 병합 할 수 없습니다. 그들은 구체적인 세부 사항이 다릅니다 ...

+0

지불 유형에는 다른 세부 사항이 없습니다. * Payments * 세부 사항이 다릅니다. –

답변

0

세 가지 지불 유형 모두 공통점이 있습니다. 그들은 모두 계좌 번호, 금액, 타임 스탬프, 지불 유형 및 일종의 거래 식별자를 가지고 있습니다. 모든 공통 속성은 하나의 테이블에 있습니다. (그들은 응용 프로그램 의존적이기 때문에 데이터 유형 중 일부는 의도적으로 순진, 내가 응용 프로그램을 모른다.)

create table payment_types (
    payment_type_code char(2) primary key, 
    payment_type varchar(8) not null unique 
); 
insert into payment_types values 
('Ca', 'Cash'),('Cr', 'Credit'),('Ba', 'Bank'); 

create table payments (
    transaction_id integer primary key, 
    account_code varchar(5) not null, -- references accounts, not shown 
    amount_usd numeric(5,2) not null, 
    payment_type_code char(2) not null references payment_types (payment_type_code), 
    transaction_timestamp timestamp not null default current_timestamp, 
    unique (transaction_id, payment_type_code) 
); 

{TRANSACTION_ID, payment_type_code}에 고유 제한 조건은 SQL이의 쌍을 사용할 수 있습니다 열을 외래 키 제약 조건의 대상으로 사용합니다. 여러 테이블의 행을 섞지 않게하는 것이 중요합니다.

각 지불에는 지불 유형에 따라 다른 속성이 있습니다. 그리고 각 지불은 하나의 유형일 수 있습니다.

create table payment_cash (
    transaction_id integer primary key, 
    payment_type_code char(2) not null default 'Ca' check (payment_type_code = 'Ca'), 
    foreign key (transaction_id, payment_type_code) 
    references payments (transaction_id, payment_type_code), 
    other_cash_columns char(1) not null 
); 

create table payment_credit (
    transaction_id integer primary key, 
    payment_type_code char(2) not null default 'Cr' check (payment_type_code = 'Cr'), 
    foreign key (transaction_id, payment_type_code) 
    references payments (transaction_id, payment_type_code), 
    other_credit_columns char(1) not null 
); 

create table payment_bank (
    transaction_id integer primary key, 
    payment_type_code char(2) not null default 'Ba' check (payment_type_code = 'Ba'), 
    foreign key (transaction_id, payment_type_code) 
    references payments (transaction_id, payment_type_code), 
    other_bank_columns char(1) not null 
); 

payment_type_code의 기본값과 check 제약 조건으로 인해 예를 들어 현금 결제를위한 신용 세부 사항을 삽입 할 수 없습니다. 그 이 될 수 있습니다 - 외래 키 제약 조건이 트랜잭션 ID 만 사용하는 경우 잘못된 것입니다.

일반적으로 금융 거래에 대한 업데이트 또는 삭제를 계단식으로 수행하지 않습니다. 대신 보상 트랜잭션을 삽입하여 오류를 수정하십시오.

사용자 및 응용 프로그램 코드에보다 친숙하게 만들려면 결제 테이블을 세부 정보에 연결하는 세 개의 업데이트 가능한보기를 만드십시오. 그것들을 업데이트 가능하게 만드는 방법은 dbms에 달려 있습니다.

create view credit_payments_all as 
select p.transaction_id, p.account_code, p.amount_usd, 
     p.payment_type_code, p.transaction_timestamp, 
     c.other_credit_columns 
from payments p 
inner join payment_credit c on c.transaction_id = p.transaction_id 

-- Rules, triggers, stored procedures, functions, or whatever you need 
-- to make this view updatable. 

신용 거래를 삽입해야하는 코드는 credit_payments_all보기에 삽입 할 수 있습니다.