Sub topic 10
The Document versus Relational Dilemma
Prerequisites and the central choice
Assume you know basic SQL, primary and foreign keys, joins, and the JSON format. You should also understand that Oracle Database can store JSON separately from ordinary relational columns.
When an application exchanges data with an API, the data often looks like a document:
{
"departmentId": 10,
"departmentName": "Research",
"employees": [
{"employeeId": 100, "name": "Grace"},
{"employeeId": 101, "name": "Alan"}
]
}When the database stores the same information, a normalized relational design usually separates it into 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)
);A document model groups related data together. A relational model stores facts in rows and connects them with keys. Neither model is always better. The correct choice depends on how the data is read, changed, validated, and shared.
Comparing the models
A JSON column is useful when the document is the natural unit of storage. For example, an event payload may have changing fields that the database does not need to join with other data. The application can store and retrieve the document as one value.
Relational tables are usually better when the data has stable fields, many relationships, or strong constraints. Primary keys, foreign keys, unique constraints, and transactions protect the structure. SQL also makes grouping, joining, and reporting direct.
The trade-offs are easier to see in a comparison:
| Concern | JSON document | Relational tables |
|---|---|---|
| Shape | Flexible and nested | Fixed columns and rows |
| Relationships | Usually represented inside the document | Explicit foreign keys |
| Validation | JSON rules and application logic | Database constraints and data types |
| Read pattern | Fetch one aggregate document | Query parts across many rows |
| Repeated data | Can become duplicated | Can be normalized |
| Partial updates | Update paths inside a document | Update individual rows and columns |
A document can become difficult to manage when many users change different parts of it. A large document update may create conflicts even when the users changed unrelated nested values. A normalized design gives each fact its own row, but reconstructing a document requires joins and JSON-generation functions.
One data set, two views
Some systems need both interfaces. An order service may want one document per order, while finance needs relational queries across customers, orders, and line items. Maintaining a separate document store and relational store creates synchronization work and possible inconsistencies.
Oracle JSON-relational duality views address this tension. They expose relational rows through a document-shaped interface. The underlying tables remain normalized, while clients can read and write a JSON document. The view maps document fields to table columns and maps nested arrays to related rows.
This approach does not remove the modeling decision. It moves the decision to the storage layer and adds a controlled representation layer. You still need to decide which fields are authoritative, which operations are allowed, and how concurrent updates are handled.
Choosing deliberately
Use JSON columns when documents are self-contained, their shape changes often, and relational queries across their fields are limited.
Use relational tables when facts are shared by many records, relationships matter, or database constraints must enforce correctness.
Use a duality view when normalized tables are the right source of truth but clients need document-shaped reads and writes. This is especially useful when different consumers need different representations of the same data.
Exercise
A department has many employees. Reports frequently group employees by department, while an API needs one nested department document. Which design best fits these requirements?
Show answer
Keep department and employee as normalized tables, then expose a JSON-relational duality view for the API. The tables support reporting and constraints, while the view supplies the document shape.