Home > Ask the Oracle Database / Applications Experts > Questions & Answers > Books and authors, many-to-many
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Books and authors, many-to-many

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site
>
QUESTION POSED ON: 07 August 2002
A question in two parts! I am attempting to design a database for a library catalogue with a fairly simple search strategy. I was wondering how you overcome the problem of a many to many relationship, e.g. Book Titles and Authors. Books can have many authors and authors can write more than one book. Do you have to create another entity with authorID and booktitleID? My second question is, can an entity have more than one foreign key? Help!!

>

Yes, a third table, the BookAuthors table, which I call an intersection or relationship table, is required.

Consider the following books:

SQL: 1999 - Understanding Relational Language Components
by Jim Melton, Alan R. Simon, Jim Gray
Understanding Sql's Stored Procedures: A Complete Guide to Sql/Psm
by Jim Melton
Database: Principles, Programming, and Performance, Second Edition
by Patrick O'Neil, Elizabeth O'Neil, Jim Gray
Lan Times Guide to Sql
by James R. Groff, Paul N. Weinberg
SQL: The Complete Reference
by James R. Groff, Paul N. Weinberg

If these books were stored in the database, they might look like this:

Books

IDBookTitle
17SQL: 1999 - Understanding Relational Language Components
23Understanding Sql's Stored Procedures: A Complete Guide to Sql/Psm
45Database: Principles, Programming, and Performance, Second Edition
65Lan Times Guide to Sql
81SQL: The Complete Reference

Authors

IDAuthor
204Jim Melton
211Alan R. Simon
243Jim Gray
255Patrick O'Neil
278Elizabeth O'Neil
280James R. Groff
291Paul N. Weinberg

BookAuthors

BookIDAuthorID
17204
17211
17243
23204
45255
45278
45243
65280
65291
81280
81291

For this design, you will most likely require INNER JOIN queries only, unless you're looking for books that don't have an author or authors that don't have a book, which you might do occasionally to ensure that the contents of the tables have been entered properly.

As for your second question, yes, an entity can definitely have more than one foreign key. The BookAuthors table has two. BookID is a foreign key to the ID column in Books, and AuthorID is a foreign key to the ID column in Authors. The database will not get confused when you declare these foreign keys, because you have to specify which table the particular ID belongs to.

You didn't ask, but there's also the question of primary keys. What should the primary key of BookAuthors be? My recommendation is to make the pair of columns, BookID and AuthorID, the primary key for BookAuthors -- i.e., a compound primary key. This will ensure that you don't enter the same author for the same book more than once.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Oracle White Papers: Fusion Middleware
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts