Views reference
Using views to denormalize data
Though not required, it's a common practice to:
Generate the raw data you need in normalized tables
Mark those tables as hidden.
Create views to to model the denormalized tables.
For example, an ecommerce database contains orders
and order_items
tables. The orders table includes the total order amount, which is based on the sum of the prices and quantities of the order items.
In this case, you can:
Create an
orders_base
table with the order data.Create a view
orders
that joins theorders_base
table with theorder_items
table, and then calculates the total amount for each order:
Using variables in views
To get access to a variable, use the $
prefix. For example, to use the user_id
variable to create a view that selects a subset of records from an events
table:
SELECT * FROM events WHERE user_id = $user_id
Functions for working with JSON
SQLite provides some helpful functions to compose JSON objects and arrays.
json_object
To create a JSON object that pulls the user's first and last names from the users_base
table:
json_group_array
The json_group_array
function creates a JSON array from a set of values.
For example, to create an array of all roles for each user in the users_base
table:
Last updated