Skip to content

Commit df6c83e

Browse files
committed
Adding the original Select AI sql-related scripts into the select-ai subfolder
Adding the original Select AI sql-related scripts into the select-ai subfolder. In the future we might remove the /sql folder from the root of the repo if it does not break any links in Docs for example.
1 parent 89cf73d commit df6c83e

24 files changed

Lines changed: 1894 additions & 0 deletions

select-ai/sql/README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Oracle Autonomous Database: Sample SQL Scripts
2+
Numerous SQL scripts are available to help you get started using Autonomous Database.
3+
4+
Prerequisites:
5+
* Create an Autonomous Database
6+
* [On OCI](../../multicloud/oci-cli/README.md)
7+
* [On Azure](../../multicloud/azure-cli/README.md)
8+
* [On Google Cloud](../../multicloud/gcloud-cli/README.md)
9+
* Azure and Google Cloud support private endpoints only; a VM must be deployed on the same VCN as Autonomous Database (or on a network that can access your Autonomous Database). OCI supports both public and private endpoints; ADB is deployed using public endpoints in our sample which means you can use your computer to access the database.
10+
* Use these great VS Code extensions that help you develop and debug your database apps:
11+
* SQL Developer for VS Code ([Learn More](https://www.oracle.com/database/sqldeveloper/vscode/) | [Marketplace](https://marketplace.visualstudio.com/items?itemName=Oracle.sql-developer))
12+
* Oracle Developer Tools for VS Code ([Learn More](https://docs.oracle.com/en/database/oracle/developer-tools-for-vscode/getting-started/gettingstarted.html) | [Marketplace](https://marketplace.visualstudio.com/items?itemName=Oracle.oracledevtools))
13+
14+
## Connect to Autonomous Database
15+
There are [numerous client tools](../../azure-cli/README.md#whats-next) that you can use to start working with Autonomous Database. This includes the built-in SQL Worksheet that's part of the Database Tools (and available from the linked OCI Console) or a tool like VS Code and one an Oracle Database extension.
16+
17+
## Sample scripts
18+
Try out these scripts to learn how to get started using Autonomous Database. Simply update the [`config.sql`](#configuration-file) script prior to running the samples:
19+
20+
|Script|Description|
21+
|----|---|
22+
|[credential-create.sql](credential-create.sql)|Autonomous Database credentials contain the secret keys used to connect to services - like Azure OpenAI. This script creates those credentials. It's called by multiple scripts listed below |
23+
|[data-create-sample-schema.sql](data-create-sample-schema.sql)|Create a sample user and install sample data|
24+
|[data-import-from-datalake.sql](data-create-synthetic-data.sql)|Import sample data from Azure Data Lake. Sample data was uploaded using the [`create-all-resources.sh`](../../azure-cli/create-all-resources.sh)and [`create-data-lake-storage.sh`](../../azure-cli/create-data-lake-storage.sh) scripts. You can run [`show-data-lake-storage-info.sh`](../../azure-cli/show-data-lake-storage-info.sh) to get connection information to the storage container.|
25+
|[data-export-to-datalake.sql](data-export-to-datalake.sql)|Export data from a table to your data lake storage|
26+
|[select-ai-admin-enable.sql](select-ai-admin-enable.sql)|Autonomous Database is secure by default. In order to access external services, you will need to enable connectivity. This script enables connectivity to your Azure OpenAI resource.|
27+
|[select-ai-create-profile.sql](select-ai-create-profile.sql)|A Select AI profile is used to connect to your AI provider. It includes information about your provider plus tables and view that you want to be targets for natural language queries|
28+
|[select-ai-create-synthetic-data.sql](select-ai-create-synthetic-data.sql)|Use AI to generate sample data sets. You will create tables and then populate them based on rules that you specify.|
29+
|[select-ai-nl2sql.sql](select-ai-nl2sql.sql)|Use natural language to query your data|
30+
|[select-ai-sql-function.sql](select-ai-sql-function.sql)|Use Select AI SQL functions to apply AI to your data. These examples summarize a support chat and make product recommendations based on info in your database|
31+
|[select-ai-transform-and-summarize.sql](select-ai-transform-and-summarize.sql)|This builds on the previous sample. This will use Select AI SQL functions to transform data, parsing unstructured text into structured fields. For example, derive an address from free-form text. Or, derive key phrases and sentiment and return the result in structured format. This is useful for then storing the data in a way that can be queried using SQL|
32+
|[select-ai-rag.sql](select-ai-rag.sql)|Select AI makes it easy to create AI vector management pipelines and then ask questions using AI and your organization's knowledge base|
33+
|[json-duality.sql](json-duality.sql)|Autonomous Database allows you to work with JSON in many different ways. JSON Collections stored your documents in tables. No changes are made to those documents. JSON Duality Views present relational data as JSON - allowing you to query and update that data using the JSON APIs (like MongoDB API) or SQL. Check out both approaches.|
34+
35+
### Configuration file
36+
Many of the SQL scripts rely on information found in your [config.sql](config.sql) file. Update the config file prior to running any of the scripts.
37+
38+
>**IMPORTANT:** This file will contain sensitive data that should be protected. Set the file's permissions so that only the file's owner can view its contents:
39+
```bash
40+
chmod 600 config.sql
41+
```
42+
43+
|Setting|Description|Example|
44+
|----|----|----|
45+
|CONN|JDBC Connection. Run the script <code>/multicloud/{oci-cli, azure-cli}/show-adb-info.sh</code> to easily get the connection details.|jdbc:oracle:thin:@(description= (retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1521)(host=your-host.oraclecloud.com))(connect_data=(service_name=my_quickstart_medium.adb.oraclecloud.com))(security=(ssl_server_dn_match=no)))
46+
|USER_NAME|Database user that will contain sample data|'moviestream'|
47+
|USER_PASSWORD|Password for the sample database user|'watchS0meMovies#'
48+
|**Select AI and GenAI** |[See Select AI documentation](https://docs.oracle.com/en/cloud/paas/autonomous-database/serverless/adbsb/sql-generation-ai-autonomous.html)
49+
|AI_PROVIDER|ADB can use different LLMs. Specify the AI provider that will be used by Select AI (oci, azure or google)|'oci'|
50+
|AI_PROFILE_NAME|The Select AI profile name that encapsulates the AI provider info + tables for NL2SQL. Enter a name using valid Oracle Database object naming rules.|'genai'|
51+
|AI_CREDENTIAL_NAME|This is a database credential that captures the secret key or other connection info. Enter a name using valid Oracle Database object naming rules.|'ai_cred'|
52+
|AI_ENDPOINT|The AI provider's endpoint. The endpoint should be the servername only. For example, myopenai.openai.azure.com. This is not required for OCI GenAI.|'ai_cred'|
53+
|AI_KEY|The API key for AI service. This is not required for OCI GenAI.|'3Cu9ABGBCwkyYI...'|
54+
|**Azure OpenAI settings**|
55+
|AZURE_OPENAI_RESOURCE_NAME|Name of the Azure OpenAI resource|'dev-adb-azure-openai'|
56+
|AZURE_OPENAI_DEPLOYMENT_NAME|Your Azure OpenAI deployment name. This is used for NL2SQL and AI SQLfunctions|'gpt-4o'|
57+
|AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME|The Azure OpenAI deployment that uses an embedding model. This is used for creating vector embeddings.|'text-embedding-ada-002'|
58+
|AI_KEY|Azure OpenAI secret key|'3Cu9AB...H53'|
59+
|AI_CREDENTIAL_NAME|The database credential that will be used to connect to Azure OpenAI|'azure_cred4o'|
60+
|**Data Lake/Object Storage**|
61+
|STORAGE_PROVIDER|Name of your storage provider. Valid values are oci, azure or google.|'oci'|
62+
|STORAGE_CREDENTIAL_NAME|The name of the Autonomous Database credential that's used to connect to your object storage service|'storage_cred'|
63+
|STORAGE_URL|The URL that points to your bucket or container. Run the script <code>/multicloud/{oci-cli, azure-cli}/show-data-lake-storage-info.sh</code> to easily get the storage details.|'https://abc.objectstorage.us-ashburn-1.oci.customer-oci.com/n/adwc4pm/b/adb-sample/o'
64+
|STORAGE_ACCOUNT_NAME|Name of your Azure Data Lake Storage Gen 2 account. Run the script <code>/multicloud/{oci-cli, azure-cli}/show-data-lake-storage-info.sh</code> to easily get the storage details.|'mysamplestorage'|
65+
|STORAGE_KEY|The secret key used to connecto Azure Data Lake Storage. Run the script <code>/multicloud/{oci-cli, azure-cli}/show-data-lake-storage-info.sh</code> to easily get the storage details.|'dJVNxq1YTT...jp/g=='
66+
|**OCI API user credentials**|[See Managing Native Credentials documentation](https://docs.oracle.com/en/cloud/paas/autonomous-database/serverless/adbsb/autonomous-manage-credentials.html#GUID-863FAF80-AEDB-4128-89E7-3B93FED550E)|
67+
|OCI_USER_OCID|Your OCI user's identifier|'ocid1.user.oc1..aaaa...'|
68+
|OCI_TENANCY_OCID|Your tenancy's identifier|'ocid1.tenancy.oc1..aaaaaaa...'|
69+
|OCI_FINGERPRINT|Your signing key's fingerprint|'ocid1.tenancy.oc1..aaaaaaa...'|
70+
|OCI_PRIVATE_KEY|Your private key. Do not include <code>-----BEGIN RSA PRIVATE KEY-----</code> and <code>-----END RSA PRIVATE KEY-----</code>. Add "-" as a new line character at the end of each line (except the last)|'MIIEpAIBA....-'|
71+
72+
73+
You can find the Azure OpenAI settings in the Azure OpenAI Studio:
74+
![Azure OpenAI settings](images/azure-openai.png)
75+
76+
77+
<hr>
78+
Copyright (c) 2026 Oracle and/or its affiliates.<br>
79+
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/

select-ai/sql/config-default.sql

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
-- Copyright (c) 2025 Oracle and/or its affiliates.
2+
-- Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
3+
4+
--
5+
-- Configuration information for Autonomous Database
6+
--
7+
-- Connect string
8+
-- you can get the ADB connection info by running /multicloud/{oci|azure|gcloud}-cli/show-adb-info.sh
9+
define CONN='jdbc:oracle:thin:@(description=...)'
10+
-- user name and password used for the sample data
11+
define USER_NAME='moviestream'
12+
-- # The password is for the sample user. It must be between 12 and 30 characters long and must include at least one uppercase letter, one lowercase letter, and one numeric character
13+
-- example: watchS0meMovies#
14+
define USER_PASSWORD=''
15+
16+
--
17+
-- GENAI
18+
-- Select AI LLM providers are pluggable. ADB will use the one specified below.
19+
--
20+
-- The AI provider: oci, azure or google
21+
define AI_PROVIDER='oci'
22+
-- The Select AI profile name that encapsulates the AI provider info + tables for NL2SQL
23+
define AI_PROFILE_NAME='genai'
24+
-- This is a database credential that captures the secret key or other connection info
25+
define AI_CREDENTIAL_NAME='AI_CRED'
26+
27+
-- The endpoint should be the servername only. This is not required for OCI GenAI.
28+
-- Examples:
29+
-- myopenai.openai.azure.com
30+
-- us-east4-aiplatform.googleapis.com
31+
define AI_ENDPOINT=''
32+
-- API key for AI service. This is not required for OCI GenAI.
33+
define AI_KEY=''
34+
35+
-- For Azure:
36+
-- Azure OpenAI resource name
37+
define AZURE_OPENAI_RESOURCE_NAME='your-openai-resource-name'
38+
-- Azure OpenAI deployment name
39+
define AZURE_OPENAI_DEPLOYMENT_NAME='your-openai-deployment-name'
40+
-- Azure OpenAI Embedding deployment name. This is used for creating embeddings for RAG
41+
define AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME='your-openai-emedding-deployment-name'
42+
43+
--
44+
-- Data Lake Storage
45+
--
46+
-- valid values for storage provider: oci, azure, google
47+
-- you can get the storage info by running /multicloud/{oci|azure|gcloud}-cli/show-data-lake-storage-info.sh
48+
define STORAGE_PROVIDER='oci'
49+
-- The url is a pointer to the bucket that will be used for import/export to object storage
50+
-- Examples:
51+
-- google: https://storage.googleapis.com/adb-sample
52+
-- azure : https://devadbstorageacct.blob.core.windows.net/adb-sample
53+
-- oci : https://adwc4pm.objectstorage.us-ashburn-1.oci.customer-oci.com/n/adwc4pm/b/adb-sample/o
54+
define STORAGE_URL=''
55+
56+
-- A database credential encapsulates the authentication details to the object store. Specify a name for the credential below
57+
define STORAGE_CREDENTIAL_NAME='storage_cred'
58+
59+
--Azure storage properties
60+
define AZURE_STORAGE_ACCOUNT_NAME=''
61+
define AZURE_STORAGE_KEY=''
62+
-- Google storage properties
63+
define GOOGLE_STORAGE_ACCESS_KEY=''
64+
define GOOGLE_STORAGE_SECRET=''
65+
66+
--
67+
-- OCI API credentials
68+
--
69+
-- One credential is used to access all services. You can get most of these settings from your ~/.oci/config file
70+
define OCI_USER_OCID=''
71+
define OCI_TENANCY_OCID=''
72+
define OCI_FINGERPRINT=''
73+
-- The private key requires a "-" continuation character at the end of the line.
74+
-- Do not include the private key's BEGIN and END lines
75+
define OCI_PRIVATE_KEY='example1-
76+
example2-
77+
example3'

select-ai/sql/config.sql

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
-- Copyright (c) 2025 Oracle and/or its affiliates.
2+
-- Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
3+
4+
--
5+
-- Configuration information for Autonomous Database
6+
--
7+
-- Connect string
8+
-- you can get the ADB connection info by running /multicloud/{oci|azure|gcloud}-cli/show-adb-info.sh
9+
define CONN='jdbc:oracle:thin:@(description=...)'
10+
-- user name and password used for the sample data
11+
define USER_NAME='moviestream'
12+
-- # The password is for the sample user. It must be between 12 and 30 characters long and must include at least one uppercase letter, one lowercase letter, and one numeric character
13+
-- example: watchS0meMovies#
14+
define USER_PASSWORD=''
15+
16+
--
17+
-- GENAI
18+
-- Select AI LLM providers are pluggable. ADB will use the one specified below.
19+
--
20+
-- The AI provider: oci, azure or google
21+
define AI_PROVIDER='oci'
22+
-- The Select AI profile name that encapsulates the AI provider info + tables for NL2SQL
23+
define AI_PROFILE_NAME='genai'
24+
-- This is a database credential that captures the secret key or other connection info
25+
define AI_CREDENTIAL_NAME='AI_CRED'
26+
27+
-- The endpoint should be the servername only. This is not required for OCI GenAI.
28+
-- Examples:
29+
-- myopenai.openai.azure.com
30+
-- us-east4-aiplatform.googleapis.com
31+
define AI_ENDPOINT=''
32+
-- API key for AI service. This is not required for OCI GenAI.
33+
define AI_KEY=''
34+
35+
-- For Azure:
36+
-- Azure OpenAI resource name
37+
define AZURE_OPENAI_RESOURCE_NAME='your-openai-resource-name'
38+
-- Azure OpenAI deployment name
39+
define AZURE_OPENAI_DEPLOYMENT_NAME='your-openai-deployment-name'
40+
-- Azure OpenAI Embedding deployment name. This is used for creating embeddings for RAG
41+
define AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME='your-openai-emedding-deployment-name'
42+
43+
--
44+
-- Data Lake Storage
45+
--
46+
-- valid values for storage provider: oci, azure, google
47+
-- you can get the storage info by running /multicloud/{oci|azure|gcloud}-cli/show-data-lake-storage-info.sh
48+
define STORAGE_PROVIDER='oci'
49+
-- The url is a pointer to the bucket that will be used for import/export to object storage
50+
-- Examples:
51+
-- google: https://storage.googleapis.com/adb-sample
52+
-- azure : https://devadbstorageacct.blob.core.windows.net/adb-sample
53+
-- oci : https://adwc4pm.objectstorage.us-ashburn-1.oci.customer-oci.com/n/adwc4pm/b/adb-sample/o
54+
define STORAGE_URL=''
55+
56+
-- A database credential encapsulates the authentication details to the object store. Specify a name for the credential below
57+
define STORAGE_CREDENTIAL_NAME='storage_cred'
58+
59+
--Azure storage properties
60+
define AZURE_STORAGE_ACCOUNT_NAME=''
61+
define AZURE_STORAGE_KEY=''
62+
-- Google storage properties
63+
define GOOGLE_STORAGE_ACCESS_KEY=''
64+
define GOOGLE_STORAGE_SECRET=''
65+
66+
--
67+
-- OCI API credentials
68+
--
69+
-- One credential is used to access all services. You can get most of these settings from your ~/.oci/config file
70+
define OCI_USER_OCID=''
71+
define OCI_TENANCY_OCID=''
72+
define OCI_FINGERPRINT=''
73+
-- The private key requires a "-" continuation character at the end of the line.
74+
-- Do not include the private key's BEGIN and END lines
75+
define OCI_PRIVATE_KEY='example1-
76+
example2-
77+
example3'
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
ARGUMENT 1 DEFAULT 'ALL'
2+
3+
define user_param=&1
4+
undefine 1
5+
6+
prompt "Creating credential: &user_param"
7+
8+
-- Get the config information
9+
@config.sql
10+
11+
-- This procedure will recreate credentials. You can specify a credential type (storage, OpenAI)
12+
13+
DECLARE
14+
l_exists number := 0;
15+
l_type varchar2(20) := nvl(upper('&user_param'),'ALL');
16+
l_username varchar2(400);
17+
l_password varchar2(400);
18+
BEGIN
19+
-- AI provider. Note, they will have different syntax based on the provider
20+
if l_type in ('AI','ALL') then
21+
-- Create your credential. Replace it if already exists
22+
select COUNT(*)
23+
into l_exists
24+
from user_credentials
25+
where upper(credential_name)=upper('&AI_CREDENTIAL_NAME');
26+
27+
-- credential exists, so drop it
28+
IF l_exists = 1 THEN
29+
dbms_cloud.drop_credential (
30+
credential_name => '&AI_CREDENTIAL_NAME'
31+
);
32+
END IF;
33+
34+
-- Check for OCI.
35+
IF UPPER('&AI_PROVIDER') = 'OCI' THEN
36+
dbms_cloud.create_credential(
37+
credential_name => '&AI_CREDENTIAL_NAME',
38+
user_ocid => '&OCI_USER_OCID',
39+
tenancy_ocid => '&OCI_TENANCY_OCID',
40+
fingerprint => '&OCI_FINGERPRINT',
41+
private_key => '&OCI_PRIVATE_KEY'
42+
);
43+
ELSE
44+
-- All other AI providers
45+
dbms_cloud.create_credential (
46+
credential_name => '&AI_CREDENTIAL_NAME',
47+
username => UPPER('&AI_PROVIDER'),
48+
password => '&AI_KEY'
49+
);
50+
END IF; -- OCI vs other AI services
51+
END IF; -- AI Credential
52+
53+
-- Create Storage credential
54+
if l_type in ('STORAGE','ALL') then
55+
-- Create your credential. Replace it if already exists
56+
select COUNT(*)
57+
into l_exists
58+
from user_credentials
59+
where upper(credential_name)=upper('&STORAGE_CREDENTIAL_NAME');
60+
61+
-- drop existing credential
62+
IF l_exists = 1 THEN
63+
dbms_cloud.drop_credential (
64+
credential_name => '&STORAGE_CREDENTIAL_NAME'
65+
);
66+
END IF;
67+
68+
-- Check for OCI
69+
IF UPPER('&STORAGE_PROVIDER') = 'OCI' THEN
70+
dbms_cloud.create_credential(
71+
credential_name => '&STORAGE_CREDENTIAL_NAME',
72+
user_ocid => '&OCI_USER_OCID',
73+
tenancy_ocid => '&OCI_TENANCY_OCID',
74+
fingerprint => '&OCI_FINGERPRINT',
75+
private_key => '&OCI_PRIVATE_KEY'
76+
);
77+
ELSE
78+
-- Google and Azure use different settings for username and password
79+
l_username := CASE WHEN UPPER('&STORAGE_PROVIDER') = 'AZURE' THEN '&AZURE_STORAGE_ACCOUNT_NAME' ELSE '&GOOGLE_STORAGE_ACCESS_KEY' END;
80+
l_password := CASE WHEN UPPER('&STORAGE_PROVIDER') = 'AZURE' THEN '&AZURE_STORAGE_KEY' ELSE '&GOOGLE_STORAGE_SECRET' END;
81+
82+
dbms_cloud.create_credential(
83+
credential_name => '&STORAGE_CREDENTIAL_NAME',
84+
username => l_username,
85+
password => l_password
86+
);
87+
END IF; -- OCI vs other AI services
88+
END IF; -- Storage
89+
END;
90+
/
91+
92+
-- Review the credentials
93+
COLUMN credential_name FORMAT A40
94+
COLUMN username FORMAT A40
95+
96+
select
97+
credential_name,
98+
username
99+
from user_credentials;

0 commit comments

Comments
 (0)