Sub topic 12
Inserting, Updating, and Reading Documents Through a Duality View
Assume you know SELECT, INSERT, UPDATE, joins, primary keys, and foreign keys. You should also understand JSON objects and arrays. The examples use Oracle Database 23ai, because JSON-relational duality views are available there.
A JSON-relational duality view presents normalized relational tables as JSON documents. An insert or update through the view is translated into changes to the underlying tables. The same rows remain available through ordinary relational SQL.
Create the tables and duality view
The following tables store departments and employees separately. The foreign key connects each employee to a department.
CREATE TABLE department (
department_id NUMBER PRIMARY KEY,
name VARCHAR2(50) NOT NULL
);
CREATE TABLE employee (
employee_id NUMBER PRIMARY KEY,
name VARCHAR2(50) NOT NULL,
department_id NUMBER REFERENCES department (department_id)
);The duality view maps one department row to one JSON object. Its nested query maps related employee rows to the employees array.
CREATE JSON RELATIONAL DUALITY VIEW department_dv AS
SELECT JSON {
'_id' : d.department_id,
'departmentName' : d.name,
'employees' : [
SELECT JSON {
'employeeId' : e.employee_id,
'name' : e.name
}
FROM employee e WITH INSERT UPDATE
WHERE e.department_id = d.department_id
]
}
FROM department d WITH INSERT UPDATE DELETE;The WITH clauses declare which operations the view may perform on each table. In this example, the view can insert, update, and delete departments. It can insert and update employees through the nested array. The exact permissions should match the operations your application needs.
Insert a complete document
Insert a JSON document into the view as if the view were a document collection.
INSERT INTO department_dv VALUES (
'{
"_id": 10,
"departmentName": "Research",
"employees": [
{"employeeId": 100, "name": "Grace"},
{"employeeId": 101, "name": "Alan"}
]
}'
);Oracle uses the document to create one row in department and two rows in employee. The department_id foreign key for both employee rows is derived from the relationship in the view.
The document must contain the fields required by the underlying tables. For example, omitting departmentName would violate the department.name NOT NULL constraint. Duplicate identifiers can also cause primary-key errors.
Read the document
A duality view can be queried as a document source. The data column contains the generated JSON document.
SELECT JSON_SERIALIZE(data PRETTY) AS department_document
FROM department_dv
WHERE JSON_VALUE(data, '$._id' RETURNING NUMBER) = 10;The result has this shape:
{
"_id" : 10,
"departmentName" : "Research",
"employees" :
[
{
"employeeId" : 100,
"name" : "Grace"
},
{
"employeeId" : 101,
"name" : "Alan"
}
]
}You can also read the same data relationally:
SELECT name, department_id
FROM employee
WHERE department_id = 10;This is the main benefit of a duality view. Document-oriented code can use the JSON representation, while reports and relational services can query the base tables.
Update through the view
Update the document value in the view. Oracle applies the change to the matching base table.
UPDATE department_dv
SET data = JSON_TRANSFORM(
data,
SET '$.departmentName' = 'Advanced Research'
)
WHERE JSON_VALUE(data, '$._id' RETURNING NUMBER) = 10;This changes department.name for department 10. It does not require manually locating and updating the row in department.
To update an employee nested in the document, target that employee by its identifier:
UPDATE department_dv
SET data = JSON_TRANSFORM(
data,
SET '$.employees[?(@.employeeId == 100)].name' = 'Augusta'
)
WHERE JSON_VALUE(data, '$._id' RETURNING NUMBER) = 10;The path predicate selects the employee whose employeeId is 100. Whether a particular nested update is allowed depends on the view definition and its updatability rules.
Exercise
After inserting a department document with two employees through department_dv, where are the three records stored?
Show answer
One department record is stored in department, and two employee records are stored in employee. The foreign key connects both employees to the department.