Concepts / Creating and Querying Relationships Between Tables

Creating and Querying Relationships Between Tables

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.

  • 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.

Definition

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.

Track row 1The BeatlesArtist rowThe BeatlesTrack row 2The BeatlesTrack row 1artist_id 1Track row 2artist_id 1matches IDmatches ID
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.

Track row 5artist_id = 3Artist row 3matching artistmatching numeric ID
How does a numeric key in one table identify the matching row in another table?
Key point

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

Worked example

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.

  1. 1
    Identify the repeated value

    The artist name is string data that can appear in multiple track rows.

  2. 2
    Create the separate table

    Create an Artist table and give each artist one row with a unique numeric ID.

  3. 3
    Replace the repeated text

    Store the artist's numeric ID in each related Track row instead of storing the full artist name there.

  4. 4
    Follow the relationship

    To find the artist for a track, match the Track row's artist_id with the ID in the Artist table.

Result

The artist information is stored once, while multiple track rows can refer to it through the same numeric ID.

Let It Beartist name: The BeatlesArtist rowThe BeatlesAnother trackartist name: The BeatlesLet It Beartist_id = 1Another trackartist_id = 1matches ID 1matches 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.

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

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

  • Common mistake

    Repeating the full artist name in every Track row

    What it looks like

    Several rows each store The Beatles in the artist column.

    Why it fails

    The same string is repeated, which creates redundancy and makes later updates harder to keep consistent.

    Do this instead

    Store the artist once in an Artist table and place its numeric ID in the related Track rows.

  • Common mistake

    Treating the numeric ID as unrelated data

    What it looks like

    A Track row contains artist_id = 1, but the design does not use that value to find an Artist row.

    Why it fails

    The relationship exists because the numeric ID matches a row in another table. Without using the match, the reference cannot connect the related information.

    Do this instead

    Match the ID in the Track row with the corresponding ID in the Artist table.

  • Common mistake

    Updating only one repeated string

    What it looks like

    One Track row is changed after an artist changes their name, while other rows still contain the old name.

    Why it fails

    Repeated copies can become inconsistent.

    Do this instead

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

At a glance

  • 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.

Related concepts

More concepts starting with C