Results 1 to 11 of 11

Thread: [RESOLVED] Retrieving three author names, two may be empty

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    Resolved [RESOLVED] Retrieving three author names, two may be empty

    Hello,
    I have a books table which apart from other fields has fields author1, author2, author3 which refer to the id in authors table. author2 and author3 may be 0, which means the book has only 1 author. Issue is how to retrieve author names for author2 and author3 only if they are not 0?
    First retrieving the author1, author2, author3 id's and then retrieving their names if they are not 0 is something I can do from php.
    I wanted to know if it can be done in a single sql statement. Or is there a better way of organizing the data?
    I am using MySQL 5.0
    Thank you.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Retrieving three author names, two may be empty

    It appears to be a very strange design...
    I think it would be better if you only have ONE AuthorID in your Books table.
    The combination of BookID+AutorID is unique so you can easily link Books and Authors tables on AuthorID field.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    Re: Retrieving three author names, two may be empty

    I couldn't understand the third line. I didn't mention that a book can have more than one author and an author can have more than one book. That being the case how can I have more than one author for a single book with just one authorid field. I do not have any entries for the bookid in the authors table.

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Retrieving three author names, two may be empty

    The author field would not be in the book table. You would have a book table and an Authors table. There would be another table AuthorToBook that would hold the Primary Keys from each table for each each auther to a book. This way a author is only entered into the system once. You can have more then one auther per book and an author can be assigned to more then one book. This is known as a Many-To-Many relationship
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    Re: Retrieving three author names, two may be empty

    And AuthorToBook will be something like this
    ----------------------------------
    id - primary key
    bookid - references book id
    authorid - references author id
    ----------------------------------
    If a book has more than one author it will have more than one entry in this table.
    Correct me if I am wrong.

  6. #6
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Retrieving three author names, two may be empty

    That is correct. There will always be one combination for every book but there can be many.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Retrieving three author names, two may be empty

    Just to argue the other side of this

    If you were to look at all the books you were going to load and found that 97% of them had only one author. And maybe 2% of the remaining had 2 authors and that only 1% ever had 3 authors...

    Then this would be a place were you could consider ignoring 3rd normal form.

    I'm certainly not saying this is the place to ignore the rule...

    Just stating what I sometimes look at before deciding it's time to make a child table and increasing the complexity of joins...

    If you always want the 1, 2 or 3 authors on the same row as the book then it's going to be much harder to do that with a query.

    But the other argument to use 3rd normal form is that you will not be able to easily find all books written by "author xyz" if you have 3 fields in the book table.

    As to your original question it's very easy to get the author names based on your original design

    Code:
    Select BookName,A1.AuthorName,A2.AuthorName,A3.AuthorName From Books BK
     Left Join Authors A1 on A1.AuthorId=BK.AuthorId1
     Left Join Authors A2 on A2.AuthorId=BK.AuthorId2
     Left Join Authors A3 on A3.AuthorId=BK.AuthorId3
    If the BK.Author# field is a 0 it won't find a relationship in the Authors table - thus returning a NULL name.

    ________________________________________________________________

    Quote Originally Posted by srisa
    And AuthorToBook will be something like this
    ----------------------------------
    id - primary key
    bookid - references book id
    authorid - references author id
    ----------------------------------
    If a book has more than one author it will have more than one entry in this table.
    Correct me if I am wrong.
    If you go this route there is no need for an ID field - the BOOKID and AUTHORID are two fields that can be made into a "composite primary key". This works fine - and has the added benefit of forcing only one row per book/author combination to ever be allowed.

    There is no benefit to the ID field - it doesn't supply a purpose in my opinion.
    Last edited by szlamany; Nov 29th, 2007 at 05:06 PM.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Retrieving three author names, two may be empty

    Quote Originally Posted by szlamany
    As to your original question it's very easy to get the author names based on your original design
    But it's bad and I don't know if it can get any worst...


    Quote Originally Posted by szlamany
    ... the BOOKID and AUTHORID are two fields that can be made into a "composite primary key". This works fine - and has the added benefit of forcing only one row per book/author combination to ever be allowed...
    We already mentioned that but in fewer words of course.

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Retrieving three author names, two may be empty

    Quote Originally Posted by RhinoBull
    We already mentioned that but in fewer words of course.
    But those fewer words weren't being listened to...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Feb 2006
    Location
    Hyderabad, India
    Posts
    233

    [RESOLVED]Re: Retrieving three author names, two may be empty

    I learned a lot from this. Hopefully I won't forget it. The id field is sort of thumb rule for me, it does the job most of the time, but now I realize it is not always so.
    Thanks to all of you.

  11. #11
    Addicted Member
    Join Date
    Mar 2006
    Posts
    235

    Re: [RESOLVED] Retrieving three author names, two may be empty

    You really don't need to have an id field. If you make the authorBook table primary key both bookid and authorid .

    Code:
    bookid - references book id
    authorid - references author id

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width