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:

The way how the application models are represented on both levels might differ due to a few reasons:

Data modeling on the database level

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

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store