Module 7 - Data Modelling & Constraints
Module 7 - Data Modelling & Constraints
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Contents
Objectives
To cover basic data modelling concepts via several examples for a blog site.
Topics
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Our task
We’re going to start modelling a blog app’s database.
A typical blog usually has text, digital images, links to other blogs, links to other web pages, etcetera
1. Users
2. Articles
3. Categories & Tags
4. Comments
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Users
A simple table to store user information
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Articles
Option 1
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Articles
Option 2
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Articles
Option 3
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Articles
Option 4
title (text) content (Int) user_id (int, foreign key to category (text)
user.id)
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Articles
Option 4
article
title (text) content (Int) user_id (int, foreign key category (text,
to user.id) foreign-key to
category.name)
category
name (text)
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Articles
Option 5
title (text) content user_id (int, foreign category (text, foreign-key to tag1 tag2
(Int) key to user.id) category.name)
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Articles
Option 5
title (text) content (Int) user_id (int, foreign key category (text, tags
to user.id) foreign-key to
category.name)
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Articles
Option 5 article
id (int) title (text) content (Int) user_id (int, foreign key category (text,
to user.id) foreign-key to
category.name)
article_tag
article_id (int) tag
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Articles
Option 5 article
id (int) title (text) content (Int) user_id (int, foreign key category (text,
to user.id) foreign-key to
category.name)
article_tag tag
article_id (int) Tag (text, foreign Name (text)
key to tag.name)
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Patterns!
- Do not keep redundant data. Keep a single copy of the truth
- user_id instead of using author_name as a column in the article table
- To constraint values, use foreign keys
- Foreign keys are not pointers. They are constraints between columns of tables
- A many-to-one relationship can be modelled by having a foreign key column in the table
- Many articles belong to one owner
- A many-to-many relationship can be modelled by having a separate table between two tables that
has foreign keys to both the tables
- An article has many tags
- Each tag can be used by many articles
- article_tag table has a foreignkey to article table (via an id) and the tag table via the tag’s name
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Suppose we want to store files?
- Every user has a profile pic.
- What column type should we use for storing files?
- Most databases have a BLOB type or a text type that can store a lot of bytes
Option 1
Username (text) Age (Int) Email (text) Description (text) Profile_pic (binary/text)
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Suppose we want to store files?
- Instead we can store the file on the file system and just store link of the file on the file system in the
database
Option 2
Username (text) Age (Int) Email (text) Description (text) Profile_pic (link_to_file)
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Some important points
- The process of organizing data into columns and tables such as to reduce redundancy in the data is
called Normalization.
- A Database transaction is a set of database operations which perform a single logical operation
- A Database transaction should follow ACID properties
- Atomicity - Either all operations within a transaction happen or none happen
- Consistency - The state of the database after a transaction should be valid. No constraints should be violated.
- Isolation - The result of running two transactions concurrently should be the same as if they had been run
serially.
- Durability - Once a transaction is completed it should persist in any scenario. Ie: Even if the DB crashes or
there is power loss
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Key Takeaways
- While modelling data for an SQL database
- Make sure that there is a single source of truth (normalize)
- Model constraints well
- Analyze trade-offs. Study established patterns and don’t take a decision without experimenting yourself.
- Transactions
- Important for applications where there is significant data processing (many db requests) and concurrent usage
of the database could result in inconsistencies
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)