简体   繁体   English

在不使用PK SQL的两列之间创建关系

[英]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: 因此,如果GameIdGame的主键,那么第二张表应该是:

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.

相关问题 SQL:有没有办法过滤两列之间的关系? - SQL: Is there a way to filter on the relationship between two columns? SQL基于两个原始列之间的一对一或一对多关系创建新列 - SQL create new column based on the one-to-one or one-to-many relationship between two original columns 是否有SQL表约束来确保两列之间存在关系? - Is there a SQL table constraint to ensure that a relationship exists between two columns? 如何使用Entity Framework在带有修订的两个表之间创建关系 - How to create relationship between two tables with revisions using Entity Framework 有没有办法使用 SQL 定义使用 2 个非唯一列的 2 个表之间的关系 - Is there a way to define the relationship between 2 tables using 2 non-unique columns columns using SQL 在 Postgres 12 中,如何使用动态 SQL 将 PK 列更改为 IDENTITY? - In Postgres 12, how to change PK columns into IDENTITY using dynamic SQL? 在不使用唯一列或键列的情况下,在两个SQL Server表(父列到子列)之间定义关系 - Define a relationship between two SQL Server tables (parent to child column) without the use of unique or key columns 选择在两列之间只有一种唯一关系的行 DBMS SQL - Selecting rows that only have one unique relationship between two columns DBMS SQL 如何命名SQL FOREIGN KEY约束,将两个表列创建为关系表 - How to name SQL FOREIGN KEY Constraint on CREATE two tables columns into a relationship TABLE sql中两个表之间的多对多关系? - Many to many relationship between two tables in sql?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM