A many-to-many relationship occurs when multiple records in a table are related to multiple records in another table. When normalizing a database or adding tables to an existing database, we need to be able to relate tables to each other. There are three ways in which a table can be related to another table:
Handling a one-to-one relationship or a one-or-many relationship can be done by adding the primary key of one table into the other table as a foreign key. However, for many-to-many relationships, it’s a bit different. Let’s have a look at an example. Many-to-Many Relationships: An ExampleLet’s say we are creating a database for a university (which is an example I’ve used often). We capture details about students who attend classes, among other things. The rules are:
This means a student has many classes, and a class has many students. We can’t add the primary key of one table into the other, or both, because this only stores a single relationship, and we need many. We couldn’t do this:
This would mean we have one column for storing multiple values, which is very hard for maintenance and querying. We also couldn’t have many columns for class ID values, as this would get messy and create a limit on the number of relationships.
Many-to-Many RelationshipsSo how do we capture this? We use a concept called a joining table or a bridging table. A joining table is a table that sits between the two other tables of a many-to-many relationship. Its purpose is to store a record for each of the combinations of these other two tables. It might seem like a bit of work to create, but it’s simple to do and provides a much better data structure. To create one for this example, we can create a new table called class_enrolment. Now, the name of the table is important. It’s good to be descriptive with the table. I’ve seen joining tables named with the two names of the other two tables together (such as student_class). I think this is valid and will get the job done, but having a more descriptive name is helpful, as it tells you more about what the table is. So, we have a new table called class_enrollment. It stores two columns: one for each of the primary keys from the other table. Our table would look like this:
This stores separate records for each combination of student and class. Our student and class tables remain the same: Student:
Class:
Having our data structure in this way makes it easier to add more relationships between tables and to update our students and classes without impacting the relationships between them. Let's say you have two databases for your theoretical business 😉
You want to know both which customers bought which items, as well as which items were purchased by which customers. This is a job for relations!
To relate two databases, you need two databases. So let's assume you've made the Customers and Items databases above for the purpose of this walkthrough.
Tip: To change the database you're connecting to, re-select Relation as the property type for that particularly property. You'll be prompted to choose a new database. Relations are created as one-way by default. But you can easily toggle on a corresponding relation in the destination database. With two-way relations, the edits work both ways! So if you add a customer to the Items database in the relation column, the change pops up in your Customers database.
When you create a relation, you're essentially adding Notion pages stored in one database into the property field of another.
Let's say you want items in the same database to have relationships with each other. For example, you have a tasks database and you want each task to relate to other tasks.
Just as with relations between two databases, you can choose to make this a one-way relation or two-way relation.
Rollups help you aggregate data in your databases based on relations. Going back to our customers and items example above, let's say you wanted to know how much each customer spent based on what they bought.
There are 17 different calculations you can apply as a rollup. Here are all of them:
These rollup calculations are only available for Number properties:
These rollup calculations are only available for Date properties:
In both tables and boards, you can apply calculations to your rollup column to get a sense of sums, ranges, averages, etc. for your entire database.
|