Concepts / Creating and Querying Relationships Between Tables

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.

  • Programming

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.

matches IDmatches IDTrack row 1The BeatlesArtist rowThe BeatlesTrack row 2The BeatlesTrack row 1artist_id 1Track row 2artist_id 1
What changes when the same artist name is stored repeatedly compared with storing it once and referencing it by number?

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.

matching numeric IDTrack row 5artist_id = 3Artist row 3matching artist
How does a numeric key in one table identify the matching row in another table?

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.

matches ID 1matches ID 1Let It Beartist name: The BeatlesArtist rowThe BeatlesAnother trackartist name: The BeatlesLet It Beartist_id = 1Another trackartist_id = 1
What changes when repeated artist values are moved from the Track table into a separate Artist table?

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.

provides artist_idprovides matching IDconnects related dataTrack tableLet It Be, artist_id 1Match ID 1Track artist_id to ArtistIDCombined resultLet It Be, The BeatlesArtist tableID 1, The Beatles
How does information move from two related tables into one result when the tables are queried together?

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

EASY

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

  1. Normalization avoids repeating the same string data in a column.
  2. Repeated information can be moved into a separate table and stored once.
  3. Numeric IDs replace repeated strings and identify matching rows in another table.
  4. A relationship connects rows across tables when their numeric IDs match.
  5. 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.