views:

56

answers:

1

I have a table where I have to relate groups of primary keys to one another. There is no information about these groups other than that they exist. Specifically, I am storing groups of Editions of Books together, so I can know that a certain set of books are editions of each other.

I currently have a setup where I have an Edition_Group_ISBN column, where one of the ISBN's is arbitrarily chosen to group a set of Editions together.

The typical approach for this problem is to have a separate table like Book_Editions, where I would have an autoincrementing integer primary key like "Edition_Group_ID" linking ISBNs together. I have been told this method is preferable.

However, the problem with implementing that system relates to the loading in of data. How am I to dynamically load in Edition Groups? One solution might be to lock the table and do a transaction on the next ID in the autoincrement. I imagine this would be slower and more cumbersome than my current method, though.

Given the difficulty of inserting data under that system, what is the optimal system to address this problem?

+1  A: 

You load in Edition Groups by having an ISBN table foreign key in your Edition Groups table, and then inner-joining your two tables together in your query, using the Primary Key of your ISBN Books table, and the Foreign Key of your Edition Groups table in the join.

ISBN Table
    ISBN_ID                                    // Auto-incrementing Primary Key
    ISBN
    Book_Title
    .etc

EDITIONS Table
    Edition_ID
    FIRST_EDITION_ISBN_ID
    ASSOCIATED_ISBN_ID                         // Foreign Key to ISBN table

Most database systems have a way to return the Primary Key ID of a newly inserted record, so:

NEW_ID = INSERT INTO ISBN (ISBN, BOOK_TITLE) 
         VALUES (12345678, "The Frog Prince"); 
         SELECT SCOPE_IDENTITY();              // Returns new ID from ISBN table.

INSERT INTO EDITIONS (FIRST_EDITION_ISBN_ID, ASSOCIATED_ISBN_ID)
    VALUES (12345678, NEW_ID);
Robert Harvey
Can you give an example of the INSERT query?
babonk
In MySQL, the equivelant of SCOPE_IDENTITY() is LAST_INSERT_ID(). Would that function work to dynamically add data? Would I need to lock the table so no new ID's are isnerted between queries? Not sure how to setup the query...
babonk
Also, one question about your table setup. Why is Edition_ID necessary? Why not just make (FIRST_EDITION_ISBN_ID and ASSOCIATED_ISBN_ID) the primary key?
babonk
Edition_ID is there because it's good practice to put a Primary Key in every table, and I dislike composite primary keys (how do you join them to other tables?). But, to each his own.
Robert Harvey
You don't need to lock any tables; you just need a reliable way to get the new ID into a variable, so that it is stable (`LAST_INSERT_ID()` sounds like the correct way to do it in MySQL). Your MySQL documentation will tell you how to do this safely. Once you have that, you can run the second INSERT.
Robert Harvey