Sub topic 14
Choosing the Right Model
Assume you know basic SQL, primary keys, foreign keys, joins, and JSON objects and arrays. You should also know that Oracle Database 21c supports the native JSON data type, while JSON-relational duality views require Oracle Database 23ai.
Oracle gives you three useful ways to represent JSON-shaped data:
- A JSON column stores a document directly in a table.
- Relational tables store fields in columns and rows.
- A JSON-relational duality view presents normalized relational tables as JSON documents while keeping the underlying tables relational.
Choose the model based on how the application reads, changes, validates, and searches the data.
Compare the models
| Model | Best fit | Main strength | Main cost |
|---|---|---|---|
| JSON column | Variable or externally defined documents | Flexible document storage | More validation and query work |
| Relational tables | Stable fields and relationships | Strong constraints and efficient joins | More work to return nested JSON |
| Duality view | One data set with both document and relational clients | One source of truth with two representations | More design rules and Oracle-version dependency |
Use JSON columns for document-shaped data
Use a JSON column when the document is the natural unit of storage. This works well for event payloads, product attributes that vary by category, and data received from an external API.
CREATE TABLE api_events (
event_id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
payload JSON
);
CREATE INDEX api_events_type_ix
ON api_events (
JSON_VALUE(payload, '$.type' RETURNING VARCHAR2(40))
);This design keeps the document together and allows fields to change without changing the table definition. You can still index frequently searched paths.
The flexibility has limits. A JSON column does not automatically give every field a relational constraint. If every document must contain a valid customer identifier, enforce that requirement with JSON validation, constraints, application checks, or a combination of them. Queries that repeatedly extract many fields may also become harder to maintain.
Use a JSON column when most operations read or write the document as a whole. Avoid making it the default for data that has stable relationships, frequent reporting, or strict integrity rules.
Use relational tables for stable business data
Relational tables are usually the better choice when fields have stable meanings and relationships matter. Primary keys, foreign keys, unique constraints, and data types protect the data close to its source.
For example, employees and departments should normally remain separate tables when employees are independently searched, updated, or assigned to other records. Relational storage also fits reporting queries that aggregate large numbers of rows.
The trade-off is presentation. A client that expects one department document with an employees array needs JSON generation such as JSON_OBJECT and JSON_ARRAYAGG, or a separate API transformation layer.
Use duality views for two access styles
A duality view is a document-shaped view over relational tables. It is useful when one application needs JSON documents while other applications need relational rows.
flowchart LR
Client["JSON client"] --> View["JSON-relational duality view"]
Reports["SQL reports"] --> Tables["Normalized tables"]
View --> Tables
Tables --> Viewcaption: One relational source supports document and SQL access alt: A JSON client reads and writes through a duality view, while SQL reports access the same normalized tables directly.
With a duality view, document updates can write to several underlying tables. The view definition controls which operations are allowed. Annotations can make a field read-only or permit inserts, updates, or deletes.
Concurrency also matters. Duality views can use an ETag, a value derived from the document state, to detect whether another transaction changed the document since it was read. The client can reject a stale update instead of silently overwriting newer data.
Decide deliberately
Ask these questions:
- Is the document the primary business object, or only an API representation?
- Do fields and relationships need database-enforced constraints?
- Will reporting query individual fields across many records?
- Must document and relational clients update the same data?
- Can the deployment use Oracle Database 23ai?
- Which fields require indexes, and which changes must be protected by concurrency checks?
Exercise
An order arrives as JSON, but its customer, line items, and payments are queried independently and must satisfy foreign-key rules. Which model is the strongest default, and when might a duality view help?
Show answer
Use normalized relational tables as the source of truth. Add a duality view when JSON clients need the order as one document while reports and other services continue using relational rows.