Creating and Querying Relationships Between Tables
The fundamental rule of database normalization is to never repeat the same string data in a column more than once.
The Repetition Problem
Imagine a music database with a Track table. A straightforward design might store the artist's full name in every track row. If several tracks belong to The Beatles, the text The Beatles appears repeatedly in the same column. This looks convenient at first, because the artist name is visible beside each track, but it creates redundant data and violates the fundamental rule of database normalization: do not repeat the same string data in a column more than once.
Normalization is the process of recognizing repeated data, moving that data into a separate table, storing it once, and replacing repeated strings with numeric references.
The Numeric-Key Design
The normalized design separates artist information from track information. The Artist table contains one row for each artist and gives that artist a unique numeric ID. The Track table does not repeat the artist's full name. Instead, each track stores the artist's numeric ID. The number is the reference that connects the track row to the corresponding artist row.
A relationship is the connection between rows in different tables created by matching numeric IDs. The ID stored in one table points to the row with the same ID in another table.
Splitting the Tables
Moving artist information out of Track
A music database needs to record tracks and their artists without repeating an artist name in the Track table.
Identify the repeated value: The artist name is string data that can appear in multiple track rows.
Create the separate table: Create an Artist table and give each artist one row with a unique numeric ID.
Replace the repeated text: Store the artist's numeric ID in each related Track row instead of storing the full artist name there.
Follow the relationship: To find the artist for a track, match the Track row's artist_id with the ID in the Artist table.
The artist information is stored once, while multiple track rows can refer to it through the same numeric ID.
Following a Relationship
A related-table query starts with data in one table and uses its numeric key to find matching data in another table. For example, a Track row for Let It Be can contain artist_id set to 1. The database follows that value to the Artist row whose ID is 1, where it finds The Beatles. The resulting view can show the track and artist together even though the artist name is stored only in the Artist table.
The relationship does not require copying the artist name into the Track table. The numeric ID is enough to locate the artist row when the related information is needed.
Benefits Beyond Storage
Separating repeated data makes updates simpler. If an artist changes their name, the normalized design requires one update in the Artist table. Every track that refers to that artist ID then uses the updated artist information. In a design that repeats the artist name in every Track row, each related row would need to be found and updated, creating a risk that some rows would be missed or would contain inconsistent text.
Normalization can also save storage space. Storing The Beatles once and referring to it with numeric IDs uses less space than storing the same string many times. This becomes more significant as a database grows to millions of rows. The separated structure also makes the data easier to query and maintain because each piece of information has a clearer home.
Mistakes in Table Design
Repeating the full artist name in every Track row
The same string is repeated, which creates redundancy and makes later updates harder to keep consistent.
Fix:
Store the artist once in an Artist table and place its numeric ID in the related Track rows.Treating the numeric ID as unrelated data
The relationship exists because the numeric ID matches a row in another table. Without using the match, the reference cannot connect the related information.
Fix:
Match the ID in the Track row with the corresponding ID in the Artist table.Updating only one repeated string
Repeated copies can become inconsistent.
Fix:
Keep the artist name in one Artist row so the artist information is updated in one place.
Apply the Pattern
A Track table contains several rows by the same artist. Describe how you would redesign the tables so the artist's name is stored once, explain what value belongs in each Track row, and describe how a query could recover the artist name for a selected track.
Hints
- First identify the string that is repeated.
- Create a separate table with one row for the artist and a unique numeric ID.
- Replace each repeated artist name in Track with that numeric ID.
- Follow the matching ID from Track to Artist when producing the result.
Key Takeaways
- Normalization avoids repeating the same string data in a column.
- Repeated information can be moved into a separate table and stored once.
- Numeric IDs replace repeated strings and identify matching rows in another table.
- A relationship connects rows across tables when their numeric IDs match.
- Normalized designs simplify updates, reduce storage needs, and help prevent inconsistent data.
Key Takeaways
- Do not repeat the same string data in a column when it represents one shared fact.
- Store shared data in its own table and assign it a unique numeric ID.
- Place the numeric ID in related rows instead of repeating the full string.
- A matching numeric ID creates the relationship between rows in separate tables.
- Queries can follow that relationship to present related information together.