1

This question may be too easy to ask, but I can't understand how to do it.

I am new in SQL.

I have two tables.

Table I

book_id (AI & Primary Key)

book_name

book_year

Table II

writer_id (AI & Primary Key)

writer_name

writer_bio

I want to create a relation between book_id and writer_id. When I add a new book, I don't want to store the writer's name for the every single time, I want to connect it to a specific writer_id. That's how relational databases work, right?

A writer could be written more than one book, but a book also can be written by more than one writers. How can I make the relation?

I also appreciate any kind of link to understand how this works.

closed as off-topic by Mark Storey-Smith, RolandoMySQLDBA, Paul White, ypercubeᵀᴹ, RLF Dec 15 '14 at 14:43

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Too localized - this could be because your code has a typo, basic error, or is not relevant to most of our audience. Consider revising your question so that it appeals to a broader audience. As it stands, the question is unlikely to help other users (regarding typo questions, see this meta question for background)." – Paul White, ypercubeᵀᴹ, RLF
If this question can be reworded to fit the rules in the help center, please edit the question.

1

It seems to me that in a case like this you need a linking table that will allow for that many to many relationship. Something like

Create Table tblBookWriter
(tblBookWriter_ID int
, Book_id --(FK to Book table-Book_Id)
, Writer_id --(FK to Writer table-Writer_Id)
);

With this you can add have a book with as many writers as it needs and a writer can have authored as many books as is needed. Something like that should get it for you anyway.

If you do a web search for "database many to many linking table" you will see all kinds of results. The first is a wiki link for a Junction Table

  • Thanks for the answer. I don't need to use foreign keys if I apply this solution, do I? – salep Dec 12 '14 at 19:44
  • Absolutely. There should be foreign key links to both the book_id and the writer_id. I modified my answer to include reference to that. – mskinner Dec 12 '14 at 19:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.