Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

sql - Creating a table that joins two databases

I am interested in creating a unified table that is the result of a join between two tables that are located in separate databases in my Snowflake environment. I found this resource which was quite helpful: https://resources.snowflake.com/youtube-all-videos/query-multiple-databases-within-snowflake

In the example in the video, the instructor says that the following will create a unified table in the CUSTOMER_SEGMENT database:

CREATE OR REPLACE TABLE UNIFIED_DATA AS (
   SELECT SEGMENT.CUST_JEY, MKTSEGMENT, COMMENT, ADDRESS, NAME
   FROM CUSTOMER_SEGMENT.PUBLIC.SEGMENT
   JOIN CUSTOMERS_DATA.PUBLIC.DATA
   ON DATA.CUST_KEY = SEGMENT.CUST_KEY)

I was confused by how Snowflake knows to store the table in the CUSTOMER_SEGMENT database. Is it because the FROM statement selects from that database?

question from:https://stackoverflow.com/questions/65946039/creating-a-table-that-joins-two-databases

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Notice that in the UI shown at the video the CUSTOMER_SEGMENT was selected as the database and PUBLIC as the schema.

enter image description here

This is the equivalent of running:

USE DATABASE CUSTOMER_SEGMENT;
USE SCHEMA PUBLIC;

When you run it Snowflake will auto add CUSTOMER_SEGMENT.PUBLIC to any table reference unless specified otherwise.

So :

CREATE OR REPLACE TABLE UNIFIED_DATA AS (
   SELECT SEGMENT.CUST_JEY, MKTSEGMENT, COMMENT, ADDRESS, NAME
   FROM CUSTOMER_SEGMENT.PUBLIC.SEGMENT
   JOIN CUSTOMERS_DATA.PUBLIC.DATA
   ON DATA.CUST_KEY = SEGMENT.CUST_KEY)

Is equivalent to:

CREATE OR REPLACE TABLE CUSTOMER_SEGMENT.PUBLIC.UNIFIED_DATA AS (
   SELECT SEGMENT.CUST_JEY, MKTSEGMENT, COMMENT, ADDRESS, NAME
   FROM CUSTOMER_SEGMENT.PUBLIC.SEGMENT
   JOIN CUSTOMERS_DATA.PUBLIC.DATA
   ON DATA.CUST_KEY = SEGMENT.CUST_KEY)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...