英文:
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?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论