Concepts / Database Normalization and Design Principles

Database Normalization and Design Principles

When related information is stored in separate tables, the database needs a reliable way to connect the rows.

  • Programming

Why Tables Need Connections

When related information is stored in separate tables, the database needs a reliable way to connect the rows. Imagine an Artist table containing artist names and eye color, alongside a Track table containing song titles and play counts. A reader can see the information in both tables, but still needs to know which track belongs to which artist. Primary keys and foreign keys provide that connection through integer values that act as references between tables.

Identifying Rows with Primary Keys

A primary key is a unique integer identifier for each row within a table. Its purpose is to distinguish one row from every other row in that same table. The conventional column name for a primary key is id. The name does not describe the subject-specific information in the row; it signals that the value serves as the row's identifier.

Artist tableid1id2id3identifiesidentifiesidentifies
How does a unique integer primary key distinguish one row from every other row in the same table?
Worked example

Reading a Primary Key

Suppose an Artist table contains three rows whose id values are 1, 2, and 3. What does the value 2 identify?

  1. 1
    Locate the table

    The value 2 is being interpreted within the Artist table.

  2. 2
    Match the identifier

    Find the row in that table whose id value is 2.

  3. 3
    Identify the row

    That matching row is the one uniquely identified by the primary key value 2.

Result

The integer 2 identifies one specific row in the Artist table. It is not merely an ordinary descriptive value; it is the row's unique identifier.

Following a Foreign Key

A foreign key is a column in one table that stores a primary-key value from another table. The stored integer creates a reference. To follow the reference, read the foreign-key value in the first table, move to the referenced table, and find the row whose primary-key value matches it.

Artist rowid = 7Track rowartist_id = 7artist_id references id
How are rows in two different tables connected through a foreign key and a referenced primary key?
artist_id7id5id7id9comparematchescompare
How does a foreign-key value in one table point to the matching primary-key row in another table?
Worked example

Tracing Track Ownership

A Track row contains artist_id = 7. The Artist table contains rows with id values 5, 7, and 9. Which Artist row is connected to the Track row?

  1. 1
    Read the foreign key

    The Track row stores the integer 7 in its artist_id column.

  2. 2
    Search the referenced table

    Look in the Artist table for a row whose primary-key column, id, contains 7.

  3. 3
    Follow the match

    The Artist row with id = 7 is the row referenced by the Track row.

Result

The Track row is linked to the Artist row whose primary key is 7.

Naming Relationships Clearly

Naming conventions make relationships explicit and self-documenting. A primary-key column is conventionally named id. A foreign-key column commonly combines the referenced table name with id, producing a form such as table_name_id. In the Artist and Track example, a column named artist_id communicates that the value refers to an Artist row.

Column roleCommon naming patternWhat the name communicates
Primary keyidThis value uniquely identifies a row in its table.
Foreign keytable_name_idThis value stores a primary-key value from the named table.

Conventional names for identifying and linking rows

Mistakes in Key Tracing

  • Common mistake

    Treating a foreign key as an unrelated number

    What it looks like

    A Track row has artist_id = 7, but the value is read without checking the Artist table.

    Why it fails

    The foreign key stores a primary-key value from another table, so its meaning comes from the matching row in that referenced table.

    Do this instead

    Use the foreign-key value to find the row whose primary-key column contains the same integer.

  • Common mistake

    Looking for a matching value in the wrong column

    What it looks like

    The value in artist_id is compared with an artist name instead of the Artist table's id column.

    Why it fails

    The relationship is defined by matching the foreign-key integer with a primary-key integer.

    Do this instead

    Compare the foreign key with the referenced table's id value.

  • Common mistake

    Assuming id is just a descriptive field

    What it looks like

    The id value is treated like an ordinary piece of information rather than the row identifier.

    Why it fails

    A primary key uniquely identifies the row within its table.

    Do this instead

    Interpret id as the conventional primary-key column and use its unique integer to distinguish rows.

  • Common mistake

    Ignoring the table named by a foreign-key convention

    What it looks like

    artist_id is read without noticing that its name points toward the Artist table.

    Why it fails

    The table_name_id convention helps make the intended relationship explicit and self-documenting.

    Do this instead

    Use the column name as a clue, then verify the relationship by matching its value to the referenced table's primary key.

Practice the Reference Path

EASY

A Track table contains a row with artist_id = 12. The Artist table contains rows with id values 4, 8, 12, and 15. Describe the exact path you would follow to identify the related Artist row.

Hints
  • Start with the integer stored in the Track row's artist_id column.
  • Search the Artist table's id column for the same integer.
  • The matching id identifies the related Artist row.

What do you think happens?

The Track row stores artist_id = 12, and the Artist table has id values 4, 8, 12, and 15. Which Artist primary-key value will the foreign key select?

  • 4

  • 8

  • 12

  • 15

Reveal answer
Answer:

12

A foreign key points to the referenced row by matching its integer value with the primary-key value in the other table.

Key Takeaways

  1. 1
    A primary key is a unique integer that identifies one row within a table.
  2. 2
    id is the conventional name for a primary-key column.
  3. 3
    A foreign key stores a primary-key value from another table.
  4. 4
    To trace a relationship, match the foreign-key integer with the referenced table's primary-key integer.
  5. 5
    Names such as artist_id make table relationships explicit and self-documenting.

At a glance

  • Primary keys uniquely identify rows within their own table.
  • The conventional primary-key column name is id.
  • Foreign keys connect tables by storing a primary-key value from another table.
  • Following a foreign key means finding the matching id in the referenced table.
  • Conventional names such as table_name_id make relationships easier to understand and support consistent database organization.

Related concepts

More concepts starting with D