Skip to content
Home » What is Cardinality in Databases?

What is Cardinality in Databases?

Understanding Cardinality in Databases

Cardinality, in the context of databases, is a critical concept that has two primary applications: it pertains to the uniqueness of data elements within a column, and it describes the nature of relationships between different tables.

Column Cardinality

Column Cardinality refers to the number of unique values present in a specific column of a table. For instance, in a ‘Students’ table, the ‘Student_ID’ column would ideally exhibit high cardinality, as each student should have a unique ID. On the other hand, a column like ‘Major’ might display lower cardinality, as multiple students could be pursuing the same major.

Relationship Cardinality

Relationship Cardinality relates to the type of relationship existing between tables. There are four cardinality types:

One-to-One (1:1)

Each entry in Table A corresponds to exactly one entry in Table B, and vice versa. An example could be a table of employees and a table of employee social security numbers.

One-to-Many (1:N)

Each entry in Table A can correspond to multiple entries in Table B, but each entry in Table B corresponds to only one entry in Table A. An example could be a table of authors and a table of books they’ve written.

Many-to-One (N:1)

This is the inverse of a One-to-Many relationship. Each entry in Table A corresponds to one entry in Table B, but each entry in Table B can correspond to multiple entries in Table A. Using the previous example, each book corresponds to one author, but each author can have multiple books.

Many-to-Many (N:M)

Each entry in Table A can correspond to multiple entries in Table B, and vice versa. An example could be a table of students and a table of courses where each student can enroll in multiple courses, and each course can have multiple students. This type of relationship typically necessitates a junction table to manage the associations.

Understanding cardinality is vital in database design and normalization, as it ensures data integrity and efficiency.

Notations

While diagraming an Entity Relationship diagram, the following symbols are used to indicate the cardinality of the relationships involved. 

Cardinality’s Impact on Index Performance

Indices are used to expedite data retrieval in a database. High cardinality columns (those with many unique values) are excellent candidates for indexing, as the index can significantly speed up queries and improve database performance. For example, a ‘Student_ID’ column with unique values for each student would benefit greatly from an index.

Conversely, low cardinality columns (those with few unique values) may not benefit as much from indexing. In fact, indexing such columns could potentially degrade performance. This is because the database may end up scanning almost as many index entries as there are rows in the table. For instance, a ‘Gender’ column with only ‘Male’ and ‘Female’ values would not be a good candidate for an index.

Therefore, understanding cardinality is not only crucial for database design but also for optimizing database performance through effective indexing.