[英]Create Relationship Between Two Columns Not Using PK SQL
I have two tables, one named Game and the other GameStats. 我有两个表,一个名为Game,另一个名为GameStats。
Game Design 游戏设计
GameId (PK)
GameGuid
MatchDate
TeamZeroScore
TeamOneScore
Active
GameStats Design GameStats设计
GameStatsId (PK)
GameGuid
TeamId
Name
Kills
Deaths
Assists
Active
A game can have many game stats. 一个游戏可以具有许多游戏统计信息。 So my question is is there any way to set up a relationship between Game and Gamestats using the GameGuid field even though they are not primary keys?
所以我的问题是,有没有办法使用GameGuid字段在Game和Gamestats之间建立关系,即使它们不是主键?
You can, but I wouldn't recommend it. 您可以,但我不建议您这样做。 In general, you can set up a foreign key relationship to another table using either a primary key or unique key (where key can actually consist of multiple columns).
通常,您可以使用主键或唯一键(其中键实际上可以由多列组成)来建立与另一个表的外键关系。
However, you don't want to store redundant data in tables. 但是,您不想在表中存储冗余数据。 So, if
GameId
is the primary key for Game
, then the second table should be: 因此,如果
GameId
是Game
的主键,那么第二张表应该是:
GameStatsId (PK)
GameId references Game(GameId)
TeamId
Name
Kills
Deaths
Assists
Active
You can look up the GameGuid
by doing a join. 您可以通过加入来查找
GameGuid
。
On the other hand, if you want to use GameGuid
as the primary key, then use that and remove GameId
. 另一方面,如果要使用
GameGuid
作为主键,请使用该键并删除GameId
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.