Search notes:

Oracle: JSON Relational Duality Views

JSON Relational Duality Views were introduced in 23c.
create table tq84_customer (
    id            integer generated by default on null as identity primary key,
    firstName     varchar2(30),
    lastName      varchar2(30) not null,
    address       varchar2(30) not null,
    phone         varchar2(10)
);
create json relational duality view tq84_customer_dv as
select json
    {'cust_id'         :  cust.id,
     'cust_first_name' :  cust.firstName,
     'cust_last_name'  :  cust.lastName,
     'cust_address'    :  cust.address,
     'cust_phone'      :  cust.phone
    }
from
    tq84_customer cust
with
    insert update delete;
insert into tq84_customer (firstname, lastname, address, phone) values ('John', 'Doe', '123 Main St', '555-1234');
select json_serialize(data pretty) from tq84_customer_dv;
-- "{
--   "_metadata" :
--   {
--     "etag" : "45B2D6051E527F0ECEFFB9C2B8FE3A63",
--     "asof" : "00000000002F26D6"
--   },
--   "cust_id" : 1,
--   "cust_first_name" : "John",
--   "cust_last_name" : "Doe",
--   "cust_address" : "123 Main St",
--   "cust_phone" : "555-1234"
-- }"
insert into tq84_customer_dv values ('
{
  "cust_first_name": "Jane",
  "cust_last_name" : "Smith",
  "cust_address"   : "456 Oak St",
  "cust_phone"     : "555-5678"
}
');
update tq84_customer_dv
   set data = json_transform(data, set '$.cust_address' = '789 Elm St')
where
   json_value(data, '$.cust_id') = 1;
select
   json_column_name,
   root_table_name,
   root_table_owner,
   json_serialize(json_schema pretty) json_schema,
   allow_insert,
   allow_update,
   allow_delete,
   read_only
from
   user_json_duality_views
where
   view_name = 'TQ84_CUSTOMER_DV';
select
   column_name,
   json_key_name,
   --
   allow_insert,
   allow_update,
   allow_delete,
   read_only,
   --
   primary_key_pos,
   etag_pos,
   --
   table_owner,
   table_name,
   data_type,
   length
from
   user_json_duality_view_tab_cols
where
   view_name = 'TQ84_CUSTOMER_DV';
select
   column_name,
   data_type,
   data_length         -- 32600
from
   user_tab_columns
where
   table_name = 'TQ84_CUSTOMER_DV';
drop view  tq84_customer_dv;
drop table tq84_customer;

Index