[英]How to handle null columns in a Relational Database Design
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:在开始创建数据库本身之前,我必须考虑架构,并且我已经为数据库设计设置了一个 UML:
This is how the TransactionDEpositBreakdown
table may look:这就是
TransactionDEpositBreakdown
表的外观:
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 breakdownsreference_number
可以分配给多个交易存款明细batch_id
is assigned to only one transaction deposit breakdownbatch_id
只分配给一个交易存款明细There 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).有一个用例,其中
TransactionDepositBreakdown
可能具有reference number
或batch id
,具体取决于支付处理器类型(类型 1 - 参考号,类型 2 - 批次 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:TransactionDepositBatch
和TransactionDepositReference
,它们将transaction_deposit_id
作为外键,第一个表上的batch_id
和后一个表上的reference_number
: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.TransactionDepositBreakdown
表中的reference_number
和batch_id
列,并根据支付处理器类型始终拥有其中一个null
。 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.注意:可能需要在
TransactionDepositBreakdown
表中添加另一列,例如card_type
,仅当支付处理器类型为 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.此外,任何关于我构建的 UML 的建议都会非常有用。
These one-of relationships are difficult to model in relational databases.这些关系之一很难在关系数据库中使用 model。 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).
不同的数据库有不同的能力,所以有些可能有可以应用到这个问题的扩展(比如 Postgres 对表继承的支持)。
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.
在这种情况下,我将 go 作为第一个选项,原因很简单:它很容易让您设计具有声明外键关系的数据 model。 The downside is that the you need space for both foreign keys, even if one of them is going to be
NULL
.缺点是两个外键都需要空间,即使其中一个是
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.