Sub topic 3
The document versus relational dilemma
This chapter assumes that you know basic SQL (SELECT, INSERT, UPDATE, joins, and GROUP BY), primary and foreign keys, and the JSON object and array shapes. The duality view examples need Oracle Database 23ai.
Two models for the same data
A normalized model splits data across tables linked by keys. An order might span an orders table, an order_items table, and a customers table. A document model stores the same order as one nested JSON object. Each model suits different access patterns, and choosing between them is the dilemma.
Defining a JSON-relational duality view
A JSON-relational duality view (a view that presents relational rows as JSON documents) maps normalized tables to a document shape. The rows stay in the tables.
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)
);
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;Inserting, updating, and reading documents through a duality view
One document insert writes rows into both tables:
INSERT INTO department_dv VALUES (
'{"_id": 10,
"departmentName": "Research",
"employees": [
{"employeeId": 100, "name": "Grace"},
{"employeeId": 101, "name": "Alan"}
]}'
);
UPDATE department_dv d
SET d.data = JSON_TRANSFORM(d.data, SET '$.departmentName' = 'Labs')
WHERE JSON_VALUE(d.data, '$._id' RETURNING NUMBER) = 10;
SELECT json_serialize(data PRETTY)
FROM department_dv
WHERE JSON_VALUE(data, '$._id' RETURNING NUMBER) = 10;ETags and updatability annotations
Each document from a duality view carries an ETag, a version string derived from its content, in its _metadata section. A client that sends a stale ETag with an update can detect that someone else changed the document first.
The WITH clauses set which operations the view allows on each table. In this example, employee accepts inserts and updates but not deletes, and department accepts all three. Restricting operations this way protects tables from changes the application should not make.
Choosing the right model
Use a JSON column when documents are read and written whole and rows are not shared. Use normalized tables when the data is relational and queried across many joins. Use a duality view when one application needs documents and another needs relational access to the same rows.
Exercise
Your application stores orders as whole JSON documents, and a reporting team needs to join orders with a customers table. Which model fits best, and why?
Show answer
A duality view fits best. Documents serve the application, and the underlying normalized tables let the reporting team run joins without duplicating data.