使用跨資料庫查詢的範例 - Amazon Redshift

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用跨資料庫查詢的範例

使用下列範例可協助您了解如何設定參考 Amazon Redshift 資料庫的跨資料庫查詢。

若要開始,請在您的 Amazon Redshift 叢集中建立 db1db2 資料庫及使用者 user1user2。如需詳細資訊,請參閱 CREATE DATABASECREATE USER

--As user1 on db1 CREATE DATABASE db1; CREATE DATABASE db2; CREATE USER user1 PASSWORD 'Redshift01'; CREATE USER user2 PASSWORD 'Redshift01';

作為 db1 上的 user1,建立資料表、授予 user2 存取權限,以及將值插入 table1。如需詳細資訊,請參閱 GRANTINSERT

--As user1 on db1 CREATE TABLE table1 (c1 int, c2 int, c3 int); GRANT SELECT ON table1 TO user2; INSERT INTO table1 VALUES (1,2,3),(4,5,6),(7,8,9);

作為 db2 上的 user2,使用三部分標記法在 db2 中執行跨資料庫查詢。

--As user2 on db2 SELECT * from db1.public.table1 ORDER BY c1; c1 | c2 | c3 ---+-----+---- 1 | 2 | 3 4 | 5 | 6 7 | 8 | 9 (3 rows)

作為 db2 上的 user2,建立外部結構描述,並使用外部結構描述標記法在 db2 中執行跨資料庫查詢。

--As user2 on db2 CREATE EXTERNAL SCHEMA db1_public_sch FROM REDSHIFT DATABASE 'db1' SCHEMA 'public'; SELECT * FROM db1_public_sch.table1 ORDER BY c1; c1 | c2 | c3 ----+----+---- 1 | 2 | 3 4 | 5 | 6 7 | 8 | 9 (3 rows)

若要建立不同的檢視並授予這些檢視的許可,作為 db1 上的 user1,請執行以下操作。

--As user1 on db1 CREATE VIEW regular_view AS SELECT c1 FROM table1; GRANT SELECT ON regular_view TO user2; CREATE MATERIALIZED VIEW mat_view AS SELECT c2 FROM table1; GRANT SELECT ON mat_view TO user2; CREATE VIEW late_bind_view AS SELECT c3 FROM public.table1 WITH NO SCHEMA BINDING; GRANT SELECT ON late_bind_view TO user2;

作為 db2 上的 user2,請使用三部分標記法執行下列跨資料庫查詢,以檢視特定檢視。

--As user2 on db2 SELECT * FROM db1.public.regular_view; c1 ---- 1 4 7 (3 rows) SELECT * FROM db1.public.mat_view; c2 ---- 8 5 2 (3 rows) SELECT * FROM db1.public.late_bind_view; c3 ---- 3 6 9 (3 rows)

作為 db2 上的 user2,請使用外部結構描述標記法執行下列跨資料庫查詢,以查詢近期繫結檢視。

--As user2 on db2 SELECT * FROM db1_public_sch.late_bind_view; c3 ---- 3 6 9 (3 rows)

作為 db2 上的 user2,請在單一查詢中使用連線的資料表執行以下命令。

--As user2 on db2 CREATE TABLE table1 (a int, b int, c int); INSERT INTO table1 VALUES (1,2,3), (4,5,6), (7,8,9); SELECT a AS col_1, (db1.public.table1.c2 + b) AS sum_col2, (db1.public.table1.c3 + c) AS sum_col3 FROM db1.public.table1, table1 WHERE db1.public.table1.c1 = a; col_1 | sum_col2 | sum_col3 ------+----------+---------- 1 | 4 | 6 4 | 10 | 12 7 | 16 | 18 (3 rows)

下列範例會列出叢集上的所有資料庫。

select database_name, database_owner, database_type from svv_redshift_databases where database_name in ('db1', 'db2'); database_name | database_owner | database_type ---------------+----------------+--------------- db1 | 100 | local db2 | 100 | local (2 rows)

下列範例會列出叢集上所有資料庫的所有 Amazon Redshift 結構描述。

select database_name, schema_name, schema_owner, schema_type from svv_redshift_schemas where database_name in ('db1', 'db2'); database_name | schema_name | schema_owner | schema_type ---------------+--------------------+--------------+------------- db1 | pg_catalog | 1 | local db1 | public | 1 | local db1 | information_schema | 1 | local db2 | pg_catalog | 1 | local db2 | public | 1 | local db2 | information_schema | 1 | local (6 rows)

下列範例會列出叢集上所有資料庫的所有 Amazon Redshift 資料表或檢視。

select database_name, schema_name, table_name, table_type from svv_redshift_tables where database_name in ('db1', 'db2') and schema_name in ('public'); database_name | schema_name | table_name | table_type ---------------+-------------+---------------------+------------ db1 | public | late_bind_view | VIEW db1 | public | mat_view | VIEW db1 | public | mv_tbl__mat_view__0 | TABLE db1 | public | regular_view | VIEW db1 | public | table1 | TABLE db2 | public | table2 | TABLE (6 rows)

下列範例會列出叢集上所有資料庫的所有 Amazon Redshift 和外部結構描述。

select database_name, schema_name, schema_owner, schema_type from svv_all_schemas where database_name in ('db1', 'db2') ; database_name | schema_name | schema_owner | schema_type ---------------+--------------------+--------------+------------- db1 | pg_catalog | 1 | local db1 | public | 1 | local db1 | information_schema | 1 | local db2 | pg_catalog | 1 | local db2 | public | 1 | local db2 | information_schema | 1 | local db2 | db1_public_sch | 1 | external (7 rows)

下列範例會列出叢集上所有資料庫的所有 Amazon Redshift 和外部資料表。

select database_name, schema_name, table_name, table_type from svv_all_tables where database_name in ('db1', 'db2') and schema_name in ('public'); database_name | schema_name | table_name | table_type ---------------+-------------+---------------------+------------ db1 | public | regular_view | VIEW db1 | public | mv_tbl__mat_view__0 | TABLE db1 | public | mat_view | VIEW db1 | public | late_bind_view | VIEW db1 | public | table1 | TABLE db2 | public | table2 | TABLE (6 rows)