如何设计一个可以连接到多个不同表的本地化内容表(关系需要是通用的......)

发布于 2024-09-24 11:11:11 字数 2215 浏览 0 评论 0原文

我知道这并不完全规范化,但是将整个应用程序中的所有本地化数据放入几个表中将会对我有很大帮助。

我必须能够将一些通用表链接到 LocalizedContent 表,该表将包含连接到它的通用表的每个本地化键值对的不同行...我想你可能会说它将是一个 -一对多关系。

我发现的问题是我不确定如何最好地对此进行建模...我可以想到两种方法,但我不确定哪种方法最好:

我的第一个选择是:

AnExampleOfAGenericTable
------------
AnExampleOfAGenericTableID
...other non-localised data...

AnotherGenericTable
------------
AnotherGenericTableID
...other non-localised data...

LocalisedContent
----------------
LocalisedContentID
genericTablePKName
GenericTableID
LanguageID
field
content

在上面可以得到使用如下 SQL 查询为通用表输出本地化内容:

    SELECT AnExampleOfAGenericTableID, field, content 
    FROM AnExampleOfAGenericTable LEFT JOIN LocalisedContent 
    ON AnExampleOfAGenericTable.AnExampleOfAGenericTableID =        
    LocalisedContent.GenericTableID 
    WHERE genericTablePKName = 'AnExampleOfAGenericTableID'

或者:

    SELECT AnotherGenericTableID, field, content 
    FROM AnotherGenericTable LEFT JOIN LocalisedContent 
    ON AnotherGenericTable.AnotherGenericTableID = LocalisedContent.GenericTableID
    WHERE genericTablePKName = 'AnotherGenericTableID'

第二个选项似乎是:

AnExampleOfAGenericTable
------------
AnExampleOfAGenericTableID
...other non-localised data...
localisedGroupID

AnotherGenericTable
------------
AnotherGenericTableID
...other non-localised data...
localisedGroupID

LocalisedContent
----------------
LocalisedContentID
localisedGroupID
LanguageID
field
content

然后我可以使用如下 SQL 查询:

 SELECT AnExampleOfAGenericTableID, field, content 
 FROM AnExampleOfAGenericTable LEFT JOIN LocalisedContent 
 ON AnExampleOfAGenericTable.localisedGroupID = LocalisedContent.localisedGroupID;

或者:

 SELECT AnotherGenericTableID, field, content 
 FROM AnotherGenericTable LEFT JOIN LocalisedContent 
 ON AnotherGenericTable.localisedGroupID = LocalisedContent.localisedGroupID;

第二个选项对我来说似乎更简洁,但它确实需要我加入了两个FK,这似乎有点奇怪。它还需要大量额外的“localizedGroupID”列。

最终,我给出的两个例子都可能是错误的,而且我没有专业知识来知道最好的解决方案。 (在您指出这还没有完全规范化之前,我已经说过我不希望每个表都有数百个不同的本地化数据表......我确实希望对本地化进行一定程度的集中化,即使它会让我失去一点参考完整性。)

想法?

I know that this isn't exactly normalised, but bringing all of the localised data throughout my application into just a few tables will help me out a lot.

I have to be able to link some generic table to a LocalisedContent table which will contain different rows for each of the localised key-value pairs of the generic table joined to it... I guess you could say that it will be a one-to-many relationship.

The problem I've found is that I am not sure how to best model this... I can think of two ways and I am not sure which one is best:

My first option is:

AnExampleOfAGenericTable
------------
AnExampleOfAGenericTableID
...other non-localised data...

AnotherGenericTable
------------
AnotherGenericTableID
...other non-localised data...

LocalisedContent
----------------
LocalisedContentID
genericTablePKName
GenericTableID
LanguageID
field
content

In the above it would be possible to get out localised content for a generic table with an SQL query like:

    SELECT AnExampleOfAGenericTableID, field, content 
    FROM AnExampleOfAGenericTable LEFT JOIN LocalisedContent 
    ON AnExampleOfAGenericTable.AnExampleOfAGenericTableID =        
    LocalisedContent.GenericTableID 
    WHERE genericTablePKName = 'AnExampleOfAGenericTableID'

Or:

    SELECT AnotherGenericTableID, field, content 
    FROM AnotherGenericTable LEFT JOIN LocalisedContent 
    ON AnotherGenericTable.AnotherGenericTableID = LocalisedContent.GenericTableID
    WHERE genericTablePKName = 'AnotherGenericTableID'

The second option seems to be, something like:

AnExampleOfAGenericTable
------------
AnExampleOfAGenericTableID
...other non-localised data...
localisedGroupID

AnotherGenericTable
------------
AnotherGenericTableID
...other non-localised data...
localisedGroupID

LocalisedContent
----------------
LocalisedContentID
localisedGroupID
LanguageID
field
content

And then I could use an SQL query like:

 SELECT AnExampleOfAGenericTableID, field, content 
 FROM AnExampleOfAGenericTable LEFT JOIN LocalisedContent 
 ON AnExampleOfAGenericTable.localisedGroupID = LocalisedContent.localisedGroupID;

Or:

 SELECT AnotherGenericTableID, field, content 
 FROM AnotherGenericTable LEFT JOIN LocalisedContent 
 ON AnotherGenericTable.localisedGroupID = LocalisedContent.localisedGroupID;

The second option seems more concise to me, but it does require me to join two FKs which seems a little strange. It also requires a lot of extra 'localisedGroupID' columns.

Ultimately both of the examples I've given may be wrong and I don't have the expertise to know the best solution to this. (Before you point out that this isn't fully normalised, I've already said I don't want hundreds of different localised data tables for each of my tables... I do want some amount of centralisation to the localisation even if it will lose me a little referential integrity.)

Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

或十年 2024-10-01 11:11:11

您的模式让我想起了网络上提供的“泛化专业化关系建模”示例,其中有一个重要的区别。

您所说的 AnExampleOfAGenericTable 和 AnotherGenericTable 对应于 gen-spec 模式中的专用表,您所说的 LocalizedContent 对应于 gen-spec 模式中的通用表。

如果我理解正确,前两个表中的每个条目都将在 LocalizedContent 表中具有对应项,但 LocalizedContent 表中的条目将仅在其他两个表之一中具有对应项。这与 gen-spec 的模式完全相同,只是向后。

在gen-spec设计中,您在通用表中使用的所有专用表中使用相同的PK。然而,专用表中的PK也是通用表中的FK。当然,您只使用 gen 表中的自动编号功能。

gen-spec 本身并没有什么不规范的地方。

Your schema remninds me of the "generalization specialization relational modeling" examples available on the web, with one important difference.

What you're calling AnExampleOfAGenericTable and AnotherGenericTable correspond to the specialized tables in the gen-spec pattern, and what you're calling LocalisedContent corresponds to the generalized table in the gen-spec pattern.

If I've understood you right every entry in the first two tables is going to have a counterpart in the LocalisedContent table, but an entry in the LocalisedContent table is going to have a counterpart in only one of the other two tables. That's exactly the same pattern as gen-spec, only backwards.

In gen-spec design, you use the same PK in all the specialized tables that you use in the generalized table. However, the PK in a specialized table is also an FK to the generalized table. And, of course, you only use the autonumber feature in the gen table.

There's nothing unnormalized about gen-spec as such.

梦在深巷 2024-10-01 11:11:11

我们使用以下内容:

LocalizedContent:

Id   - identity
Key  - format 'TableName.ColumnName'
Value - localized value 
LanguageId - reference to the languageid
TableRowId - generic table row id

其中关键格式为“TableName.ColumnName”

用法:

SELECT IFNULL(lc1.Value, name) as Name,  
       IFNULL(lc2.Value, Description) as Description
From GenericTable t 
LEFT JOIN LocalizedContent lc1
   ON (lc1.TableRowId = t.Id AND Key = 'GenericTable.Name' And LanguageID = YourLangId)
LEFT JOIN LocalizedContent lc2 
   ON (lc2.TableRowId = t.Id AND Key = 'GenericTable.Description' And LanguageID = YourLangId)

GenericTable 为(Id、Name、Description)

We use the following:

LocalizedContent:

Id   - identity
Key  - format 'TableName.ColumnName'
Value - localized value 
LanguageId - reference to the languageid
TableRowId - generic table row id

Where key in format 'TableName.ColumnName'

usage:

SELECT IFNULL(lc1.Value, name) as Name,  
       IFNULL(lc2.Value, Description) as Description
From GenericTable t 
LEFT JOIN LocalizedContent lc1
   ON (lc1.TableRowId = t.Id AND Key = 'GenericTable.Name' And LanguageID = YourLangId)
LEFT JOIN LocalizedContent lc2 
   ON (lc2.TableRowId = t.Id AND Key = 'GenericTable.Description' And LanguageID = YourLangId)

GenericTable is (Id, Name, Description)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文