Oracle 12.1.0.2 對JSON的支援
阿新 • • 發佈:2018-10-31
Oracle 12.1.0.2版本有一個新功能就是可以儲存、查詢、索引JSON資料格式,而且也實現了使用SQL語句來解析JSON,非常方便。JSON資料在資料庫中以VARCHAR2, CLOB或者BLOB進行儲存。Oracle建議使用者在插入JSON資料之前,使用is_json來驗證輸入JSON資料的正確性。另外,Oracle也提供了相關的函式:
- Functions:json_value, json_query, json_table.
- Conditions:json_exists, is json, is not json, json_textcontains.
初識Oracle資料庫使用JSON
1:建立一個帶有表,裡面包含儲存JSON資料的欄位型別比如CLOB,而且需要對輸入JSON的驗證限制
[[email protected] ~]$ sqlplus sde/[email protected] SQL*Plus: Release 12.1.0.2.0 Production on Wed Jul 30 05:19:34 2014 Copyright (c) 1982, 2014, Oracle. All rights reserved. Last Successful login time: Tue Jul 29 2014 09:47:20 -04:00 Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options SQL> CREATE TABLE j_purchaseorder (id RAW (16) NOT NULL, date_loaded TIMESTAMP WITH TIME ZONE, po_document CLOB CONSTRAINT ensure_json CHECK (po_document IS JSON)); 2 3 4 5 Table created.
2:插入帶有JSON資料的一條記錄
SQL> INSERT INTO j_purchaseorder 2 VALUES (SYS_GUID(), 3 SYSTIMESTAMP, 4 '{ "PONumber" : 1600, 5 "Reference" : "ABULL-20140421", 6 "Requestor" : "Alexis Bull", 7 "User" : "ABULL", 8 "CostCenter" : "A50", 9 "ShippingInstructions" : { "name" : "Alexis Bull", 10 "Address": { "street" : "200 Sporting Green", 11 "city" : "South San Francisco", 12 "state" : "CA", 13 "zipCode" : 99236, 14 "country" : "United States of America" }, 15 "Phone" : [ { "type" : "Office", "number" : "909-555-7307" }, { "type" : "Mobile", "number" : "415-555-1234" } ] }, 16 17 "Special Instructions" : null, 18 "AllowPartialShipment" : false, 19 "LineItems" : [ { "ItemNumber" : 1, 20 "Part" : { "Description" : "One Magic Christmas", 21 "UnitPrice" : 19.95, 22 "UPCCode" : 13131092899 }, 23 "Quantity" : 9.0 }, 24 { "ItemNumber" : 2, 25 "Part" : { "Description" : "Lethal Weapon", 26 "UnitPrice" : 19.95, 27 "UPCCode" : 85391628927 }, 28 "Quantity" : 5.0 } ] }'); 1 row created.
3:進行查詢
SQL> SELECT po.po_document.PONumber FROM j_purchaseorder po;
PONUMBER
--------------------------------------------------------------------------------
1600
SQL> SELECT po.po_document.ShippingInstructions.Phone FROM j_purchaseorder po;
SHIPPINGINSTRUCTIONS
--------------------------------------------------------------------------------
[{"type":"Office","number":"909-555-7307"},{"type":"Mobile","number":"415-555-12
34"}]
SQL> SELECT po.po_document.ShippingInstructions.Phone.type FROM j_purchaseorder po;
SHIPPINGINSTRUCTIONS
--------------------------------------------------------------------------------
[Office,Mobile]