Get started with relational databases, including normalization and associations, to build robust data storage solutions.
A relational database stores an item's data across multiple related tables, unlike key-value databases like DynamoDB that store all information for an item in a single table.
Here's an example of an Employees
table in a relational database:
id | leave | level | officeCode | officeName | dateJoined | state |
---|---|---|---|---|---|---|
dac041be | true | 5 | SEA24 | Fiona | 2016-07-23 | WA |
dac04434 | false | 6 | SEA41 | Day 1 | 2019-12-03 | WA |
dac045a6 | true | 4 | SEA32 | Nessie | 2015-09-15 | WA |
Unlike DynamoDB, adding a new column to an RDB table requires modifying the table schema and backfilling data, making careful upfront design important.
Normalization is the process of organizing data to minimize redundancy by dividing it into tables with each containing information about only one type of entity.
Redundant data not only takes up unnecessary space but causes three specific anomalies:
To normalize the employee/office example from the previous section, we would isolate employee data in one table and office data in another:
Employees (Normalized):
id | leave | level | dateJoined |
---|---|---|---|
dac041be | true | 5 | 2016-07-23 |
dac04434 | false | 6 | 2019-12-03 |
dac045a6 | true | 4 | 2015-09-15 |
Offices (Normalized):
id | name | state |
---|---|---|
SEA24 | Fiona | WA |
SEA41 | Day 1 | WA |
SEA32 | Nessie | WA |
EmployeeOffice (Relationship Table):
employee | office |
---|---|
dac041be | SEA24 |
dac04434 | SEA41 |
dac045a6 | SEA32 |
This approach allows adding new employees or offices independently and updates to office information only need to happen in one place.
Associations define relationships between tables in a relational database. The primary mechanism for creating associations is through foreign keys.
A foreign key is a column (or set of columns) in one table that refers to the primary key in another table. It enforces referential integrity, ensuring that relationships between tables remain consistent.
For example, in the EmployeeOffice
table above:
employee
column is a foreign key referencing the id
column in the Employees
tableoffice
column is a foreign key referencing the id
column in the Offices
tableReferential integrity enforces rules like: