While mostly working with non-relational databases I need to switch gears and use a relational database as the application that I need to build will run complex queries and the join operation between tables is needed.
Before starting to create the database itself I've had to think about the architecture and I've set up an UML for Database Design:
This is how the TransactionDEpositBreakdown
table may look:
id amount date reference_number batch_id payment_processor_id mid_id main_dep_id
1 100 2020-10-11 900 null 1 100 2
2 101 2020-10-11 900 null 1 100 2
3 102 2020-10-11 900 null 1 100 1
4 103 2020-10-11 350 null 1 100 1
5 104 2020-10-11 350 null 1 100 3
6 105 2020-10-11 600 null 1 100 4
7 106 2020-10-11 null 1000 2 201 null
8 107 2020-10-11 null 1001 2 201 null
9 108 2020-10-11 null 1002 2 201 null
10 109 2020-10-11 null 1003 2 201 null
reference_number
can be assigned to multiple transaction deposit breakdownsbatch_id
is assigned to only one transaction deposit breakdownThere is a use case where a TransactionDepositBreakdown
may have a reference number
or a batch id
, depending on the payment processor type (type 1 - reference number, type 2 - batch id). I'm not sure how to handle this case, but I'm thinking about the following options:
TransactionDepositBatch
and TransactionDepositReference
which will have the transaction_deposit_id
as a foreign key, batch_id
on the first table and reference_number
on the latter one:reference_number
and batch_id
columns in the TransactionDepositBreakdown
table and have at all times one of them null
depending on the payment processor type. Note: There might be a need of adding another column to the TransactionDepositBreakdown
table, such as card_type
, which will have a value assigned only when the payment processor type is 1.
Is the first option the correct way to handle this, by also taking into consideration the above note?
Also, any recommendations regarding the UML that I've built would be really useful.
These one-of relationships are difficult to model in relational databases. Different databases have different capabilities, so some may have extensions that can be applied to this problem (such as Postgres's support of table inheritance).
Your situation is rather simple, given just two options. Under those circumstances, I would go for the first option for one simple reason: it easily allows you to design the data model with declared foreign key relationships. The downside is that the you need space for both foreign keys, even if one of them is going to be NULL
.
You can also enforce that one or the other is set, but not both using a check constraint:
constraint chk_TransactionDepositBreakdown_reference_or_batch
check (reference_number is null or batch_id is null);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.