Data Modeling
What is data modeling?
The term data modeling refers to the process of defining the shape and structure of the objects in an application.
These objects are often called “application models”.
In relational databases, they are stored in tables. When using document databases, they are stored in collections.
Data modeling typically needs to happen on two levels:
- On the database level
- On the application level (i.e, in your programming language)
The way how the application models are represented on both levels might differ due to a few reasons:
- Databases and programming languages use different data types
- Relations are represented differently in a database than in a programing language
- Databases typically have more powerful data modeling capabilities, like indexes, cascading deletes, or a variety of additional constraints (e.g. unique, not null…)
- Databases and programming languages have different technical constraints
Data modeling on the database level
- Relational databases
In relational databases, models are represented by tables. For example, you might define ausers
table to store information about the users of your application. Using PostgreSQL, you would define it as:
CREATE TABLE users {
user_id SERIAL PRIMARY KEY NOT NULL,
name VARCHAR(255),
email VARCHAR(255) UNIQUE NOT NULL,
isAdmin BOOLEAN NOT NULL DEFAULT false
}
A visual representation of the users
table with some random data might look as follows
- MongoDB
In MongoDB databases, models are represented by collections and contain documents that can have any structure:
{
_id: '123',
slug: 'mongodb',
title: 'MongoDB',
body: ''}
https://docs.mongodb.com/manual/core/data-model-design/#normalized-data-models
Data modeling on the application level
In addition to creating the tables that represent the entities from your application domain, you also need to create application models in your programming language. In object-oriented languages, this is often done by creating classes to represent your models. Depending on the programming language, this might also be done with interfaces or structs.
There often is a strong correlation between the tables in your databases and the models you define in your code. For example, you might define a ES6 class looking similar to this
class User {
constructor(user_id, name, email, isAdmin) {
this.user_id = user_id
this.name = name
this.email = email
this.isAdmin = isAdmin
}
}
When using TypeScript, you might define an interface instead:
interface User {
user_id: Int
name: String
email: String
isAdmin: Boolean
}
Notice how the User
model in both cases has the same properties as the users
table in the previous example. While it’s often the case that there is a 1:1 mapping between database tables and application models, it can also happen that models are represented differently in the database and your application.
Data modeling with ORMs
ORMs are commonly used in object-oriented languages to make it easier for developers to work with databases. The key characteristic of an ORM is that it lets you model your application data in terms of classes which are mapped to tables in the underlying database.