1
1
/*
2
- ** Copyright (c) 2023 Oracle and/or its affiliates
2
+ ** Copyright (c) 2024 Oracle and/or its affiliates
3
3
** The Universal Permissive License (UPL), Version 1.0
4
4
**
5
5
** Subject to the condition set forth below, permission is hereby granted to any
44
44
-- through SQL.
45
45
--
46
46
-- PREREQUISITES
47
- -- Ensure that you have Oracle database 23c installed and running on a
47
+ -- Ensure that you have Oracle database 23ai installed and running on a
48
48
-- port. Ensure that the compatible parameter is set to 23.0.0.0.
49
49
--
50
50
-- USAGE
55
55
-- DIRECTORY, DROP ANY DIRECTORY, DBA TO janus IDENTIFIED BY janus;
56
56
--
57
57
-- NOTES
58
- -- Oracle Database 23c Free - Developer Release is the first release of
59
- -- the next-generation Oracle Database, allowing developers a head-start
60
- -- on building applications with innovative 23c features that simplify
61
- -- development of modern data-driven apps. The entire feature set of
62
- -- Oracle Database 23c is planned to be generally available within the
63
- -- next 12 months.
64
- --
65
58
-- Please go through the duality view documentation
66
59
-- (https://docs.oracle.com/en/database/oracle/oracle-database/23/jsnvu/index.html)
67
60
-- to learn more about duality views and their advantages.
@@ -137,6 +130,14 @@ CREATE TABLE driver_race_map
137
130
-- Create a trigger on the driver_race_map table to populate
138
131
-- the points fields in team and driver based on race results.
139
132
--
133
+ -- For people that are not familiar with Formula One: Depending on the position in a race,
134
+ -- both the racing team and the driver get points. There are two championships in Formula One:
135
+ -- one for the drivers and one for the teams. The team championship is called the Constructors'
136
+ -- Championship and the scoring system is the same as in the Drivers' Championship — except the
137
+ -- points from both drivers on a team are tallied together.
138
+ -- If you want to read up more, then check out
139
+ -- - https://www.redbull.com/in-en/formula-1-points-system-guide
140
+ --
140
141
CREATE OR REPLACE TRIGGER driver_race_map_trigger
141
142
BEFORE INSERT ON driver_race_map
142
143
FOR EACH ROW
182
183
183
184
-- Creation using SQL syntax
184
185
CREATE OR REPLACE JSON RELATIONAL DUALITY VIEW race_dv AS
185
- SELECT JSON {' raceId ' : r .race_id ,
186
+ SELECT JSON {' _id ' : r .race_id ,
186
187
' name' : r .name ,
187
188
' laps' : r .laps WITH NOUPDATE,
188
189
' date' : r .race_date ,
@@ -204,7 +205,7 @@ CREATE OR REPLACE JSON RELATIONAL DUALITY VIEW race_dv AS
204
205
CREATE OR REPLACE JSON RELATIONAL DUALITY VIEW race_dv AS
205
206
race @insert @update @delete
206
207
{
207
- raceId : race_id
208
+ _id : race_id
208
209
name : name
209
210
laps : laps @noUpdate
210
211
date : race_date
@@ -229,7 +230,7 @@ CREATE OR REPLACE JSON RELATIONAL DUALITY VIEW race_dv AS
229
230
230
231
-- Creation using SQL syntax
231
232
CREATE OR REPLACE JSON RELATIONAL DUALITY VIEW driver_dv AS
232
- SELECT JSON {' driverId ' : d .driver_id ,
233
+ SELECT JSON {' _id ' : d .driver_id ,
233
234
' name' : d .name ,
234
235
' points' : d .points ,
235
236
UNNEST
@@ -254,7 +255,7 @@ CREATE OR REPLACE JSON RELATIONAL DUALITY VIEW driver_dv AS
254
255
CREATE OR REPLACE JSON RELATIONAL DUALITY VIEW driver_dv AS
255
256
driver @insert @update @delete
256
257
{
257
- driverId : driver_id
258
+ _id : driver_id
258
259
name : name
259
260
points : points
260
261
team @noInsert @noUpdate @noDelete @unnest
@@ -282,7 +283,7 @@ CREATE OR REPLACE JSON RELATIONAL DUALITY VIEW driver_dv AS
282
283
283
284
-- Creation using SQL syntax
284
285
CREATE OR REPLACE JSON RELATIONAL DUALITY VIEW team_dv AS
285
- SELECT JSON {' teamId ' : t .team_id ,
286
+ SELECT JSON {' _id ' : t .team_id ,
286
287
' name' : t .name ,
287
288
' points' : t .points ,
288
289
' driver' :
@@ -298,7 +299,7 @@ CREATE OR REPLACE JSON RELATIONAL DUALITY VIEW team_dv AS
298
299
CREATE OR REPLACE JSON RELATIONAL DUALITY VIEW team_dv AS
299
300
team @insert @update @delete
300
301
{
301
- teamId : team_id
302
+ _id : team_id
302
303
name : name
303
304
points : points
304
305
driver : driver @insert @update
@@ -330,7 +331,7 @@ SELECT json_serialize(data PRETTY) FROM team_dv;
330
331
-- This automatically populates the driver and team table as well as the
331
332
-- driver collection.
332
333
--
333
- INSERT INTO team_dv VALUES (' {"teamId " : 301,
334
+ INSERT INTO team_dv VALUES (' {"_id " : 301,
334
335
"name" : "Red Bull",
335
336
"points" : 0,
336
337
"driver" : [ {"driverId" : 101,
@@ -340,7 +341,7 @@ INSERT INTO team_dv VALUES ('{"teamId" : 301,
340
341
"name" : "Sergio Perez",
341
342
"points" : 0} ]}' );
342
343
343
- INSERT INTO team_dv VALUES (' {"teamId " : 302,
344
+ INSERT INTO team_dv VALUES (' {"_id " : 302,
344
345
"name" : "Ferrari",
345
346
"points" : 0,
346
347
"driver" : [ {"driverId" : 103,
@@ -350,7 +351,7 @@ INSERT INTO team_dv VALUES ('{"teamId" : 302,
350
351
"name" : "Carlos Sainz Jr",
351
352
"points" : 0} ]}' );
352
353
353
- INSERT INTO team_dv VALUES (' {"teamId " : 2,
354
+ INSERT INTO team_dv VALUES (' {"_id " : 2,
354
355
"name" : "Mercedes",
355
356
"points" : 0,
356
357
"driver" : [ {"driverId" : 105,
@@ -363,19 +364,19 @@ INSERT INTO team_dv VALUES ('{"teamId" : 2,
363
364
-- Insert a collection of race documents into RACE_DV.
364
365
-- This automatically populates the race table.
365
366
--
366
- INSERT INTO race_dv VALUES (' {"raceId " : 201,
367
+ INSERT INTO race_dv VALUES (' {"_id " : 201,
367
368
"name" : "Bahrain Grand Prix",
368
369
"laps" : 57,
369
370
"date" : "2022-03-20T00:00:00",
370
371
"podium" : {}}' );
371
372
372
- INSERT INTO race_dv VALUES (' {"raceId " : 202,
373
+ INSERT INTO race_dv VALUES (' {"_id " : 202,
373
374
"name" : "Saudi Arabian Grand Prix",
374
375
"laps" : 50,
375
376
"date" : "2022-03-27T00:00:00",
376
377
"podium" : {}}' );
377
378
378
- INSERT INTO race_dv VALUES (' {"raceId " : 203,
379
+ INSERT INTO race_dv VALUES (' {"_id " : 203,
379
380
"name" : "Australian Grand Prix",
380
381
"laps" : 58,
381
382
"date" : "2022-04-09T00:00:00",
@@ -413,7 +414,7 @@ SELECT json_serialize(data PRETTY) FROM race_dv;
413
414
-- is used by the REST interface to translate QBEs.
414
415
--
415
416
SELECT json_serialize(data PRETTY)
416
- FROM race_dv WHERE json_value(data, ' $.raceId ' ) = 201 ;
417
+ FROM race_dv WHERE json_value(data, ' $._id ' ) = 201 ;
417
418
418
419
-- Project specific document fields.
419
420
-- In SQL, specific documents fields can be requested using the KEEP operator
@@ -448,7 +449,7 @@ SELECT json_serialize(json_transform(data, KEEP '$.name', '$.team') PRETTY)
448
449
--
449
450
UPDATE race_dv dv
450
451
SET data = (' {_metadata : {"etag" : "2E8DC09543DD25DC7D588FB9734D962B"},
451
- "raceId " : 201,
452
+ "_id " : 201,
452
453
"name" : "Bahrain Grand Prix",
453
454
"laps" : 57,
454
455
"date" : "2022-03-20T00:00:00",
@@ -475,16 +476,15 @@ UPDATE race_dv dv
475
476
"position" : 4,
476
477
"driverId" : 105,
477
478
"name" : "George Russell"} ]}' )
478
- WHERE dv .data .raceId = 201 ;
479
+ WHERE dv .data ." _id " = 201 ;
479
480
480
481
COMMIT ;
481
482
482
483
-- See the results for the Bahrain Grand Prix.
483
484
-- You can use a predicate on the primary key field to query by ID.
484
485
--
485
486
SELECT json_serialize(data PRETTY)
486
- FROM race_dv dv WHERE dv .data .raceId = 201 ;
487
-
487
+ FROM race_dv dv WHERE dv .data ." _id" = 201 ;
488
488
489
489
-- ----------------------------------------------------------------------------
490
490
-- Step 7: Update specific fields in the document identified by a predicate --
@@ -542,7 +542,7 @@ SELECT json_serialize(data PRETTY) FROM team_dv dv
542
542
--
543
543
UPDATE team_dv dv
544
544
SET data = (' {_metadata : {"etag" : "855840B905C8CAFA99FB9CBF813992E5"},
545
- "teamId " : 2,
545
+ "_id " : 2,
546
546
"name" : "Mercedes",
547
547
"points" : 40,
548
548
"driver" : [ {"driverId" : 106,
@@ -555,7 +555,7 @@ UPDATE team_dv dv
555
555
556
556
UPDATE team_dv dv
557
557
SET data = (' {_metadata : {"etag" : "DA69DD103E8BAE95A0C09811B7EC9628"},
558
- "teamId " : 302,
558
+ "_id " : 302,
559
559
"name" : "Ferrari",
560
560
"points" : 30,
561
561
"driver" : [ {"driverId" : 105,
@@ -594,7 +594,7 @@ SELECT json_serialize(data PRETTY) FROM driver_dv dv
594
594
--
595
595
UPDATE driver_dv dv
596
596
SET DATA = (' {_metadata : {"etag" : "FCD4CEC63897F60DEA1EC2F64D3CE53A"},
597
- "driverId " : 103,
597
+ "_id " : 103,
598
598
"name" : "Charles Leclerc",
599
599
"points" : 25,
600
600
"teamId" : 2,
@@ -609,7 +609,7 @@ UPDATE driver_dv dv
609
609
}
610
610
]
611
611
}' )
612
- WHERE dv .data .driverId = 103 ;
612
+ WHERE dv .data ." _id " = 103 ;
613
613
614
614
615
615
-- --------------------------------
@@ -623,7 +623,7 @@ UPDATE driver_dv dv
623
623
-- valid SQL expression, e.g. equality on primary key, some condition using
624
624
-- simplified syntax, or JSON function, such as json_value or json_exists.
625
625
--
626
- DELETE FROM race_dv dv WHERE dv .data .raceId = 201 ;
626
+ DELETE FROM race_dv dv WHERE dv .data ." _id " = 201 ;
627
627
628
628
SELECT json_serialize(data PRETTY) FROM race_dv;
629
629
SELECT json_serialize(data PRETTY) FROM driver_dv;
0 commit comments