Data Relationships

When looking at splitting large tables into smaller ones and creating relationships between those new tables it is important to understand the different data relationships available.

One-to-One (1:1)

A one to one relationship refers to 1 row in Table A being linked to 1 (and only 1) row, or no rows in Table B and vice versa. Imagine a company with a table of employees and a table of company mobile phones. Each employee is only assigned 1 mobile phone. Each mobile phone can only be assigned to 1 employee. However, not all employees are given a company mobile phone.

Employee Table EmployeeId FirstName Surname Int String String MobilePhone Table MobilePhoneId Make Model PhoneNumber AssignedToEmployeeId Int String String String Int

One-to-Many (1:N)

A one to many relationship refers to 1 row in Table A being linked to many rows in Table B and many rows in Table B linked to 1 row in table A. Imagine a company with a table of customers and a table of sales invoices. Each customer can have many sales invoices but each sales invoice can only be for one customer.

Customer Table CustomerId Name Int String Invoice Table InvoiceId CustomerId TotalExVAT TotalIncVAT Int Int Decimal Decimal

Many-to-Many (M:N)

A many to many relationship refers to many rows in Table A being linked to many rows in Table B and vice versa. Imagine a college with a table of students and a table of classes. Each class will have many students taking it and each student will take many classes. In order to implement a many to many relationship in a database a Bridge table sometimes referred to as a Linking table is used to split the many to many relationship into two one to many relationships.
Student TableStudentIdFirstNameSurnameIntStringStringClass TableClassIdClassNameIntStringBECOMESClass TableClassIdClassNameIntStringStudent TableStudentIdFirstNameSurnameIntStringStringStudent Classes TableStudentIdClassIdIntIntLinking Table

Self Referencing

A self referencing relationship refers to when a table relates to itself. Imagine an employee table with a column for manager ID that column refers back to the employee ID of the manager.

Employee Table EmployeeId FirstName Surname ManagerId Int String String Int