Database design 3rd Normal Form -


i have database many tables, 4 of these

  1. payment
  2. credit card
  3. paypal
  4. bitcoin

credit card attributes:

  • cardid (pk)
  • type
  • number
  • expiredate
  • ...

paypal attributes:

  • paypalid (pk)
  • account
  • ...

bitcoin attributes:

  • bitcoinid (pk)
  • ...

payment table attributes:

  • amount
  • ...
  • ...
  • cardid (fk)
  • paypalid (fk)
  • bitcoinid (fk)

a payment can paid either card/paypal/bitcoin breaking 3rd normal form because if client uses card know didnt use paypal or bitcoin. how can fix not breaking 3rd normal form.

there isn't completely clean way today in sql, because sql platforms don't support assertions. (create assertion in sql standards) can design tables support sensible constraints, without support assertions.

push attributes common scheduled payments "up" table "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 payment types.   unique (pmt_id, pmt_type) );  -- tables bitcoin , paypal not shown, they're similar -- table 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 credit cards. ); 

the primary key, not null, , check(...) constraints in "credit_cards" guarantee every row have payment id number , 'c'. foreign key constraint guarantees every row in "credit_cards" reference 'c' row in "scheduled_payments".


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -