This Is What It's Like When Data Collides (Relational + JSON)
MariaDB Server will ensure both the inventory and the shopping cart are updated using multi-statement transactions. You can also use both relational and JSON data.
Join the DZone community and get the full member experience.
Join For FreeIn the past, a benefit to using non-relational databases (for example, NoSQL) was its simple and flexible structure. If the data was structured, a relational database was deployed. If it was semi-structured (for example, 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 |
|
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 |
|
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.
Published at DZone with permission of , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments