Solved

how to structure one to many in mysql with php

Posted on 2011-02-27
5
405 Views
Last Modified: 2012-05-11
Hi.
Need to get some advice on the best way to store/structure (one to many) in mysql using php.
I have a products table  like this:
p_id  p_price  p_name  p_supplier  

I created a seperate table to store category names:
id_auto_increment    category_name  

my problem is with the category,  a product can be assign in to many categories so what is the best way and how to store the products in the categories?
0
Comment
Question by:ikon32
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 22

Expert Comment

by:Thomasian
ID: 34994149
That is actually a many to many relationship since each product can be in many categories and each category can have many products.

Products
----------------------------------------
p_id *PK
p_price
p_name
p_supplier  


Categories
----------------------------------------
c_id *PK
category_name


ProductCategories
----------------------------------------
p_id *PK (fk to Products)
c_id *PK (fk to Categories)
0
 

Author Comment

by:ikon32
ID: 34994184
could you pls explain what  *PK and fk, stands for
0
 
LVL 22

Accepted Solution

by:
Thomasian earned 500 total points
ID: 34994210
PK stands for primary key. It's the unique id of a record in a table.
http://www.1keydata.com/sql/sql-primary-key.html

FK stands for foreign key. It's the field used to relate to another table.
http://www.1keydata.com/sql/sql-foreign-key.html

e.g.
Products
----------------------------------------
p_id     p_name
1        Product1   
2        Product2


Categories
----------------------------------------
c_id     category_name
1        Category1
2        Category2


ProductCategories
----------------------------------------
p_id     c_id
1        1     ->Product1 is assigned to Category 1
1        2     ->Product1 is assigned to Category 2
2        2     ->Product2 is assigned to Category 2

Open in new window

0
 
LVL 9

Expert Comment

by:rfportilla
ID: 34994218
*PK: primary key

When you setup the primary key in the ProductCategories table, you will set up two fields as the primary key.  This guarantees that the combination of the two fields will be unique.  

You will have a one-to-many relationship between the Products table and hte ProductCategories table, respectively.  You will also have a one-to-many relationship between the Categories table and hte ProductCategories table.  This will give you the many-to-many relationship that you are looking for.  

If you need more info, google junction tables or mapping tables.

Hope this helps.
0
 

Author Comment

by:ikon32
ID: 34994232
thanks  rfportilla, will do
0

Featured Post

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword This article was written many years ago, in the days when PHP supported the MySQL extension (http://php.net/manual/en/function.mysql-connect.php).  Today (http://php.net/manual/en/migration70.removed-exts-sapis.php) you would not use MySQL…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question