Jump to content

Help with a query


KingSpongo

Recommended Posts

Hi everyone,

I have made two tables (Product and Customer). They look like:

 

2rf99w1.jpg

 

What I want to do is set Customer_ID in the Product table to a foreign key but have no clue how to do that.

I also want to make a query to show in a web page a table that looks like:

 

2czq3w8.jpg

 

If the product doesnt have a customer am i able to click on the empty customer name box, input a name and add it to the mysql database?

Everytime i refresh the page i want to see every product and if a product has a customer. I don't really want somone to do this all for me but maybe help me understand how to do it. I look forward to every reply.

 

Many thanks.  8)

 

Link to comment
Share on other sites

1st of all, you do use InnoDB to store your tables?

Otherwhise it makes no sence creating foreign keys

(http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-foreign-keys.html)

 

ALTER TABLE `product`

ADD CONSTRAINT FOREIGN KEY (`Customer_ID`)

REFERENCES (`Customer`.`Customer_ID`

(http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html)

 

but i'm no expert in foreign-keys, because i dont use InnoDB, and ......  :D

Link to comment
Share on other sites

  • 2 weeks later...

I suggest you create a new table that links both those tables together.  That way, you have only product information in a product table, only customer information in a customer table, and another table to determine who has what.  This also will provide the many-to-many relationship you will want. For example, it's possible that a customer may have more than one product.  Similarly, a product can be purchased by many customers.

 

[product]

product_id

product_name

product_description

 

[customer]

customer_id

firstname

lastname

email

 

[customer_product]

customer_product_id

customer_id

product_id

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.