简体   繁体   English

如何设计用于存储销售物品的 MySQL 数据库

[英]How to design a MySQL database for storing sell items

I have been trying to design a MySQL table o store the items of the store purchased by the costumers.我一直在尝试设计一个 MySQL 表来存放客户购买的商店物品。 I am stuck with what approach should I take to design a good table.我不知道应该采取什么方法来设计一张好桌子。

My first option is:我的第一个选择是:

id ID bill_id_fk bill_id_fk item1_id item1_id item2_id item2_id item3_id item3_id item4_id item4_id

In this approach, I'll create may be 20 columns for items (assuming that a costumer may buy a maximum of 20 items at a time).在这种方法中,我将为项目创建可能是 20 列(假设客户一次最多可以购买 20 个项目)。 ID of the items will be stored in the item(n)_id columns for that specific bill_id_fk .项目的 ID 将存储在该特定bill_id_fkitem(n)_id列中。 My concern with this approach is that it would be difficult to query later for a specific item, like how many times a specific item has been sold.我对这种方法的担忧是以后很难查询特定商品,例如特定商品已售出多少次。

My second opinion is:我的第二个意见是:

id ID bill_id_fk bill_id_fk item_id item_id
1 1 1 1 23 23
2 2 1 1 29 29
3 3 2 2 23 23

In this approach, I'll just create 3 columns and for each item I'll create a rows with the bill_id_fk for a specific bill.在这种方法中,我将只创建 3 列,并为每个项目创建一行,其中包含特定账单的bill_id_fk In this approach, it is easier to query for a counts of the sell of a specific item.在这种方法中,更容易查询特定商品的销售计数。 But my concern is creating thousands and thousands of rows when the app will be used and how will that affect the performance of the app over time?但我担心的是在使用应用程序时会创建成千上万行,随着时间的推移,这将如何影响应用程序的性能?

I'd like to have your opinion on what is the best practice for designing such database.我想就设计此类数据库的最佳实践征求您的意见。 Or is there any other approach should I take?或者我应该采取其他方法吗?

There's no chance that you will go with the first choice, the second is the best approach for your case.您不可能将 go 与第一个选择,第二个是您的情况的最佳方法。

it will not affect your performance if you indexed the right columns.如果您索引了正确的列,它不会影响您的性能。

When it comes to items can add a column to your bills table that holds item numbers, for example:当涉及到项目时,可以在您的账单表中添加一个包含项目编号的列,例如:

bills (id - total_price - user_id - item_counts)
bill_items (id - bill_id - item_id - item_price)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM