DZone
Database Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Database Zone > This Is What It's Like When Data Collides (Relational + JSON)

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.

Shane Johnson user avatar by
Shane Johnson
·
Jun. 22, 17 · Database Zone · Tutorial
Like (1)
Save
Tweet
2.93K Views

Join the DZone community and get the full member experience.

Join For Free

In 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

{
  "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.

JSON Database Relational database Data integrity

Published at DZone with permission of , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Right Way to Hybridize Your Product Development Technique
  • The Evolution of Configuration Management: IaC vs. GitOps
  • Memory Debugging and Watch Annotations
  • Top Six Kubernetes Best Practices for Fleet Management

Comments

Database Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo