首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据库设计第三范式

数据库设计第三范式
EN

Stack Overflow用户
提问于 2015-04-23 00:08:07
回答 1查看 76关注 0票数 1

我有一个有许多表的数据库,其中4个是

  1. 付款
  2. 信用卡
  3. Paypal
  4. 比特币

信用卡属性:

  • cardID (PK)
  • 类型
  • expireDate
  • ..。

PayPal属性:

  • paypalID (PK)
  • 账号
  • ..。

比特币属性:

  • bitcoinID (PK)
  • ..。

付款表属性:

  • 数量
  • ..。
  • ..。
  • cardID (FK)
  • paypalID (FK)
  • bitcoinID (FK)

支付只能由卡/贝宝/比特币支付,所以我打破了第三种形式,因为如果客户使用卡,我知道他没有使用贝宝或比特币。我怎样才能解决这个问题,这样我就不会破坏第三种正常形态。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-23 12:54:52

在今天的SQL中,没有一种完全干净的方法可以做到这一点,因为SQL平台不支持断言。(在SQL标准中创建断言),但是您可以设计表以支持合理的约束,即使不支持断言。

将所有定期付款“向上”常见的属性推入"scheduled_payments“表。

代码语言:javascript
复制
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 nullcheck(...)约束保证每一行都有一个支付标识号和一个'c‘。外键约束保证"credit_cards“中的每一行将引用"scheduled_payments”中的'c‘行。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29811222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档