Sub topic 11
Defining a JSON-Relational Duality View
Assume you know SELECT, joins, primary keys, foreign keys, and basic JSON objects and arrays. Also assume Oracle Database 23ai or later. JSON-relational duality views are an Oracle feature that presents normalized relational tables as JSON documents.
Why use a duality view?
Relational tables are good for constraints, joins, and updates to individual rows. Applications often prefer one document containing a department and its employees. A JSON-relational duality view provides both representations:
- The database stores normalized rows.
- Clients read a document-shaped JSON result.
- Writes through the view can update the underlying tables.
- The same rows remain available through ordinary relational SQL.
The view definition describes how table columns become JSON fields and how related rows become arrays.
| Relational data | JSON document |
|---|---|
| `department.name` | `departmentName` |
| Many `employee` rows | `employees` array |
Define the source tables
The tables need primary keys. A foreign key connects each employee to its 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)
);Create the duality view
The JSON constructor creates an object. Its field names are JSON property names. The nested query creates 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 _id field identifies the document. It maps to the department's primary key. The outer query produces one document for each row in department. For each department row, the nested query finds matching employees and places them in the employees array.
The WITH clauses are updatability annotations. They state 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, but the definition does not allow employee deletion through this view.
Understand the row-to-document mapping
The foreign-key condition is essential:
WHERE e.department_id = d.department_idIt tells Oracle which employee rows belong inside each department document. Without this relationship, the nested query could not correctly build the array.
A duality view normally needs stable key mappings. Do not use a display name as an identifier when a primary key is available. Names can change or may not be unique.
Visualize the mapping
Check the definition
After creating the view, inspect its metadata to confirm the object exists and review its columns.
SELECT object_name, object_type
FROM user_objects
WHERE object_name = 'DEPARTMENT_DV';
SELECT column_name, data_type
FROM user_tab_columns
WHERE table_name = 'DEPARTMENT_DV'
ORDER BY column_id;The exact exposed metadata can vary by Oracle release and client tool. The important check is that the view was created successfully and exposes its document data.
Exercise
Which part of the view definition places related employee rows into the department's JSON document?
Show answer
The nested SELECT JSON query, combined with the condition e.department_id = d.department_id, creates the employees array for each department.