Starting with the new release MySQL 5.7 there is support to store
JSON documents in a column. During our recent Tech Tour events we
got questions about document validation, so ensuring that a JSON
document has a certain structure. (Funny. It all started with the
idea to be schema-free. Now people seem to need schema
enforcement.)
I have two ideas how to implement a schema validation for JSON
columns. The first one is by leveraging generated columns
together with a foreign key. The second idea is by implementing a
trigger. Today I want to focus on the generated columns and
foreign keys.
When defining foreign keys with generated columns there are two
limitations we need to be aware of:
- Foreign keys require indexes. JSON columns cannot be indexed. We need to leverage other types.
- Only STORED generated columns are supported for foreign keys.
So here is an example of an address table that …
[Read more]