This is What it’s Like When Data Collides (Relational + JSON)

JUNE 16, 2017

In the past, a benefit to using non-relational databases (i.e., NoSQL) was its simple and flexible structure. If the data was structured, a relational database was deployed. If it was semi-structured (e.g., JSON), a NoSQL database was deployed.

Today, a relational database like MariaDB Server (part of MariaDB TX) can read, write and query both structured and semi-structured data, together. MariaDB Server supports semi-structured data via dynamic columns and JSON functions. This blog post will focus on JSON functions with MariaDB Server, using examples to highlight one of they key benefits: data integrity.

MariaDB JSON example #1 – Table constraints with JSON

In this example, there is a single table for products, and every product has an id, type, name, format (e.g., Blu-ray) and price. This data will be stored in the id, name, format and price columns. However, every movie has audio and video properties and this data will be stored in the attributes column, in a JSON document.

id

type

name

format

price

attr

1

M

Aliens

Blu-ray

13.99

{

  “video”: {

     “resolution”: “1080p”,

     “aspectRatio”: “1.85:1”

  },

  “cuts”: [{

     “name”: “Theatrical”,

     “runtime”: 138

  },

  {

     “name”: “Special Edition”,

     “runtime”: 155

  }

  ],

  “audio”: [“DTS HD”, “Dolby Surround”]

}

We can’t have anyone inserting a movie record without audio and video properties.

To ensure data integrity, the JSON document for audio and video attributes must have:

  • a video object

    • with a resolution

    • with an aspect ratio

  • an audio array

    • with at least one element

  • a cuts array

    • with at least one element

  • a disks integer

And while you may not be able to enforce the data integrity of JSON documents in a NoSQL database, you can with MariaDB Server – create a table constraint with JSON functions.

ALTER TABLE products ADD CONSTRAINT check_movies
	CHECK (
type = 'M' and 
	JSON_TYPE(JSON_QUERY(attr, '$.video')) = 'OBJECT' and
	JSON_TYPE(JSON_QUERY(attr, '$.cuts')) = 'ARRAY' and
	JSON_TYPE(JSON_QUERY(attr, '$.audio')) = 'ARRAY' and
	JSON_TYPE(JSON_VALUE(attr, '$.disks')) = 'INTEGER' and
	JSON_EXISTS(attr, '$.video.resolution') = 1 and
	JSON_EXISTS(attr, '$.video.aspectRatio') = 1 and
JSON_LENGTH(JSON_QUERY(attr, '$.cuts')) > 0 and
JSON_LENGTH(JSON_QUERY(attr, '$.audio')) > 0);

With MariaDB you get the best of both worlds, the data integrity of a relational database and the flexibility of semi-structured data.

MariaDB JSON example #2 – Multi-statement transactions

In this example, there is one table for inventory and one table for shopping carts. In the inventory table, there are columns for the SKU, the in-stock quantity and the in-cart quantity. In the shopping cart table, there are columns for the session id and the cart.

Inventory

sku

in_stock

in_cart

123456

1

99

Shopping carts

session_id

cart

aldkjdi8i8jdlkjd

{

  “items”: {

     “123456”: {

        “name”: “Aliens”,

        “quantity”: 1

     }

  }

}

Let’s assume this is the SKU of Aliens on Blu-ray (one of the best movies ever), and let’s assume we have to update the inventory when a customer adds a copy of it to their shopping cart.

It’s easy to update the inventory table.

UPDATE products SET 
in_stock = in_stock = in_stock - 1,
In_cart = in_cart + 1
WHERE sku = '123456';

What about the JSON document for the shopping cart?

UPDATE products SET
	cart = JSON_SET(cart, '$.items.123456.quantity', 2)
WHERE session_id = 'aldkjdi8i8jdlkjd';

This is a problem for NoSQL databases, which do not support multi-statement transactions like relational databases do. You don’t know what’s going to be updated: both the inventory and shopping cart, either the inventory or the shopping cart, or neither.

MariaDB Server will ensure both the inventory and the shopping cart are updated using multi-statement transactions. On top of that, you get to use both structured and semi-structured data in the same database, together – relational and JSON.

Learn more

Also, read two new white papers: dynamic columns and JSON functions.

About Shane Johnson

Shane Johnson is the Senior Director of Product Marketing at MariaDB. Prior to MariaDB, he led product and technical marketing at Couchbase. In the past, he performed technical roles in development, architecture and evangelism at Red Hat and other companies. His background is in Java and distributed systems.

Sponsored by MariaDB

You may also like...