

 Amazon Redshift non supporterà più la creazione di nuovi Python UDFs a partire dalla Patch 198. Python esistente UDFs continuerà a funzionare fino al 30 giugno 2026. Per ulteriori informazioni, consulta il [post del blog](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/). 

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

# Esempi di query tra database
<a name="cross-database_example"></a>

Questo argomento contiene esempi su come utilizzare le query tra database. Le query tra database sono query che operano su più database all’interno di un singolo cluster Amazon Redshift.

Utilizzare gli esempi seguenti per saperne di più su come configurare una query tra database che faccia riferimento a un database Amazon Redshift. 

Per iniziare, creare i database `db1` e `db2` e gli utenti `user1` e `user2` nel cluster Amazon Redshift. Per ulteriori informazioni, consultare [CREATE DATABASE](r_CREATE_DATABASE.md) e [CREA UTENTE](r_CREATE_USER.md).

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

CREATE DATABASE db2;

CREATE USER user1 PASSWORD 'Redshift01';

CREATE USER user2 PASSWORD 'Redshift01';
```

Come `user1` in `db1`, creare una tabella, concedere i privilegi di accesso a `user2` e inserire i valori in `table1`. Per ulteriori informazioni, consultare [GRANT](r_GRANT.md) e [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);
```

Come `user2` in `db2`, eseguire una query tra database in `db2`utilizzando la notazione in tre parti. 

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

Ora concedi i privilegi di scrittura a `user2` e inserisci i valori in `table1` in `db1` come `user2`.

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

Come `user2` in `db2` esegui una query tra database in `db2` utilizzando la notazione in tre parti per inserire i dati in `table1` in `db1`.

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

Come `user2` in `db2`, creare uno schema esterno ed eseguire una query tra database in `db2` utilizzando la notazione dello schema esterno. 

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

Per creare viste diverse e concedere autorizzazioni a tali viste, come `user1` in `db1`, completare le seguenti operazioni. 

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

Come `user2` in `db2`, emettere la seguente query tra database utilizzando la notazione in tre parti per visualizzare la determinata vista.

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

Come `user2` in `db2`, emettere la seguente query tra database utilizzando la notazione dello schema esterno per eseguire una query sulla vista ad associazione tardiva.

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

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

Come `user2` in `db2`, emettere il seguente comando utilizzando le tabelle connesse in una singola query.

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

Nell'esempio seguente sono elencati tutti i database del cluster.

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

Nell'esempio seguente sono elencati tutti gli schemi Amazon Redshift di tutti i database del cluster.

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

Nell'esempio seguente sono elencate tutte le tabelle o le viste Amazon Redshift di tutti i database del cluster.

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

Nell'esempio seguente sono elencati tutti gli schemi esterni e di Amazon Redshift di tutti i database del cluster.

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

Nell'esempio seguente sono elencati tutte le tabelle esterne e di Amazon Redshift di tutti i database del cluster.

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