Modeling JSON in Oracle Database Tables

thatjeffsmith SQL Developer 0 Comments

Tell Others About This Story:

Having an extra bit bucket in your tables is pretty easy now in Oracle Database 12c and higher. We offer native JSON support [JSON DOCS] – you can have a free form text column and store anything you want in it.

AND, we pretty much give you the full power of the SQL interface to those bit bucket items – pretty neat I think.

I’m still big fan of the relational paradigm, and I think data modeling is ALWAYS key to performance and flexbility for your database apps. So, how would one go about taking advantage of this in your Oracle SQL Developer Data Modeler designs?

CONSTRAINT XYZ123 CHECK (bit_bucket IS JSON)

That’s all you need – just add a column level check constraint.

Here’s how to do that in the modeler.

First off, build your table. Then you’re going to need to decide if you want to store your JSON in a VARCHAR2, CLOB, or BLOB. I’m going with CLOB because I’m not sure how big my bit bucket is going to get, and I prefer them to BLOBs.

I went with CLOB, but you use what you want to use.

That will bring up THIS dialog:

Give your constraint a GOOD name, and then click the CHECK constraint button.

And now we get to input our CHECK constraint text:

You don’t have to open the Edit Constraint text dialog, you can type it directly in the Constraint window if you’d prefer.

Previewing our DDL shows this:

CREATE TABLE identity_example (
    id       INTEGER
        GENERATED BY DEFAULT AS IDENTITY ( START WITH 1 NOCACHE ORDER )
    NOT NULL,
    text     CLOB,
    "date"   DATE
)
    LOGGING;
 
ALTER TABLE identity_example ADD CONSTRAINT text_is_json CHECK (
    text IS JSON
);
 
ALTER TABLE identity_example ADD CONSTRAINT identity_example_pk PRIMARY KEY ( id );

Wondering about the IDENTITY clause, and how to model that?

Make It So.

So I’m going to copy and paste that DDL to my SQL Developer worksheet window and run it. Table IDENTITY_EXAMPLE created. Table IDENTITY_EXAMPLE altered. Table IDENTITY_EXAMPLE altered…yada, yada, yada.

Now let’s go shove some data in.

The CHECK constraint is working, sweet.

Now let’s go query the thing.

It gets MUCH cooler than this – go check out the Data Guide feature.

Tell Others About This Story:

Leave a Reply

Your email address will not be published. Required fields are marked *