我有一个有许多表的数据库,其中4个是
信用卡属性:
PayPal属性:
比特币属性:
付款表属性:
支付只能由卡/贝宝/比特币支付,所以我打破了第三种形式,因为如果客户使用卡,我知道他没有使用贝宝或比特币。我怎样才能解决这个问题,这样我就不会破坏第三种正常形态。
发布于 2015-04-23 12:54:52
在今天的SQL中,没有一种完全干净的方法可以做到这一点,因为SQL平台不支持断言。(在SQL标准中创建断言),但是您可以设计表以支持合理的约束,即使不支持断言。
将所有定期付款“向上”常见的属性推入"scheduled_payments“表。
create table scheduled_payments (
pmt_id integer primary key,
pmt_amount numeric(14, 2) not null
check (pmt_amount > 0),
pmt_type char(1) not null
check (pmt_type in ('b', 'c', 'p')), -- (b)itcoin, (c)redit card, (p)aypal.
other_columns char(1) not null default 'x', -- Other columns common to all payment types.
unique (pmt_id, pmt_type)
);
-- Tables for Bitcoin and PayPal not shown, but they're very similar
-- to this table for credit cards.
create table credit_cards (
pmt_id integer primary key,
pmt_type char(1) not null default 'c'
check (pmt_type = 'c'),
foreign key (pmt_id, pmt_type)
references scheduled_payments (pmt_id, pmt_type),
other_columns char(1) not null default 'x' -- Other columns unique to credit cards.
);“primary key”中的not null和check(...)约束保证每一行都有一个支付标识号和一个'c‘。外键约束保证"credit_cards“中的每一行将引用"scheduled_payments”中的'c‘行。
https://stackoverflow.com/questions/29811222
复制相似问题