简体   繁体   English

MySQL,如何重组可选的多个外键

[英]MySQL, how to restructure optional multiple foreign keys

For this example, I'm trying to build a system that will allow output from multiple sources, but these sources are not yet built. 对于此示例,我正在尝试构建一个系统,该系统将允许来自多个来源的输出,但是尚未构建这些来源。 The output "module" will be one component, and each source will be its own component to be built and expanded upon later. 输出“模块”将是一个组件,每个源将是其自己的组件,以供以后构建和扩展。

Here's an example I designed in MySQLWorkbench: 这是我在MySQLWorkbench中设计的一个示例: 桌子布置

The goal is to make my output module display data from the output table while being easily expanded upon later as more sources are built. 目标是使我的输出模块显示输出表中的数据,同时在以后构建更多源时轻松对其进行扩展。 I also want to minimize schema updates when adding new sources. 我还想在添加新源时最小化架构更新。 Currently, I will have to add a new table per source, then add a foreign key to the output table. 当前,我将必须为每个源添加一个新表,然后将一个外键添加到输出表。

Is there a better way to do this? 有一个更好的方法吗? I don't know how I feel about these NULL-able foreign keys because the JOIN query will contains IFNULL's and will get unruly quickly. 我不知道我对这些可为NULL的外键的感觉如何,因为JOIN查询将包含IFNULL并将很快变得不规则。

Thoughts? 思考?

EDIT 1: Clarification 编辑1:澄清

I will be displaying a grid using data in the output table. 我将使用输出表中的数据显示网格。 The output table will contain general data for all items in the grid and will basically act as an aggregator for the output_source_X tables: 输出表将包含网格中所有项目的常规数据,并且基本上将充当output_source_X表的聚合器:

output(id, when_added, is_approved, when_approved, sort_order, ...)

The output_source_X tables will contain additional data specific to a source. output_source_X表将包含特定于源的其他数据。 For example, let's say one of the output source tables is for Facebook posts, so this table will contain columns specific to the Facebook API: 例如,假设输出源表之一用于Facebook帖子,因此该表将包含特定于Facebook API的列:

output_source_facebook(id, from, message, place, updated_time, ...)

Another may be Twitter, so the columns are specific for Twitter: 另一个可能是Twitter,因此这些列专门针对Twitter:

output_source_twitter(id, coordinates, favorited, truncated, text, ...)

A third output source table could be Instagram, so the output_source_instagram table will contain columns specific to Instagram. 第三个输出源表可以是Instagram,因此output_source_instagram表将包含特定于Instagram的列。

There will be a one-to-one foreign key relationship with the output table and ONLY ONE of the output_source_X tables, depending on if the output item is a Facebook, Twitter, Instagram, etc... post, hence the NULL-able foreign keys. 与输出表和output_source_X表中只有一个一对一的外键关系,这取决于输出项是Facebook,Twitter,Instagram还是其他类,因此,可以为NULL的外键键。

output table
------------
foreign key (source_id_facebook) references output_source_facebook(id)
foreign key (source_id_twitter) references output_source_twitter(id)
foreign key (source_id_instagram) references output_source_instagram(id)

I guess my concern is that this is not as modular as I'd like it to be because I'd like to add other sources as well without having to update the schema much. 我想我担心的是这不是我想要的模块化,因为我也想添加其他源,而不必太多更新架构。 Currently, this requires me to join output_source_X on the output table using whatever foreign key is not null. 当前,这要求我使用任何不为null的外键在输出表上加入output_source_X。

This design in almost certainly bad in a few ways. 这种设计在某些方面几乎可以肯定是不好的。

It's not that clear what your design is representing but a straightforward one would be something like: 目前尚不清楚您的设计代表什么,但是简单的设计将是这样的:

// source [id] has ...
source(id,message,...)
// output [id] is approved when [approved]=1 and ...
output(id,approved,...)
// output [output_id] has [source_id] as a source
output_source(output_id,source_id)
foreign key (source_id) references source(id)
foreign key (source_id) references source(id)

Maybe you have different subtypes of outputs and/or sources? 也许您有不同的输出和/或来源子类型? Based on sources and/or outputs? 基于来源和/或输出? Maybe each source is restricted to feeding particular outputs? 也许每个来源都只能提供特定的输出? Are "outputs" and "sources" actually kinds of outputs and sources, and this is info not on how outputs are sourced but info on what kinds of output-source pairings are permittted? 是“产出”和“源”实际上是输出和来源,这是信息不输出的方式采购,但对信息什么样的输出源配对的是permittted?

Please give us statements parameterized by column names for the basic statements you want to make about your application. 请为我们提供您要针对应用程序编写的基本语句的参数,这些语句由列名参数化。 Ie for the application relationships you are interested in. (Eg like the code comments above.) (You could do it for the diagrammed design but probably that would be overly complicated and not really reflecting what you are trying to model.) 即针对您感兴趣的应用程序关系。(例如,如上面的代码注释。)(您可以为图解设计完成此操作,但可能会过于复杂且不能真正反映您要建模的内容。)

Re your EDIT: 重新编辑:

There will be a one-to-one foreign key relationship with the output table and ONLY ONE of the output_source_X tables, depending on if the output item is a Facebook, Twitter, Instagram, etc... post, hence the NULL-able foreign keys. 与输出表和output_source_X表中只有一个一对一的外键关系,这取决于输出项是Facebook,Twitter,Instagram还是其他类,因此,可以为NULL的外键键。

You have a case of multiple disjoint subtypes of a supertype. 您有一个超类型的多个不相交子类型的情况。

Your situation is a lot like that of this question except that where they have a subtype discriminator/tag column indicating which subtype table you have a set of columns where the non-empty one indicates which subtype table. 您的情况与该问题非常相似,不同之处在于它们具有一个子类型区分符/标签列,该列指示哪个子类型表,您具有一组列,非空列指示哪个子类型表。 See Erwin Smout's & my answers. 请参阅Erwin Smout和我的答案。 Also this answer . 这个答案

Please give us statements parameterized by column names for the basic statements you want to make about your application 请为您提供关于您的应用程序的基本陈述的列名称参数化的陈述

and you will find straightforward statements (as above). 并且您会发现简单明了的语句(如上所述)。 And if you give the statements for your current design you will find them complex. 如果您给出当前设计的陈述,您会发现它们很复杂。 See also this. 另请参阅。

I guess my concern is that this is not as modular as I'd like it to be because I'd like to add other sources as well without having to update the schema much. 我想我担心的是这不是我想要的模块化,因为我也想添加其他源,而不必太多更新架构。

Your structure is not reducing schema changes compared to proper subtype designs. 与正确的子类型设计相比,您的结构并未减少架构更改。

Anyway, DDL is there for that. 无论如何,DDL在那里。 You can genericize subtypes to avoid DDL only by loss of the DBMS managing integrity. 您可以仅通过丢失DBMS管理完整性来通用化子类型以避免DDL。 That would only be relevant or reasonable based on evaluating DDL vs DML performance tradeoffs. 基于评估DDL与DML性能的权衡,这才是相关或合理的。 Search re (usually, anti-pattern) EAV. 搜索重新(通常是反模式)EAV。

( Only after you shown that creating & deleting new tables is infeasible and the corresponding horrible integrity-&-concurrency-challenged mega-joining table-and-metadata-encoded-in-table EAV information-equivalent design is feasible should you consider using EAV.) 当您表明创建和删除新表是不可行的,并且对应的可怕的完整性和并发性挑战的巨型联接表和元数据编码表中的EAV信息等效设计才是可行的,才应考虑使用EAV 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM