What is Cardinality in DBMS?

Cardinality at its most basic can be defined as the number of elements in a given set. In the specific case of databases, it refers to the number of lines or records in the record. Yet, when applied to the field of databases to define the cardinality of items within tables, it represents the number of occurrences of one entity that are linked to the other. Cardinality comes in here to define the types of rules and constraints eminent in these relationships.

Cardinality in Relationships

Every table in a relational database contains one or more keys to be used to relate it to other tables. Cardinality deploys the number of records in one table, that may or must be associated with records in another table. The relationships are categorized into four main types:

  1. One-to-One (1:1)
  2. One-to-Many (1:M)
  3. Many-to-One (M:1)
  4. Many-to-Many (M: N)

All of these relations have their own importance and usage in the design of the database. Now a little more on each type and how they work See sample below.

1. One-to-One Relationship (1:1)

In this type of relationship, one record in one of the tables will relate directly to only one record in another table. Although this kind of relationship is not common more often when handling large amounts of data this type of relationship is useful when handling sensitive data or when a large table is divided into more manageable parts.

Cardinality Example:

Suppose we are given two tables or entities named Person and Passport. Each record in the Person table has a passport number and no passport number is repeated for a different person.

Person Table

Person_IDNameAge
1John30
2Sarah25
3Emily28

Passport Table

Passport_IDPerson_IDIssue_Date
10112023-01-01
10222023-05-10
10332024-03-20

Here, every single person has a unique Passport, and vice versa there is one Passport linked to one Person only. This is a one-to-one relationship.

2. One-to-Many Relationship (1:M)

The one-to-many relationship is another type of relationship that is more popular than the other relationships. Here, one row in the first table has a number of rows in the second table, while in the second, each number will only have a row in the first table.

Cardinality Example:

Suppose, there is a Department table, and there is also an Employee Table. Every department can have multiple workers while every worker works in only one department.

Department Table

Department_IDDepartment_Name
1HR
2IT
3Marketing

Employee Table

Employee_IDNameDepartment_ID
101John1
102Sarah2
103Emily1
104James3

In this case, one-to-many relationships exist between the Department and Employee table where one Department has many employees but one employee can be only of one department.

3. Many-to-One Relationship (M:1)

The many-to-one relationship is almost the opposite of the one-to-many relationship that is defined above. Here, a number of records of the first table is linked to one record of the second table. In other words, there are many instances where several rows in the first table look to one row in the second table.

Cardinality Example:

For our purpose of the explanation let's assume we have an ‘Employee’ table and a ‘Department’ table. Here, it is possible to include many employees in a certain department, however, every employee is included in only one department.

Employee Table

Employee_IDNameDepartment_ID
101John1
102Sarah2
103Emily1
104James3

Department Table

Department_IDDepartment_Name
1HR
2IT
3Marketing

Here, more employees are allowed for one department e.g. John@HR and Emily@HR making it a many-to-one relationship as viewed from the table Employee.

4. Many-to-Many Relationship (M: N)

Related data records are of two types; in a one-to-one relationship where one record in the first table is related to one record in the second table and in a many-to-many relationship where many records can be related to many records. This kind of relationship needs a junction table to hold the connections.

Cardinality Example:

Let's use the Student table and the Course table as examples. Many students can participate in many courses while many courses can be participated by many students.

Student Table

Student_IDName
1John
2Sarah
3Emily

Course Table

Course_IDCourse_Name
101Math
102Science
103History

Enrollment Table (Junction Table)

Student_IDCourse_ID
1101
1102
2101
3103
3102

Here, one student may be registered for many courses while a course can accommodate many students. This means that there has to be a junction table- Enrollment table to represent the many-to-many relationship.

Importance of Cardinality in DBMS

Understanding cardinality is essential for effective database design. Here’s why:

  1. Efficient Data Retrieval: If we know the cardinality of the relationships we can design the queries, which will involve less number of joins and select the data more efficiently.
  2. Enforcing Data Integrity: Cardinality is used in defining constraints and helps in maintaining the technical integrity of relationships which in turn assists in inserting the right data into the database.
  3. Normalization: This cardinality normally is an important element in the process of database normalization, which is aimed at the elimination of as much redundancy and dependencies between the tables as possible with the help of correct translation of the database schema.
  4. Foreign Keys: Cardinality is used to describe the nature of the relationships when we are planning the relationships between tables using a foreign key. Foreign keys help to maintain the relationship between two tables through reference.