0% found this document useful (0 votes)
17 views18 pages

Module 7 - Data Modelling & Constraints

The document discusses data modeling concepts for a blog application. It provides several options for modeling articles, users, categories, and tags in a relational database. The best practice is to normalize data by eliminating redundancy and adding constraints via foreign keys. A many-to-many relationship between articles and tags is modeled using a separate join table. Files can be stored on the file system with links in the database for performance. Data modeling should follow principles of normalization, transactions should uphold ACID properties, and key decisions require experimentation.

Uploaded by

jaydave422
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views18 pages

Module 7 - Data Modelling & Constraints

The document discusses data modeling concepts for a blog application. It provides several options for modeling articles, users, categories, and tags in a relational database. The best practice is to normalize data by eliminating redundancy and adding constraints via foreign keys. A many-to-many relationship between articles and tags is modeled using a separate join table. Files can be stored on the file system with links in the database for performance. Data modeling should follow principles of normalization, transactions should uphold ACID properties, and key decisions require experimentation.

Uploaded by

jaydave422
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

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

- Modelling data across various examples to learn


- Primary keys, Foreign keys, Constraints
- Normalisation
- Nullable columns, Schema migrations
- Modelling data across various real world types
- Relationships in your data
- ‘Enum’ types
- Storing files

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

A very simple app that will have

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

Username (text) Age (Int) Email (text) Description (text)

Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Articles
Option 1

title (text) content (Int) author (text) ...

Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Articles
Option 2

title (text) content (Int) user_id (int) ...

Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Articles
Option 3

title (text) content (Int) user_id (int, foreign key to ...


user.id)

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)

web, apps, databases

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)

You might also like