JSON Relational Duality

JSON Relational Duality

I really enjoyed playing with Oracle JSON Relational Duality which is a feature introduced to seamlessly integrate and manage both relational and JSON data in Oracle databases. It allows me to work with data as both JSON documents and relational tables, providing the flexibility to choose the most convenient data representation and access method based on the application’s requirements.

Here are the Key Benefits I noticed:

  1. Dual Representation:
    • Relational View: Data is stored in traditional relational tables with structured schemas.
    • JSON View: The same data can be accessed as JSON documents, making it easier to work with in applications that prefer JSON.
  2. Bi-Directional Mapping:
    • Changes made to the JSON representation are automatically reflected in the relational tables and vice versa.
  3. SQL and JSON Integration:
    • Allows for querying and updating JSON data using SQL.
    • Supports JSON functions and operators within SQL queries.

Here are some examples:

CREATE TABLE employees (
    employee_id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    name VARCHAR2(100),
    department_id NUMBER,
    salary NUMBER,
    json_data JSON
);
-- Insert using relational columns
INSERT INTO employees (name, department_id, salary)
VALUES ('John Doe', 10, 50000);

-- Insert using JSON data
INSERT INTO employees (json_data)
VALUES ('{"name": "Jane Smith", "department_id": 20, "salary": 60000}');
-- Query using relational columns
SELECT name, salary FROM employees WHERE department_id = 10;

-- Query using JSON data
SELECT json_data FROM employees WHERE json_data.department_id = 20;
-- Update relational columns
UPDATE employees
SET salary = salary * 1.1
WHERE department_id = 10;

-- Update JSON data
UPDATE employees
SET json_data = json_mergepatch(json_data, '{"salary": 66000}')
WHERE json_data.department_id = 20;
SELECT * FROM employees WHERE department_id = 1;
SELECT JSON_OBJECT(*) FROM employees WHERE department_id = 1;

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.