I have a MySQL table with customer numbers, customer names, and item numbers. It's a little more complex than this, but I tried to simplify it a little and remove sensitive data.
CURRENT TABLE
Unique Customer Number | Customer Name | Item Number
10001 | Bob | 20001
10001 | Bob | 20002
10001 | Bob | 20003
10002 | Tim | 20002
10003 | Frank | 20001
10003 | Frank | 20003
I want to break this into a couple of tables so that it's easier to work with. I was thinking of something similar to this:
CUSTOMER TABLE
Unique Customer Number | Customer Name
10001 | Bob
10002 | Tim
10003 | Frank
ITEM TABLE
Unique Customer Number | 20001 | 20002 | 20003
10001 | YES | YES | YES
10002 | NO 开发者_Go百科 | YES | NO
10003 | YES | NO | YES
Is there a better way to organize this? How would I write the PHP code to separate my CURRENT TABLE into my new CUSTOMER / ITEM TABLEs?
You want to use a junction table to resolve the many-to-many relationship between customers and items. Something like:
Eww ... items as columns ... very bad idea ... you could potentially end up with 50+ columns for basically nothing ...
Customers ( CustomerNumber, CustomerName, ... )
Items ( ItemNumber, ItemDescription, ... )
CustomerItemMapping ( CustomerNumber, ItemNumber )
Where the CustomerItemMapping both fields are a Foreign Key to the corresponding table values (i.e. CustomerNumber maps to the Customers.CustomerNumber field).
精彩评论