4. Use the populate_product_category.sql script from the code download to populate the
product_category table with sample data.
How It Works: Many-to-Many Relationships
Many-to-many relationships are created by adding a third table, called a junction table, which is named product_
category in this case. This table contains product_id and category_id pairs, and each record in the table
associates a particular product with a particular category. So, if you see a record such as (1, 4) in product_
category, you know that the product with the ID of 1 belongs to the category with the ID of 4.
This is also the first time that you set a primary key consisting of more than one column. The primary key of
product_category is formed by both its fields: product_id and category_id. This means that you won??™t be
allowed to have two identical (product_id, category_id) pairs in the table. However, it??™s perfectly legal to
have a product_id or category_id appear more than once, as long as it is part of a unique (product_id,
category_id) pair. This makes sense because you don??™t want to have two identical records in the product_
category table. A product can be associated with a particular category or not; it cannot be associated with a
category multiple times.
Using Database Diagrams
All the theory about table relationships can be a bit confusing at first, but you??™ll get used to it.
Pages:
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216