Search This Blog

Wednesday, September 23, 2009

SQL, Many-to-Many Relationship

To implement many-to-many relationship, we first need a junction table. The schema of this table has at least two column: primary key both original tables which have many-to-many relationship with. In short,

1. Create a junction table
2. Put primary key from both table into this new table

Now the junction table became the Many side of the other two tables. And the primary key of the junction table is the 2 primary key combine from the other two tables.

Movie_table
movie_id movie
1........Titanic
2........Aliens


Category_table
category_id category
1...........Love Story
2...........Adventure
3...........Drama
4...........Sci Fi
5...........Horror


Movie_Category_table
movie_id category_id
1.........1
1.........2
1.........3
2.........4
2.........5


http://www.webmasterworld.com/databases_sql_mysql/3749320.htm

No comments: