

 从补丁 198 开始，Amazon Redshift 将不再支持创建新的 Python UDF。现有的 Python UDF 将继续正常运行至 2026 年 6 月 30 日。有关更多信息，请参阅[博客文章](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)。

# 跨数据库查询示例
<a name="cross-database_example"></a>

本主题包含如何使用跨数据库查询的示例。跨数据库查询是对单个 Amazon Redshift 集群中的多个数据库进行操作的查询。

使用以下示例帮助了解如何设置引用 Amazon Redshift 数据库的跨数据库查询。

首先，请在您的 Amazon Redshift 集群中创建数据库 `db1` 和 `db2` 以及用户 `user1` 和 `user2`。有关更多信息，请参阅[CREATE DATABASE](r_CREATE_DATABASE.md)和[CREATE USER](r_CREATE_USER.md)。

```
--As user1 on db1
CREATE DATABASE db1;

CREATE DATABASE db2;

CREATE USER user1 PASSWORD 'Redshift01';

CREATE USER user2 PASSWORD 'Redshift01';
```

作为 `db1` 上的 `user1`，授予对 `user2` 的访问权限，然后将值插入 `table1` 中。有关更多信息，请参阅[GRANT](r_GRANT.md)和[INSERT](r_INSERT_30.md)。

```
--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)
```

现在向 `user2` 授予写入权限，并以 `user2` 身份将值插入到 `db1` 中的 `table1`。

```
--As user1 on db1
GRANT INSERT ON table1 TO user2;
```

作为 `db2` 上的 `user2`，使用三部分表示法在 `db2` 中运行跨数据库查询，以便将数据插入到 `db1` 中的 `table1`。

```
--As user2 on db2
INSERT INTO db1.public.table1 VALUES (10,11,12);
SELECT * from db1.public.table1 ORDER BY c1;

c1  | c2   | c3
----+------+----
1   |  2   | 3
4   |  5   | 6
7   |  8   | 9
10  |  11  | 12
(4 rows)
```

作为 `db2` 上的 `user2`，创建外部 schema 并在 `db2` 中使用外部 schema 表示法运行跨数据库查询。

```
--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
10  | 11 | 12
(4 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
10
(4 rows)

SELECT * FROM db1.public.mat_view;

c2
----
2
5
8
11
(4 rows)

SELECT * FROM db1.public.late_bind_view;

c3
----
3
6 
9
12
(4 rows)
```

作为 `db2` 上的 `user2`，使用外部 schema 表示法运行以下跨数据库查询以查询后期绑定视图。

```
--As user2 on db2
SELECT * FROM db1_public_sch.late_bind_view;

c3
----
3
6
9
12
(4 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 schema。

```
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 和外部 schema。

```
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)
```