Skip to main content

Introduction to Couchbase for Oracle Developers & Experts: Part 3: Data types



As part of data remodeling, while moving from the relational model to the JSON model, you’ll have to consider the data type mapping. In Oracle, you’ll have to create and declare the types of each column explicitly before you load the data or write queries. In Couchbase, you simply conform to JSON syntax and the data type interpretation is automatic and implicit. Here’s the overview of mappings, conversion, and arithmetic on these data types.

[Here’s the home page for the series: http://blog.planetnosql.com/2022/01/introduction-to-

ReModeling: From relational to Document model

 

OracleCouchbase
ModelRelational, Object-relationalJSON model with N1QL (SQL for JSON)
Data TypesLong list of data typeshttps://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/datatypes.html
String Data typesCHAR, VARCHAR, VARCHAR2, NCHAR, NVARCHAR, LOONGVARCHARString, up to 20MB in length. All of the string data in JSON is Unicode.
Date and Time Data TypesDATE, TIME, TIMESTAMP, all these with TIMEZONE, INTERVALDate, Time, Timestamp, all these with timezones should be represented in ISO 8601 format and saved as strings. N1QL has extensive functions for formatting, extraction, conversion, and arithmetic.

This article covers conversion and arithmetic on them in detail.

https://blog.couchbase.com/dates-timestamps-timezones-comparative-study-oracle-n1ql-support-date-time-feature-part-1/

BooleanBOOLEANBoolean 

true and false are automatically interpreted as a boolean value.

NumericalNUMBER

DECIMAL

BIGINT

SMALLINT

INTEGER

FLOAT

REAL

DOUBLE

Numeric data: can be integer, fraction or an exponent.  Here are the ranges: https://docs.couchbase.com/server/current/analytics/9_data_type.html
Binary Data TypesBINARY

VARBINARY

LONGVARBINARY

You can store the whole document as binary or encode the binary as base64 values.
Large Object Data Types
BLOB, RAW, LONG_RAWEach document can be up to 20 MB.  Binary data can be encoded via BASE64
CLOBEach document can be up to 20 MB. 
ABSTRACT Types, NESTED TABLESBuiltin support for objects, arrays, arrays of objects, objects of arrays. No support for user defined opaque data types.
XMLAnyone still use XML in databases? ;-) 
ANY TYPEANY TYPE was invented to make the Oracle routines flexiblle — in terms of types they handle as parameters and return types. 

In JSON model, 

ObjectsCREATE the object types and the columns associated with it explicity: CREATE TYPE person_typ AS OBJECT

URL: https://docs.oracle.com/en/database/oracle/oracle-database/18/adobj/key-features-object-relational-model.html#GUID-0C7CE261-467B-4F8B-A04D-B0900E1FB284

OBJECTs are built into JSON model with any number of nested levels with any data type, objects or arrays.
ArraysVARRAY can create an array of a specific type and then use that type as the type for a column. 

URL: https://docs.oracle.com/cd/B28359_01/appdev.111/b28371/adobjcol.htm#i468598

Array is a first class citizen in the JSON data model and can be used for any value. Each array can be of scalars of any type: scalars, objects, arrays, etc.

“A”: [1, “X”, [3, 4]]

“B”: [{“x”:1}, {“x”:2}]

Additional Notes

Date and Datetime types.

Oracle and other RDBMS have extensive data types for handling time-related data and manipulating them. JSON does not have a date or datetime type.   We’ve chosen the ISO 8601 . The idea is to store the date & time related data in a string form, conforming to ISO 8601 and then manipulating it in a consistent way.  See this blog with detailed comparison to Oracle types, conversion and formatting functions you can use in N1QL to extract and manipulate date and time data.

Primary key to Document key conversion.

Couchbase document key is always less than 255 bytes and is usually a string. RDBMS can have a single column or multiple columns (composite) primary key for a table. One common way convert is to simply have a separator between the individual parts after converting each part to a string. The document key should be unique to a bucket and hence it’s typically prefixed with the table (collection) type.  See the blog with examples of how to do this correctly.

 

Comments

Popular posts from this blog

Swami Vivekananda: The Monk That Nobody Sent to Chicago

  There’s a saying in Chicago: “We don’t want nobody that nobody sent.” This was the cold reception Swami Vivekananda faced when he arrived in the windy city in July 1893, determined to attend the World Parliament of Religions that September. He belonged to no organization, carried no letter of recommendation, his countrymen were nobody, and represented an alien religion to the Western world. As the days passed, his hope of attending the parliament dwindled. With money running out and the odds stacked against him, he left the Windy City and went to Boston, praying for a glimmer of opportunity.  Swamiji came to America to share India’s most profound gift: the wisdom of the Hindu sages, preserved through centuries of oral tradition and embodied by its monks. This was 1893, not 1993—India was under the British grip, its resources drained, and its spirit subdued. Swamiji’s mission was not just a cultural exchange; it was a bold step toward envisioning a future where India could re...

Why Should Databases Go Natural?

From search to CRM, applications are adopting natural language and intuitive interactions. Should databases follow? This article provides a strategic perspective. Amid the many technological evolutions in software and hardware (CISC/RISC, Internet, Cloud, and AI), one technology has endured:  Relational Database Systems   (RDBMS), aka SQL databases. For over 50 years, RDBMS has survived and thrived, overcoming many challenges. It has evolved and adopted beneficial features from emerging technologies like object-relational databases and now competes robustly with   NoSQL databases .  Today, RDBMS dominates the market, with four of the top five databases and seven of the top ten being relational. RDBMS has smartly borrowed ideas, like JSON support, from NoSQL, while NoSQL has also borrowed from RDBMS. NoSQL no longer rejects SQL. From a user perspective, all modern databases have SQL-inspired query language and a set of APIs. All applications manage the respective data...

iQ Interactive: Cool Things for Developers on Couchbase Capella iQ

  The landscape of software development is ever-evolving with the advent of new technologies. As we venture into 2023, natural language processing ( NLP ) is rapidly emerging as a pivotal aspect of programming. Unlike previous generations of tools that primarily aimed at enhancing coding productivity and code quality, the new generation of Artificial Intelligence ( GenAI ) tools, like iQ, is set to revolutionize every facet of a developer's workflow. This encompasses a wide range of activities: Reading, writing, and rewriting specifications Designing, prototyping, and coding Reviewing, refactoring, and verifying software Going through the iterative cycle of deploying, debugging, and improving the software Create a draft schema and sample data for any use case Natural language queries. Generate sample queries on a given dataset Fix the syntax error for a query Don't stop here. Let your imagination fly. Although the insights garnered from iQ are preliminary and should be treated ...