在BigQuery中进行表连接操作

huangapple go评论79阅读模式
英文:

Table joins in BigQuery

问题

你好!根据你的描述,你想知道是否有一种类似于MySQL中使用外键约束的方法来连接两个表格。如果没有的话,是否有替代方法。

英文:

Do you know if there's a way to join two tables by, for
example, using a foreign key constraint like in MySQL (I don't seem to
find anything about this) ? If not, is there a replacement ?

Thanks!

答案1

得分: 1

我将你的问题翻译如下:

> 有没有办法限制tableX中可以使用的值只能是存在于tableY中的ID?例如,是否可以像在MySQL中使用外键约束一样实现?

BigQuery没有直接提供这样的机制。但是你可以很容易地自己实现。

例如,假设你需要向tableX插入一些数据,但你希望只有那些新数据中的id存在于tableY中的行才会被插入。

所以,你可以通过以下查询来"强制"实现:

<!-- language-all: lang-sql -->

#standardSQL
SELECT n.*
FROM newData AS n
JOIN tableY AS y
ON n.id = y.id

... 你可以将这个查询作为目标运行,只有需要的行才会被插入。

希望你有了一个思路。

你还可以查看现有的相关功能请求:
https://issuetracker.google.com/issues/35906045
https://issuetracker.google.com/issues/35906043

英文:

I interpret your question as below -

> Is there a way to limit the values that can be used on the tableX to only the IDs that exist on the tableY? For example via using a foreign key constraint like in MySQL!

BigQuery does not provide any direct mechanism for this to happen.
But you can easily achieve this by yourself.

For example, assume you need to insert some data to tableX, but you want to make sure that only those rows will be inserted where id in that new data is in tableY

So, you can "enforce" this via below query

<!-- language-all: lang-sql -->

#standardSQL
SELECT n.*
FROM newData AS n
JOIN tableY AS y
ON n.id = y.id

... you can run this query with tableX as destination and ONLY needed rows will be inserted

Hope you got an idea

Also you can check existing related feature requests -
https://issuetracker.google.com/issues/35906045
https://issuetracker.google.com/issues/35906043

答案2

得分: 0

由于您提出了两个问题(Stack Overflow建议每个问题提出一个问题),我将回答其中一个:

> 另外,您是否知道是否有一种方法可以通过使用外键来连接两个表

在BigQuery中,您可以通过任何键来连接表,甚至可以通过即时定义的键来连接表(当您需要连接来自不同数据集的两个表时,这非常有用,这些表选择以不同的方式编码相同的值)。

为什么您需要外键来进行这些连接呢?

英文:

Since you asked two questions (Stack Overflow suggests asking 1 question per question), I'll answer one:

> Also, do you know if there's a way to join two tables by, for example,
> using a foreign key

In BigQuery you can join tables by any key - even by keys defined on the fly (this is pretty useful when you need to join two tables from different datasets that choose to encode identical values in different ways).

Why would you need a foreign key to do these joins?

huangapple
  • 本文由 发表于 2017年3月24日 01:29:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/42983018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定