组织 MySQL 客户/数据表
我有一个 MySQL 表,其中包含客户编号、客户名称和商品编号。它比这更复杂一些,但我尝试稍微简化它并删除敏感数据。
当前表
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
我想将其分成几个表,以便更容易使用。我正在考虑类似的事情:
客户表
Unique Customer Number | Customer Name
10001 | Bob
10002 | Tim
10003 | Frank
项目表
Unique Customer Number | 20001 | 20002 | 20003
10001 | YES | YES | YES
10002 | NO | YES | NO
10003 | YES | NO | YES
有更好的方法来组织这个吗? 我将如何编写 PHP 代码来将我的当前表分离到我的新客户/项目表中?
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 | 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您希望使用联结表来解决客户和项目之间的多对多关系。类似于:
You want to use a junction table to resolve the many-to-many relationship between customers and items. Something like:
Eww ...项目作为列...非常糟糕的主意...您可能最终会得到 50 多个列,而基本上什么都没有...
其中 CustomerItemMapping 两个字段都是相应表值的外键(即 CustomerNumber 映射到Customers.CustomerNumber 字段)。
Eww ... items as columns ... very bad idea ... you could potentially end up with 50+ columns for basically nothing ...
Where the CustomerItemMapping both fields are a Foreign Key to the corresponding table values (i.e. CustomerNumber maps to the Customers.CustomerNumber field).