Types of Databases

Types of Databases

When it comes to designing a database for your app, you can choose from 3 types of databases:

In this tutorial, we’ll only work with relational databases. Much of what you learn about relational databases can be applied to other types of app databases, but that doesn’t work the other way around.

The Schema: Defining Database Objects & Properties

Let’s say we are creating a mobile app database for a social media app like Facebook or Instagram.

We’ll design 3 objects:

  1. A Post that stores information for a social media post, like some text, a photo, and an array of likes

  2. A User that stores information for a user, like username, password, and posts the user has created

  3. A Like that stores information related to a “like” given to a post by a user

When you’re designing your database, simply start by looking through its mockups or UI designs. Every single user interface or app screen typically corresponds to a data object. Which types of data do you see? Start there.

In a social media app we probably have a timeline. The timeline shows posts from users, so that’s how we know we need a Post object. Users need to identify themselves, for logging in, and to organise posts. Now we know we need a User object. (More about likes, later.)

Some more examples:

Once you know the types of objects you want to save in your app’s database, you can define the properties they have. The objects and properties together make up the so-called schema of your app database.

You’re most likely already familiar with the concept of “properties” – they are no different from Swift classes and Object-Oriented Programming.

We can define the properties of a Post object like this:

class Post {
    var text: String
    var image: URL
    var user: User
    var likes: [Like]
}

In the example above, we’ve defined a text property of type String and an image property of type URL. See how it’s just an ordinary Swift class with properties? Relational database objects are no different.

We’ll get to the user and likes properties next…

Defining Relationships Between Objects

In almost any app, database objects interact with each other. Posts are liked, users create posts, and comments are added to a post. When you design your app database these interactions are defined as relationships between objects.

There are 3 types of relationships:

Most relationships you define in your apps will be one-to-many. Let’s look at an example.

Here is the Post object again:

class Post {
    var likes:[Like]
}

And this is the Like object:

class Like {
    var post:Post
}

The Post object can have multiple likes, that’s why the likes property has the type “array of likes” or [Like]. Conversely, a single like can only be given to one post, evidenced by the property post on the Like object.

Interestingly, some many-to-many relationships are in fact just multiple one-to-many relationships! In a social media app, for example, many users can “follow” many other users. This is a many-to-many relationship, but it’s defined as two directional one-to-many relationships in your database. You commonly use a helper table/object to accomplish a many-to-many relation.

When User A follows User B, that User A is added to the followers property of User B. When User B follows back, it’s added to the followers property of User A. Conversely, when User A follows User B, the User A adds User B to its own following property (and vice versa).

By making these two one-to-many relationships, both users know who they are following and who follows them! You could say that a follower relationship is directional, i.e. when I follow you, that doesn’t mean you’re also following me back.

However, when you think of befriending someone it is different: when we’re friends, I am your friend, and you are my friend. This implies a many-to-many relationship.

Exciting, isn’t it? Make sure to keep the difference between one-to-many and many-to-many in mind, as it will save you a lot of headache in the future.

Reference: https://learnappmaking.com/app-database-design/