세 가지 지불 유형 모두 공통점이 있습니다. 그들은 모두 계좌 번호, 금액, 타임 스탬프, 지불 유형 및 일종의 거래 식별자를 가지고 있습니다. 모든 공통 속성은 하나의 테이블에 있습니다. (그들은 응용 프로그램 의존적이기 때문에 데이터 유형 중 일부는 의도적으로 순진, 내가 응용 프로그램을 모른다.)
는
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보기에 삽입 할 수 있습니다.
지불 유형에는 다른 세부 사항이 없습니다. * Payments * 세부 사항이 다릅니다. –