Day 19 - Data and Databases, Part 2 - Design a Database with UML
Let's design a database with UML and entity relationship diagrams, using the Top-down learning approach.
If you missed yesterday's post, check it out here. We introduced basic concepts about Data, Data types, What is a Database (DB), and different Database types, such as Relational and NoSQL databases.
We also touched upon several commonly used terms around Data and databases, such as Transactions, Normalisation, and Primary Key.
In today's post, we'll explore working with Databases hands-on, from a schematic perspective. By schematic, I mean being able to read UML diagrams and Entity Relationships (ER).
Why hands-on? Because I think it's the best way to learn something that sounds complex. This is called Top-down Learning and looks something like this:
According to the Merriam-Webster dictionary, Top-Down refers to the breaking down of large general aspects (as of a problem) into smaller more detailed constituents: working from the general to the specific.
In this post, we'll explore Data and Databases from this angle, and with the best, I can make it Top-down. 😄
Slides can be found here
What do we need?
If I want to break down Databases top-down, after we have covered the basics yesterday, I'll walk you through how to Design a Database by yourself. Sounds scary?
Well, it shouldn't be. Because we will do it all "on paper" and there is no cost for any mistakes. You can design any app you want (or use) in any way you'd like.
But before jumping into designing, we need to know how we are going to design a database. For that, we'll get familiar with UML first.
What is UML?
UML (like any other concept) can be deep and complex by itself, but let's touch on this in the Top-down approach too. We will learn only the things we need.
UML (Unified Modeling Language) is a standardized modeling language that is used to create visual diagrams of software systems. It is a visual language that is used to represent the structure and behavior of a system, including the relationships and interactions between different elements of the system.
UML includes a set of graphical notation elements that can be used to create visual models of a system. These elements include things like classes, objects, relationships, and interactions. The UML specification defines a set of rules for using these elements to create visual models of a system, and these models can be used to communicate the design of a system to stakeholders and developers.
These symbols (and a UML chart) look something like below. (Note it's a Cheat Sheet, so the text is explaining the symbol itself).
UML is often used to create diagrams that represent the structure of a system, including class diagrams that show the relationships between different classes, and sequence diagrams that show the interactions between objects and their relationships over time. UML is also used to create activity diagrams that show the flow of activities within a system, and state machine diagrams that show the different states that an object can be in and the transitions between those states.
In that sense, UML is like a map. Map of the system we are building, in the data and class levels.Â
Note that UML can be used for many more applciations and there are many different types, such as Structural (Class Diagram, Component Diagram, Package Diagram,..) and Behavioural (Activity, Communication, Interaction Diagrams, ...).
Today we focus on ERD (Entity Relationship Diagrams) to be specific.
Example UML diagram
Before jumping to understanding the pieces inside the map (which would be "normal" in a bottom-up approach, as we did in school), let's have a look at a complete UML for a database and then explore each element of it.
Let's look at a sample UML diagram like below.
The chart above has two main object types:
Boxes (called Entities)
and Lines, connecting the boxes, probably showing their "relationship".
ERD basics
A quick for me to dive into a new concept in the Top-down approach is always looking at the "cheat sheets" available for that topic.
🤓 I will use UML and ERD interchangeably going forward. ERD is a subset of UML diagrams, but in this post, we only talk about ERD diagrams.
You can start by searching for "ERD Chear sheet" and you'll get many images like the one below, explaining basic elements in one or a few pages.
Homework
Having the above cheat sheets and an app of your choice (for example Uber, Airbnb, Netflix, Amazon,...) design the ERD for each entity you can think of and connect them using the association links (cardinalities).
Which tools you can use?
You can use tools such as Miro and Lucidchart, among many others, to design your ERD.
More resources to read on ERD if you need any help:
Have fun diagramming!