Skip to content

Commit c0890e1

Browse files
committed
New example JSON
SQL for JSON Collection and JSON Duality View. Updated README to include the JSON sql
1 parent fd89c79 commit c0890e1

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

sql/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Try out these scripts to learn how to get started using Autonomous Database. Sim
2525
|[select-ai-nl2sql.sql](select-ai-nl2sql.sql)|Use natural language to query your data|
2626
|[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|
2727
|[select-ai-rag.sql](select-ai-rag.sql)|Select AI makes it easy to create AI vector pipelines and then ask questions using AI and your organization's knowledge base|
28-
28+
|[json-duality.sql](json-duality.sql)|Autonomous Database allows you to with JSON in many different ways. JSON Collections stored in the database, and JSON Duality Views are a couple of the examples here.|
2929

3030
### Configuration file
3131
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.

sql/json-duality.sql

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
-- Copyright (c) 2024 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+
/* Apply AI SQL function to your data to summarize information, make recommendations and more */
5+
6+
/* PREREQUISITES
7+
Install the sample schema using script
8+
@data-create-sample-schema.sql
9+
*/
10+
11+
/* Working with JSON in Autonomous Database
12+
We can query relational data as JSON and also create JSON Collection tables. Different ways to work with JSON,
13+
flexiblity to work with JSON, and leverage relational tables by using JSON Duality views providing the best of both worlds.
14+
Let's look at a couple of ways of creating JSON collection tables, JSON Duality Views and querying JSON data.
15+
*/
16+
-- JSON Collection
17+
create JSON COLLECTION TABLE MOVIE_BUDGET;
18+
19+
insert into movie_budget values ('{"movie_id": 1,"movie_title":"Avatar","movie_year": 2009,
20+
"sku":"LYG56160","runtime": 162,"cast":["Sam Worthington","Zoe Saldana"],
21+
"studio":["20th Century Studios","Lightstorm Entertainment"]}' );
22+
insert into movie_budget values ('{"movie_id": 2,"movie_title":"Ghostbusters II","movie_year": 1989,
23+
"sku":"FWT19789","runtime": 104,"cast":["Bill Murray","Sigourney Weaver"],
24+
"genre":["Fantasy","Sci-Fi","Thriller","Comedy"]}' );
25+
--see the JSON collection that has been created along with the ability to query it
26+
select * from movie_budget;
27+
--Let's add budget information to the movies
28+
update movie_budget set data= JSON_TRANSFORM(data, set '$.budgetUnit' = 'Million USD', set '$.budget'=1000000);
29+
select json_value(data, '$.movie_id') movie_id,json_value(data,'$.budget') budget from movie_budget;
30+
--Change the budget for one of the movies
31+
update movie_budget set data= JSON_TRANSFORM(data, set '$.budget'=(json_value(data,'$.budget') * 2))
32+
where JSON_VALUE(data,'$.movie_id')=2;
33+
select json_value(data, '$.movie_id') movie_id,json_value(data,'$.budget') budget from movie_budget;
34+
35+
--JSON Duality
36+
/* We can work with our relational tables as JSON Documents by creating a JSON Duality View.
37+
Inserts, updates and deletes can be performed directly against the JSON Duality View and the changes happen on the underlying relational tables.
38+
This means that the JSON documents for these views will be updated without having to maintain each document, and the data is stored relationally
39+
avoiding duplication.
40+
*/
41+
--Simple JSON view on one table
42+
43+
--We need primary keys on the tables that are part of the views
44+
CREATE OR REPLACE JSON RELATIONAL DUALITY VIEW customer_dv AS
45+
customer @insert @update @delete
46+
{
47+
_id : cust_id,
48+
FirstName : first_name,
49+
LastName : last_name,
50+
age : age,
51+
Email : email,
52+
street_address : street_address,
53+
city : city,
54+
postal_code : postal_code
55+
yrs_customer : yrs_customer
56+
pet : pet
57+
}
58+
;
59+
select * from customer_dv;
60+
61+
--JSON Duality View on a couple of tables. We do need primary keys on the tables being used in the views
62+
alter table streams add constraint constraint_name primary key(cust_id,day_id, movie_id);
63+
--JSON of the customers with the movies that they streamed
64+
CREATE OR REPLACE JSON RELATIONAL DUALITY VIEW customer_streams_dv AS
65+
customer @insert @update @delete
66+
{
67+
_id : cust_id,
68+
FirstName : first_name,
69+
LastName : last_name,
70+
age : age,
71+
yrs_customer : yrs_customer
72+
streams : streams
73+
[{
74+
cust_id : cust_id
75+
day_id : day_id
76+
genre_id : genre_id
77+
movie_id : movie_id
78+
}]
79+
}
80+
;
81+
select * from customer_streams_dv;
82+
--Pull back the customers watching romance movies
83+
select * from customer_streams_dv
84+
where JSON_VALUE(data,'$.streams.genre_id')=19;
85+
86+
/* With the JSON Collections and JSON Duality Views you can use API calls with GET and PUT to work
87+
with the JSON in the database. We have just shown here SQL access to the JSON. */
88+

0 commit comments

Comments
 (0)