10.5 Query Remote and Federated Data
Provides examples of how to use Select AI to generate SQL query for remote and federated data.
Topics:
- Example: Use Select AI with Database Links to Query Another Autonomous AI Database
This example shows how to set up a Database Link from Autonomous AI Database to the source database and use Select AI to generate SQL from natural language prompts. Select AI uses the metadata from the source database to generate SQL. - Example: Use Select AI with Database Links to Query Non-Oracle Database
This example shows how Autonomous AI Database works as an Live AI Hub, formerly called AI Proxy Database, and uses Select AI to generate federated SQL that joins local Oracle data with remote PostgreSQL data. Autonomous AI Database support for Oracle-managed heterogeneous connectivity makes it easy to create database links to non-Oracle databases. The PostgreSQL database is the official, authoritative source for the data. - Example: Use Select AI with Cloud Links to Query Another Autonomous AI Database
This example shows how to use Cloud Links to access data stored in another Autonomous AI Database and query it using Select AI. - Example: Use External Table over Table Hyperlink with Select AI
This example shows how an Autonomous AI Database (consumer database) acts as an Live AI Hub, to query remote data hosted in another Autonomous AI Database (provider database) using an External Table over a Table Hyperlink. - Example: Use Federated Table with Select AI
This example shows how an Autonomous AI Database (consumer) uses Select AI to query a Federated Table that automatically connects to a remote Autonomous AI Database (provider). The (consumer database) acts as an Live AI Hub (formerly called AI Proxy Database) to query remote data hosted in another Autonomous AI Database (provider database).
Parent topic: Examples Using Select AI
10.5.1 Example: Use Select AI with Database Links to Query Another Autonomous AI Database
This example shows how to set up a Database Link from Autonomous AI Database to the source database and use Select AI to generate SQL from natural language prompts. Select AI uses the metadata from the source database to generate SQL.
-
Download your cloud wallet credentials and upload to an Object Storage bucket:
-
Download the wallet (
cwallet.sso) from your source database through OCI Console or Cloud Shell. See Download Database Connection Information for more details. -
Upload the wallet file to an Object Storage bucket. See Creating an Object Storage Bucket for more details.
-
This example shows how to set up a Database Link (DB Link) in an Autonomous AI Database to securely connect with another Autonomous AI Database. However, you can create DB Links to non-Autonomous AI Databases and third-party databases. Database links enable Select AI to query across remote data sets without replicating data through a wallet, credentials, and linked views.
You first create a credential to store your username and password to
authenticate the source database. Create a directory to store the wallet files used for
authentication when you are connecting to another Autonomous AI Database. Download the source database wallet credentials using
GET_OBJECT procedure. Create a secure Database Link from Autonomous AI Database to the source Autonomous
Database. You then create views on the remote tables. Create an AI profile with
object_list attribute specifying the views as JSON objects and include
the view name directly in object_list because Select AI profiles do not
recognize database link syntax. Finally, issue any NL2SQL Select AI actions such as
runsql, showsql, explainsql,
narrate, or chat. This example uses
showsql.
--Create Cloud Credential (run in Autonomous AI Database)
BEGIN
DBMS_CLOUD.DROP_CREDENTIAL(credential_name => 'DB_LINK_CRED');
EXCEPTION WHEN OTHERS THEN NULL;
END;
/
BEGIN
DBMS_CLOUD.CREATE_CREDENTIAL(
credential_name => 'DB_LINK_CRED',
username => 'DB_USER', -- Username on source database
password => '<password>' -- Password for source database
);
END;
/
--Create Directory (run in Autonomous AI Database)
CREATE DIRECTORY dblink_wallet_dir AS 'DATA_PUMP_DIR';
--Prepare and Upload Source Database Wallet in Object Storage bucket and run in Autonomous AI Database:
BEGIN
DBMS_CLOUD.GET_OBJECT(
credential_name => 'DB_LINK_CRED',
object_uri => 'https://objectstorage.ca-toronto-1.oraclecloud.com/n/namespace-string/b/bucketname/o/data_folder/cwallet.sso/cwallet.sso',
directory_name => 'DBLINK_WALLET_DIR'
);
END;
/
--Create Database Link (Drop dblink if it exists) to Source Database (run in Autonomous AI Database)
BEGIN
DBMS_CLOUD_ADMIN.DROP_DATABASE_LINK(db_link_name => 'MY_DATA_LINK');
EXCEPTION WHEN OTHERS THEN NULL;
END;
/
BEGIN
DBMS_CLOUD_ADMIN.CREATE_DATABASE_LINK(
db_link_name => 'MY_DATA_LINK',
hostname => 'adb.<region>-1.oraclecloud.com', -- Source database hostname
port => '1522', -- Source database port
service_name => 'your_service_name.adb.oraclecloud.com', -- Source database service
credential_name => 'DB_LINK_CRED',
directory_name => 'DBLINK_WALLET_DIR'
);
END;
/
--Create Views (run in Autonomous AI Database)
CREATE VIEW customer_view AS SELECT * FROM customer@MY_DATA_LINK;
CREATE VIEW streams_view AS SELECT * FROM streams@MY_DATA_LINK;
--Create an AI Profile (run in Autonomous AI Database)
BEGIN
DBMS_CLOUD_AI.CREATE_PROFILE(
profile_name => 'MY_AI_PROFILE',
attributes => JSON_OBJECT(
'provider' => 'openai',
'credential_name' => 'OPENAI_CRED',
'object_list' => JSON_ARRAY(
JSON_OBJECT('owner' => 'SELECT_AI_USER', 'name' => 'CUSTOMER_VIEW'),
JSON_OBJECT('owner' => 'SELECT_AI_USER', 'name' => 'STREAMS_VIEW')
)
)
);
END;
/
--Showsql test:
SELECT AI SHOWSQL how many customers are there;
--Run on Source Database
Copy the generated SQL, remove @MY_DATA_LINK and run the
query on your source database to verify.
Parent topic: Query Remote and Federated Data
10.5.2 Example: Use Select AI with Database Links to Query Non-Oracle Database
This example shows how Autonomous AI Database works as an Live AI Hub, formerly called AI Proxy Database, and uses Select AI to generate federated SQL that joins local Oracle data with remote PostgreSQL data. Autonomous AI Database support for Oracle-managed heterogeneous connectivity makes it easy to create database links to non-Oracle databases. The PostgreSQL database is the official, authoritative source for the data.
-
Use a PostgreSQL user that has read access to the target schema or table
-
Confirm network access from Autonomous AI Database to the PostgreSQL endpoint
Use Case Scenario
-
Autonomous AI Database contains
CUSTOMER_REVENUEtable. - PostgreSQL contains
support_ticket_metricstable. - Select AI generates SQL from a natural language prompt that joins both tables.
Parent topic: Query Remote and Federated Data
10.5.3 Example: Use Select AI with Cloud Links to Query Another Autonomous AI Database
This example shows how to use Cloud Links to access data stored in another Autonomous AI Database and query it using Select AI.
Cloud Links provide read-only access to registered tables and views across databases within a tenancy, compartment, or region.
This example walks through the complete flow required to make data available through Cloud Links and use it with Select AI.
Source Database: Oracle Autonomous AI Database where your data (tables or views) that you want to share resides.
Target Database (receiving side) acts as the Live AI Hub, formerly called AI Proxy Database: Oracle Autonomous AI Database where you configure Select AI and issue natural language queries.
Cloud Links provide a secure, read-only mechanism for sharing data across Autonomous AI Databases without copying data, managing database credentials, or setting up network connections manually.
In stateless environments (such as APEX or Database Actions SQL Worksheet),
test Select AI using DBMS_CLOUD_AI.GENERATE and pass the profile name
directly.
DECLARE
result CLOB;
BEGIN
result := DBMS_CLOUD_AI.GENERATE(
prompt => 'how many customers do I have',
profile_name => 'MY_AI_PROFILE',
action => 'showsql'
);
DBMS_OUTPUT.PUT_LINE(result);
END;
/
Parent topic: Query Remote and Federated Data
10.5.4 Example: Use External Table over Table Hyperlink with Select AI
This example shows how an Autonomous AI Database (consumer database) acts as an Live AI Hub, to query remote data hosted in another Autonomous AI Database (provider database) using an External Table over a Table Hyperlink.
This example uses the SH schema tables SH.CUSTOMERS and
SH.SALES in the provider Autonomous AI Database.
Select AI augments the prompt with table and view metadata, then sends it to the LLM to generate federated SQL. Select AI treats the external tables as local objects while the data remains in the remote Autonomous AI Database.
In stateless environments (such as APEX or Database Actions SQL Worksheet),
test Select AI using DBMS_CLOUD_AI.GENERATE and pass the profile name
directly.
DECLARE
result CLOB;
BEGIN
result := DBMS_CLOUD_AI.GENERATE(
prompt => 'how many customers do I have',
profile_name => 'AI_HYPERLINK_PROFILE',
action => 'showsql'
);
DBMS_OUTPUT.PUT_LINE(result);
END;
/
Parent topic: Query Remote and Federated Data
10.5.5 Example: Use Federated Table with Select AI
This example shows how an Autonomous AI Database (consumer) uses Select AI to query a Federated Table that automatically connects to a remote Autonomous AI Database (provider). The (consumer database) acts as an Live AI Hub (formerly called AI Proxy Database) to query remote data hosted in another Autonomous AI Database (provider database).
This example uses the SH schema table SH.CUSTOMERS in the
provider Autonomous AI Database. Both databases
belong to the same tenancy and compartment.
Select AI augments the prompt with table and view metadata, then sends it to the LLM to generate federated SQL. Select AI treats the federated tables as local objects while the data remains in the remote Autonomous AI Database.
In stateless environments (such as APEX or Database Actions SQL Worksheet),
test Select AI using DBMS_CLOUD_AI.GENERATE and pass the profile name
directly.
DECLARE
result CLOB;
BEGIN
result := DBMS_CLOUD_AI.GENERATE(
prompt => 'how many customers do I have',
profile_name => 'AI_FEDERATED_TABLE_PROFILE',
action => 'showsql'
);
DBMS_OUTPUT.PUT_LINE(result);
END;
/
Parent topic: Query Remote and Federated Data