Author Topic: Help with a query  (Read 396 times)

0 Members and 1 Guest are viewing this topic.

Offline KingSpongoTopic starter

  • Irregular
  • Posts: 15
    • View Profile
Help with a query
« on: February 09, 2010, 03:27:06 PM »
Hi everyone,
I have made two tables (Product and Customer). They look like:



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:



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)

Offline jskywalker

  • Enthusiast
  • Posts: 289
  • Gender: Male
    • View Profile
Re: Help with a query
« Reply #1 on: February 10, 2010, 04:07:42 AM »
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)

Quote
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

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,444
  • Gender: Male
    • View Profile
Re: Help with a query
« Reply #2 on: February 16, 2010, 08:31:01 AM »
Where's the many-to-many table?
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline zeodragonzord

  • Enthusiast
  • Posts: 104
  • Gender: Male
    • View Profile
Re: Help with a query
« Reply #3 on: March 01, 2010, 05:08:23 PM »
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