DataBase design: one to many database?

主宰稳场 提交于 2019-12-25 04:17:32

问题


I am very new to database designing.

I have a doubt. The question is very basic. But please help me out. i will try to expain it through an example.

Suppose, I got books in one table and their authors in the other(assuming that one book is written by just one author(one to many) and one author can write many books(many to one)). I am not getting how exactly to link the tables and what should be auto-incremented?

tblBooks     //Table 1 contains two entities
    {
     bookId      // This field is auto-incremented
     bookName
    }


tblAuthors    //Table 2
    {
     authorId    // Should this field also be auto-incremented?
     authorName
     Field3      // What should be the 'Field3' then which acts as link between both the tables?
                 // What exactly is foreign key? Here 'Field3' would be foreign key or what?

    }   

Help appreciated


回答1:


The "Many" table gets a foreign key of the "One" table.

tblBooks {
    bookId   
    bookName
    authorId
}
tblAuthors {
    authorId  
    authorName
}  

Example queries

//Grabs EVERY book that was made by an author with id 1
SELECT * FROM tblBooks where authorId='1' 

//Grabs the author of the book with id 223
SELECT * FROM tblAuthors where authorId=(SELECT authorId FROM tblBooks WHERE bookId='223')

//Joins the tables so each book will show its author
SELECT 
    tblBooks.bookId,
    tblBooks.bookName,
    tblAuthors.authorName
    tblAuthors.authorId 
FROM 
    tblBooks 
JOIN 
    tblAuthors 
ON 
    tblBooks.authorId=tblAuthors.authorId

The syntax will probably change depending on what database you use (mysql,oracle,sqlite etc) but thats the basic structure.

And if you decide to go with a many-to-many structure you can do a couple things, one create a third table that works to link both tables e.g. Books that have many authors:

tblBooks {
    bookId
    bookName
}

tblAuthors {
    authorId
    authorName
}

tblBookAuthors {
    bookId
    authorId
}

OR have a field in one of the tables that has a comma seperated string of author ids:

tblBooks {
    bookId
    bookName
    authorIds
}

tblAuthor {
    authorId
    authorName
}

authorIds would be like 1,12,32, in this case you would have to use a database function to select the authors IN that set, for example MYSQL has find_in_set(tblAuthors.authorId,tblBooks.authorIds) where the first argument is the search and the second is the set of data you are searching

And the way to decide which table in the many-to-many structure gets the field with the comma separated ids is the table where the foreign ids dont get added removed often, for example the authors dont usually get removed or added to a book so it gets the list field.




回答2:


@Patrick evans is correct. Linking field is in child table. To lay it out for you,

tblBooks     //Table 1 contains Books
{
 bookId     Primary Key // This field is auto-incremented
 bookName
 AuthorId   Foreign Key constraint references tblAuthors.AuthorId
}


tblAuthors    //Table 2
{
 authorId,  Primary Key // this field can also be auto-incremented
 authorName
}



回答3:


Field3 could be placed in tblBooks and called, for instance, authorId defined as a foreign key. You can add a constraint like this

ALTER TABLE tblBooks
ADD CONSTRAINT fk_author
FOREIGN KEY (authorId)
REFERENCES tblAuthors(authorId)

In this case bookId should not be unique, because there will be several inputs in table tblBooks with the same authorId.

By the way, a foreign key is a field (or collection of fields) in one table that uniquely identifies a row of another table.



来源:https://stackoverflow.com/questions/16923599/database-design-one-to-many-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!