^ Back to parent
Adapting model from the provided products.json
.
Operation | Field | Description |
Normalize | category |
|
Normalize | rating |
|
Normalize | quantity |
|
Normalize | price |
|
Column | Type | Note |
---|---|---|
id | int | autoincrement, primary key |
code | char 255 | unique for deleted=0 |
name | varchar 1024 | |
description | varchar 5120 | |
image | varchar 2048 | optional |
ProductCategories_id | int | foreign key |
deleted | tinyint | boolean flag |
code_filtered_index_workaround | char 255 | NULL for deleted=1 |
- Before insert: set
code_filtered_index_workaround
value ascode
. - Before update: set
code_filtered_index_workaround
value ascode
ifdeleted=0
or asNULL
ifdeleted=1
.
Column | Type | Note |
---|---|---|
id | int | autoincrement, primary key |
name | varchar 1024 |
Depending on use cases, there may be needs for additional information such as: region, timezone, target group, active flag,… In this demo, only one price will be applied at a given time, date start defaults to now, any null date end in the system will update to now when a new price is inserted.
Column | Type | Note |
---|---|---|
id | int | autoincrement, primary key |
Products_id | int | primary key |
date_start | datetime | |
date_end | datetime | |
price | decimal (8,2) |
Depending on use cases, a transaction id for automatic update or reasons and user fields may be required. In this demo, only the latest inventory entry is considered, hence keeping track of inventory changes.
Column | Type | Note |
---|---|---|
id | int | autoincrement, primary key |
Products_id | int | primary key |
date | datetime | |
quantity | mediumint | |
inventory_status | enum('INSTOCK','LOWSTOCK','OUTOFSTOCK') | set in before insert triggers |
- Before insert: compute
inventory_status
value based offquantity
.
Product rating tallies all ratings and provides a rating value.
In the real world, a customer rating table would make more sense, linking a user, a product and a purchase to the rating value.
A computed value of the rating could be saved to the Products table. Its value updated on rating insert/update or at periodic intervals to simplify loads.
In this demo, only the latest rating is considered.
Column | Type | Note |
---|---|---|
id | int | autoincrement, primary key |
Products_id | int | primary key |
date | datetime | |
rating_count_5 | mediumint | |
rating_count_4 | mediumint | |
rating_count_3 | mediumint | |
rating_count_2 | mediumint | |
rating_count_1 | mediumint | |
rating | tinyint |
- Before insert: compute
rating
value based off rating counts.
These tables are not in use in the demo.
These tables represent a quick and dirty hack for optional features: user, auth and data history.
Column | Type | Note |
---|---|---|
id | int | autoincrement, primary key |
name | varchar 1024 |
A simplistic old style hash+salt password storage.
On a serious note, just implement an Oauth library.
Column | Type | Note |
---|---|---|
id | int | autoincrement, primary key |
Users_id | int | foreign key |
hash | VARCHAR 512 | |
salt | VARCHAR 45 |
Column | Type | Note |
---|---|---|
id | int | autoincrement, primary key |
role_name | VARCHAR 1024 | |
role_code | VARCHAR 512 |
Column | Type | Note |
---|---|---|
id | int | autoincrement, primary key |
Users_id | int | foreign key |
Roles_id | int | foreign key |
A table to store CRUD operations history.
This draft implementation only stores database changes.
A more realistic implementation should also track from which user actions CRUD operations originated.
Column | Type | Note |
---|---|---|
id | int | autoincrement, primary key |
Users_id | int | foreign key |
database_table | varchar 1024 | |
before | JSON | |
after | JSON | |
timestamp | datetime |
In one transaction:
- Insert to
Products
- Select LAST_INSERT_ID() as @newid
- Insert to
ProductsPrices
- Insert to
ProductsInventory
- Insert to
ProductsRatings
- Return @newid
In one transaction:
- Update
Products
if code, name, description, image or category is not null - If price is not null
- Update
ProductsPrices
change all nulldate_end
to now() with sameProducts_id
- Insert to
ProductsPrices
- Update
- Insert to
ProductsInventory
if quantity is not null
Source MySQL Workbench file
Exported SQL creation script
Exported SQL model diagram
A JS script is provided which converts the source products.json
into a SQL script that inserts its data to the Database model.