Skip to content
Advertisement

Making sense of database table references with foreign and primary keys

I am relatively new to database logic, and am trying to figure out how multiple tables should/shouldn’t reference each other.

I have a table, ‘Book’, that should have columns:

  1. I want each book to have the possibility to have 1 or more genres.
  2. I want each book to have the possibility to have 1 or more authors.
  3. I want each book to have the possibility to have 1 or more purchase options (‘buyOption’).
    • and each purchase option (Amazon, Walmart, etc.) for each book has a unique url.

What I think makes sense (please correct me where I’m wrong):

Does it make sense to have the title table? If so, can i use its primary key to fill various other tables, as depicted?

If the ‘Buy Options’ table is going to have a bunch of different options and associated urls for each book, will it be possible to get the buyBrand and buyUrl directly from the main ‘Book’ table? In the end, I just want a giant table that I can grab cell data from. Right now I’m trying to figure out how to populate tables with my data, and what tables to fill for each piece of data.

(again, I’m new to database logic, so I apologize if my wording is hard to understand)

Advertisement

Answer

Your design does not look good. You are describing many-to-many relationships between books and genres, books and authors, books and options.

Storing references to the related genre, author, and option in the books table is not the right way to go: you can only store one related value per book (one genre, one author, one option), while you need many. Instead, for each of these relationships, you should have a separate table, called a bridge table, that references the associations.

On the other hand, information that is dependent on the book (say, the title) should be stored in the book table.

Here is one example for books and genres:

Now, say you want to list all books that belong to genre 'Sci-Fi'; you would go:

The same logic should be implemented for each and every many-to-many relationship in your schema.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement