Reader small image

You're reading from  Becoming a Salesforce Certified Technical Architect - Second Edition

Product typeBook
Published inJun 2023
PublisherPackt
ISBN-139781803239439
Edition2nd Edition
Right arrow
Author (1)
Tameem Bahri
Tameem Bahri
author image
Tameem Bahri

Tameem Bahri is the Global Engineering Director for the CRM platform at A.P. Moller - Maersk and was formerly the European Chief Technology Officer for the Salesforce COE at Capgemini. He is an experienced technology specialist with a demonstrated history of working in the information technology and services industry with over 20 years of experience across Business Transformation, Digital Services, Innovation, Process design and redesign, Enterprise System Security, Identity and Access Management Strategy (IAM), and Enterprise Solution Architecture. Tameem Bahri is a Salesforce Certified Technical Architect (CTA) and has led CTA coaching activities for years for companies such as Accenture and Capgemini. He is also a public speaker who participated in multiple events, including Dreamforce, the Data Innovation Forum for Salesforce Architects, and the Tunisia Digital Summit 2021.
Read more about Tameem Bahri

Right arrow

Using Database Relationships

One of the goals of the normalization process is to remove data redundancy. To do that, you must divide tables into multiple related subject-based tables. There are three main types of relationships—one-to-one, one-to-many, and many-to-many. Take a look at these in more detail:

  • One-to-one relationship: In this type of relationship, each record in the first table has one–and only one–linked record from the second table. And similarly, each record from the second table can have one–and only one–matching record from the first table. In this relationship, one of the tables is effectively considered an extension of the other. In many cases, the additional attributes will simply end up being added to one of the tables. That is why this relationship is not very common. However, it still has its valid cases, including splitting the data for security reasons.

In Salesforce, this is particularly useful considering the rich sharing model available. You can implement one-to-one relationships in many ways, with the most common being to create a lookup relationship from each object to the other, with an automation script (trigger, workflow, process builder, and others) to ensure both lookup fields are always populated with the right values and each object is linked to its counterpart:

This figure shows one-on-one relationships between two tables namely ‘Customer’ and ‘Customer details.’ The table ‘Customer’ lists ‘ID’, ‘Customer Details ID’, ‘First Name’, and ‘Last Name’. The table ‘Customer details’ lists ‘ID’, ‘Customer ID’, ‘Gender’, and ‘Social Status’.

Figure 2.12 – One-to-one relationship

  • One-to-many relationship: This common type of relationship occurs when a record in one table (sometimes referred to as a parent) is associated with multiple records of another table (sometimes referred to as a child). There are plenty of examples of this, including a customer placing multiple orders, an account with multiple addresses, and an order with multiple order line items.

To have this relationship, you would need to use what are known as primary keys and foreign keys. In the child table, the primary ID would be the unique identifier for each record in that table, while the foreign key would refer to the unique identifier of the parent’s primary key. This type of relationship can be created in Salesforce using lookup fields or master-detail fields. This book assumes you already know the differences between the two. You will also cover some particular differences that you need to remember in later chapters:

This figure shows an example of one-to-many relationships between two tables namely ‘Customer’ and ‘Customer details.’ The table ‘Customer’ lists ‘ID’, ‘First Name’, ‘Last Name’. The second table lists ‘ID’, ‘Customer ID’, ‘Order Total Amount’, ‘Order Date’, and ‘Order Status’.

Figure 2.13 – One-to-many relationship

  • Many-to-many relationship: This type of relationship occurs when multiple records from the first table can be associated with multiple records from the second table. This is also a common type of relationship that you come across every day. Think of the students and courses example, where each student can attend multiple courses, and each course can have multiple students attending it.

To create a many-to-many relationship between two tables, you need to create a third table, often called a bridge table or a junction table. This many-to-many relationship would effectively be broken into two one-to-many relationships. The junction table’s records would hold the value of two foreign keys, each pointing to a record from the two main tables.

Junction objects can be created in Salesforce by creating a custom object with two master-detail fields, or with two lookup fields (in this case, it is more often referred to as a bridge rather than a junction). Both approaches are valid and have their most suitable use cases.

You can even create a junction object using a mix of master-detail fields and a lookup field. This is assuming you put all the validation and business rules required in place to ensure they are always populated with the right values. You will come across examples of and use cases for Salesforce junction objects in later chapters:

This figure shows an example of many-to-many relationships between three tables namely ‘Student’, ‘Student Course’, and ‘Course’. The first table lists ‘ID’, ‘First Name’ and ‘Last Name’. The second table lists ‘ID’, ‘Student ID’, ‘Course ID’. The third table lists ‘ID’, ‘Course Name’.

Figure 2.14 – Many-to-many relationship

Keep in mind that the cardinality of these relationships can take different forms. For example, a one-to-many relationship might be better represented as one-to-zero-or-many if there are occurrences where the first table will have zero related records from the second table, as illustrated in the students and courses example earlier.

The following diagram illustrates a list of other relationships’ cardinalities. The one-and-only-one relationship could be a bit confusing. It is used when the relationship between two records from two different tables cannot be modified—for example, a user and a login ID. Once a login ID is associated with a user, it cannot be assigned to any other user after that. And it can be associated with one user only. This differs from the ownership relationship between a user and a mobile phone, for example, where the relationship itself might get updated in the future if the phone were to be sold to another owner:

This diagram illustrates a list of other relationships’ cardinalities, namely, ‘One’, ‘Many’, ‘Zero or One’, ‘One or Many’, ‘Zero or Many’, ‘One and Only One’.

Figure 2.15 – Table relationship cardinalities

Previous PageNext Page
You have been reading a chapter from
Becoming a Salesforce Certified Technical Architect - Second Edition
Published in: Jun 2023Publisher: PacktISBN-13: 9781803239439
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Tameem Bahri

Tameem Bahri is the Global Engineering Director for the CRM platform at A.P. Moller - Maersk and was formerly the European Chief Technology Officer for the Salesforce COE at Capgemini. He is an experienced technology specialist with a demonstrated history of working in the information technology and services industry with over 20 years of experience across Business Transformation, Digital Services, Innovation, Process design and redesign, Enterprise System Security, Identity and Access Management Strategy (IAM), and Enterprise Solution Architecture. Tameem Bahri is a Salesforce Certified Technical Architect (CTA) and has led CTA coaching activities for years for companies such as Accenture and Capgemini. He is also a public speaker who participated in multiple events, including Dreamforce, the Data Innovation Forum for Salesforce Architects, and the Tunisia Digital Summit 2021.
Read more about Tameem Bahri