如何将一张表与另一张表关联起来以供将来记录

发布于 2024-08-29 10:14:24 字数 284 浏览 5 评论 0原文

我有一个游戏表,其中保存有关游戏的数据。 然后另一个表保存有关新闻的数据。

到目前为止,一切都很好。

首先,我考虑为 game_news 创建一个连接表,这样我就可以将新闻与游戏联系起来。

当游戏存在时,这种方式可以按预期工作。因此,每当我插入新闻时,我都可以使用连接表将其与游戏关联起来。

然而,有些情况下,有关于游戏的新闻,但游戏尚未发布且不存在。

所以我的问题是;有没有办法在创建游戏记录时将这些新闻与特定游戏相关联。

最好的方法是什么?有什么想法吗?

I have a games table which holds the data about a game.
Then another table which holds the data about news.

So far so good.

First I thought about creating a junction table for game_news so I could relate news to games.

This way works as intended when the game exists. So whenever I insert a news I can relate it to a game using the junction table.

However there are cases when there is news about game but the game isn't published and it doesn't exists.

So my question would be; is there a way to relate these news to a particular game when the game record is created.

What is the best way to do this? Any ideas?

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

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

发布评论

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

评论(5

神也荒唐 2024-09-05 10:14:25

连接表就是要走的路。如果一篇新闻文章涉及多个游戏,那么您就需要它。要处理尚不存在的游戏,只需为它们插入一行,包括您当前了解的所有信息(可能来自新闻文章),并有一个状态列将其标记为尚未发布。您可以将此游戏显示为尚未发布或谣言等。

设置表格如下:

Games
GameID           int          not null auto increment PK
GameStatus       char(1)      not null "P"=published, "N"=not released yet, "R"=game is only a rumor
GameReleaseDate  date         null
GameName         varchar(...) not null
GameDescription...
...

News
NewsID      int               not null auto increment PK
NewsTitle   varchar(...)      not null
...

GameNews
GameNewsID  int auto increment PK
GameID      int FK to Games.GameID
NewsID      int FK to News.NewsID

通过此设置,您可以拥有与单个新闻项目相关的多个游戏。只需插入所有正确的 GameNews 行即可将每个游戏链接到 News 行。

如果游戏尚未发布,您仍然可以通过创建状态为“N”或“R”(或类似内容)的 Games 行并将其链接到新闻,并使用 GameNews 表,就像处理已发布的游戏一样。您可以使用尽可能多的信息填充游戏中的所有字段,并在了解更多信息时更新它。最后,您将在游戏行中获得完整的游戏信息(游戏发布后),并且它将链接到所有新闻行,即使它只是新闻中的谣言。

为了让您了解我所说的内容,这里是“传闻”游戏的数据随着时间的推移会是什么样子的示例(这是一个简化的示例,每个新闻行没有多个游戏):

data as of 1/1/2010
    Games    GameID  GameStatus  GameReleaseDate  GameName
             1234    "R"         1/1/2012         "God of War 4"
    News     NewsID  NewsTitle
             543     "Future Of Games"
    GameNews GameNewsID  GameID  NewsID
             768         1234    543

data as of 4/1/2010
    Games    GameID  GameStatus  GameReleaseDate  GameName
             1234    "R"         1/1/2012         "God of War 4"
    News     NewsID  NewsTitle
             543     "Future Of Games"
             544     "Interview with John Hight"
    GameNews GameNewsID  GameID  NewsID
             768         1234    543
             769         1234    544

data as of 11/20/2010
    Games    GameID  GameStatus  GameReleaseDate  GameName
             1234    "N"         12/31/2011         "God of War IV"
    News     NewsID  NewsTitle
             543     "Future Of Games"
             544     "Interview with John Hight"
             545     "God of War Expected Next Year"
    GameNews GameNewsID  GameID  NewsID
             768         1234    543
             769         1234    544
             770         1234    545

data as of 8/15/2011
    Games    GameID  GameStatus  GameReleaseDate  GameName
             1234    "N"         12/01/2011         "God of War IV"
    News     NewsID  NewsTitle
             543     "Future Of Games"
             544     "Interview with John Hight"
             545     "God of War Expected Next Year"
             546     "Retailers Get Ready For New Games"
    GameNews GameNewsID  GameID  NewsID
             768         1234    543
             769         1234    544
             770         1234    545
             771         1234    546

data as of 1/1/2012
    Games    GameID  GameStatus  GameReleaseDate  GameName
             1234    "P"         12/01/2011         "God of War IV"
    News     NewsID  NewsTitle
             543     "Future Of Games"
             544     "Interview with John Hight"
             545     "God of War Expected Next Year"
             546     "Retailers Get Ready For New Games"
             547     "God of War IV Review"
    GameNews GameNewsID  GameID  NewsID
             768         1234    543
             769         1234    544
             770         1234    545
             771         1234    546
             772         1234    547

如果在 1 /1/2012 你要查看 News.NewsID=543,你会看到它链接到完整且经过审查的 Games.GameID=1234,即使 News.NewsID=543 文章是关于“传闻”即将推出的《God》版本战争的。所有这一切都是在没有对旧的 News 或 GameNews 行进行任何更改的情况下完成的。

The junction table is the way to go. If a news article is about more than one game, then you need it. To handle games that do not exist yet, just insert a row for them, include all the info you currently know about it (possibly from the news article) and have a status column that marks it as not released yet. You can display this game as not released yet or rumor, etc.

set the tables up something like this:

Games
GameID           int          not null auto increment PK
GameStatus       char(1)      not null "P"=published, "N"=not released yet, "R"=game is only a rumor
GameReleaseDate  date         null
GameName         varchar(...) not null
GameDescription...
...

News
NewsID      int               not null auto increment PK
NewsTitle   varchar(...)      not null
...

GameNews
GameNewsID  int auto increment PK
GameID      int FK to Games.GameID
NewsID      int FK to News.NewsID

With this setup you can have multiple games related to a single News item. Just insert all the proper GameNews rows to link each game to the News row.

If a game has not been published yet, you can still link it to news by creating the Games row with the status "N" or "R" (or something like that) and using the GameNews table just as you would for a published game. You could populate all the fields within Games with as much info as possible and update it as you find out more. in the end you would have complete game info in the Games row (after the game is published) and it would link to the all the News rows, even when it was just a rumor in the news.

To give you an idea about what I'm talking about, here is a sample of what the data for a "rumored" game would look like over time (this is a simplified example and without multiple Games per News rows):

data as of 1/1/2010
    Games    GameID  GameStatus  GameReleaseDate  GameName
             1234    "R"         1/1/2012         "God of War 4"
    News     NewsID  NewsTitle
             543     "Future Of Games"
    GameNews GameNewsID  GameID  NewsID
             768         1234    543

data as of 4/1/2010
    Games    GameID  GameStatus  GameReleaseDate  GameName
             1234    "R"         1/1/2012         "God of War 4"
    News     NewsID  NewsTitle
             543     "Future Of Games"
             544     "Interview with John Hight"
    GameNews GameNewsID  GameID  NewsID
             768         1234    543
             769         1234    544

data as of 11/20/2010
    Games    GameID  GameStatus  GameReleaseDate  GameName
             1234    "N"         12/31/2011         "God of War IV"
    News     NewsID  NewsTitle
             543     "Future Of Games"
             544     "Interview with John Hight"
             545     "God of War Expected Next Year"
    GameNews GameNewsID  GameID  NewsID
             768         1234    543
             769         1234    544
             770         1234    545

data as of 8/15/2011
    Games    GameID  GameStatus  GameReleaseDate  GameName
             1234    "N"         12/01/2011         "God of War IV"
    News     NewsID  NewsTitle
             543     "Future Of Games"
             544     "Interview with John Hight"
             545     "God of War Expected Next Year"
             546     "Retailers Get Ready For New Games"
    GameNews GameNewsID  GameID  NewsID
             768         1234    543
             769         1234    544
             770         1234    545
             771         1234    546

data as of 1/1/2012
    Games    GameID  GameStatus  GameReleaseDate  GameName
             1234    "P"         12/01/2011         "God of War IV"
    News     NewsID  NewsTitle
             543     "Future Of Games"
             544     "Interview with John Hight"
             545     "God of War Expected Next Year"
             546     "Retailers Get Ready For New Games"
             547     "God of War IV Review"
    GameNews GameNewsID  GameID  NewsID
             768         1234    543
             769         1234    544
             770         1234    545
             771         1234    546
             772         1234    547

if on 1/1/2012 you were to look at News.NewsID=543 you would see that it links to the complete and reviewed Games.GameID=1234, even though the News.NewsID=543 article is about a "rumored" upcoming version of God of War. And all this was done without making any changes to the old News or GameNews rows.

つ低調成傷 2024-09-05 10:14:25

你有 3 个选项 - 1 是在“游戏”表上设置一个标志来说明它是否已发布,如果游戏未发布,则仅显示名称(甚至不显示名称)。

另一种是在添加游戏项目并链接后编辑新闻项目。因为在添加游戏之前你无法知道唯一的 id 是什么。

第三个不推荐的选项是通过游戏名称而不是主键链接它们,因此新闻表中有一个名为 game_name 的列,它以这种方式链接表。但是,如果您拼写错误,该方法就会失败,而且远不如选项 1 或 2 好。

you have 3 options - 1 is to set a flag on the 'game' table to say whether it's released or not, and show only the name (or not even that) if the game is unreleased.

another is to edit the news item after you add the game item and link it then. because you cannot tell what a unique id is going to be before you add a game.

a 3rd UNRECOMMENDED option is to link them via game name instead of a primary key, so you have a column called game_name in the news table and it links the tables that way. however, that will fail if you mis-spell things and is nowhere near as good as options 1 or 2.

紫罗兰の梦幻 2024-09-05 10:14:25

最简单的答案是使用从新闻到游戏的外键。如果您要为尚不存在的游戏创建新闻,只需在游戏表中创建存根记录即可。如果您愿意,您可以将其标记为这样。添加游戏时,只需充实此存根记录即可。

The simplest answer is to have a foreign key from news to game. If you are creating news for a game that doesn't exist yet, just create a stub record in your games table. You can flag it as such if you want. When you add the game, just flesh out this stub record.

请别遗忘我 2024-09-05 10:14:25

将两个外键放在联结表中是一个好主意。外键引用存在的事物。强制执行这一点称为“引用完整性”。允许引用不存在的项目会导致混乱。

如果您收到有关尚不存在(在数据库中)的游戏的新闻文章,您基本上有两种选择:将游戏添加到游戏表,然后在连接表中添加对游戏的引用;或者,暂时忽略文章和游戏之间的关系。

Putting two foreign keys in a junction table is a good idea. Foreign keys reference things that exist. Enforcing this is called "referential integrity". Permitting references to non existent items is the road to chaos.

If you get a news article about a game that does not yet exist (in the database), you basically have two choices: add the game to the game table before adding the reference to the game in the junction table; alternatively, omit the relationship between the article and the game, for the time being.

℡寂寞咖啡 2024-09-05 10:14:25

“然而,有些情况下,有关于游戏的新闻,但游戏并未发布,也不存在。”

你应该理清“存在”的意义。如果某物不存在,那么就没有任何东西可以与之相关,就这样。

如果您的业务现实包含“现有且为公众所知”与“现有,但只是私下且不为公众所知”之间的差异,那么您的模型应该承认并反映这一现实。

将信息与任何“存在但不公开”的事物联系起来是完全可能的。不可能将信息与任何不存在的事物联系起来。

"However there are cases when there is news about game but the game isn't published and it doesn't exists."

You should get your thoughts straightened out on the meaning of "existance". If something does not exist, then there is nothing that can relate to it, period.

If your business reality encompasses a difference between "existing and being known to the public" and "existing, but only privately, and not being known to the public", then your models should acknowledge and refelct that reality.

It is perfectly possible to relate information to any thing that "exists, but not publicly". It is not possible to relate information to any thing that does not exist.

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