客户与购物车中的订单之间的关系
我正在制作一个 ERD,以便我可以构建我的购物车。
我对订单和客户之间的关系感到困惑。
如果我没记错的话,一个客户可以订购很多产品,
一个订单可以由 1 个客户下,
那么
Create Table OrderProduct(
orderProductId int PRIMARY KEY,
productID int,
Quantity int
)
Create Table Orders(
OrderId int PRIMARY KEY,
orderProductId int, //foregin key
CustomerId int,
date
)
我是正确的,还是 mu 表结构错误?
I'm making an ERD, so that I can build my shopping cart.
I am confused about the relationship between Order and Customer.
If im not mistaken, a customer can order many products,
an order can be placed by 1 customer
So
Create Table OrderProduct(
orderProductId int PRIMARY KEY,
productID int,
Quantity int
)
Create Table Orders(
OrderId int PRIMARY KEY,
orderProductId int, //foregin key
CustomerId int,
date
)
Am I correct, or is mu table structure wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Order
表有一列orderProductId
- 这是没有意义的;如果您考虑一下,这意味着一个Order
将有一个OrderProduct
。OrderProduct
表应有一列OrderId
- 与Order
表的主键相关的外键。这样,同一个订单就可以有 10 个OrderProduct
记录(通常称为订单行)。对于这些问题,我发现模拟一些测试数据很有帮助——你很快就会看到它是否有效。
The
Order
table has a columnorderProductId
- which makes no sense; if you think about it that means anOrder
will have oneOrderProduct
.The
OrderProduct
table should have a column,OrderId
- a foreign key relating to the primary key of theOrder
table. That way you could have 10OrderProduct
records (commonly called order lines) for the same order.With these issues I find it helps to mock up some test data - you'd see pretty quick if it would work out.
这是关于购物车的简单且有点异端的建议。
以下是设计背后的基本原理:
如果您正在构建的内容与大多数购物网站类似,则每个客户一次只会有 0-1 个活跃购物车。因此,购物车可以继承客户密钥。
通常,一个人不会购买一种产品两次。相反,人们会购买 2 件相同的产品,因此我将产品键设置为购物车项目表中主键的一部分。
要么客户决定不继续购买购物车中的商品。在这种情况下,购物车和商品将保留在表中,直到他回来并决定继续付款,或者直到您决定(例如基于 cart.create_date)是时候删除数据了。
当客户继续结账并完成订单时,您可以创建真实订单和订单行(产品),包括订单日期,该日期不一定与客户开始将商品放入购物车的日期相同。 (我在亚马逊上购买的书籍通常需要一周的时间才能完成,因为我一直在寻找有趣的书籍,并且我必须先阅读一些评论:)
至于您的订单/订单行表应该有哪些键,它实际上取决于很多因素,例如:
上述设计使用自然键,在这种情况下恰好提供了出色的性能。
Here is simple and slightly heretic suggestion for a shopping cart.
Here is the rationale behind the design:
If what you are building looks anything like most shopping sites, there will only be 0-1 active carts at a time per customer. Therefore, the cart can inherit the customer key.
Usually, one does not buy a product 2 times. Instead one buys 2 pieces of the same product, so I made the product key part of the primary key in the Cart Item table.
Either the customer decides NOT to proceed with the stuff in his cart. In this case, the cart and the items will just stay in the table until he either comes back and decides to go ahead and pay up, or until you decide (for example based on cart.create_date) that it is time to remove the data.
When the customer proceeds to checkout and finalizes his order, you can create the real order and orderlines (products), including the order date which is not necessarily the same as the date when the customer began to stick items in the cart. (My book purchases on amazon typically takes a week to finalize because I keep finding interesting books and I have to read some reviews first :)
As for what keys your order/orderline table should have, it actually depends on lots of things such as:
The above design is using natural keys, which happens to provide excellent performance in this case.
这对我来说似乎很好,但您需要 OrderProduct 表中的 OrderId 才能将订单详细信息链接到订单 - 从 Orders 中删除 OrderProductId。
当然,其他一些列(例如 ProductId 和 CustomerId)也应该是外键。
“订单”是最终订单还是有后续开票步骤?因为您通常可能希望锁定订单的单价(来自订单时的产品文件,或者可能来自签署/批准的报价)。
That seems fine to me, but you need a OrderId in the OrderProduct table in order to link the order details to the order - drop the OrderProductId from Orders.
Some of the other columns like ProductId and CustomerId should also be foreign keys, of course.
Is an "Order" a finalized order or is there a later invoicing step? Because you typically may want to lock in the unit price at the order (from the product file at the time of the order, or perhaps from a signed/approved quote).
一个订单可以有多个产品。您可以从OrderProuct表中删除orderProductId,并将productId作为主键。同时将OrderId作为OrderProduct表中的外键,这样就可以在Order和产品之间建立映射关系。相应的顺序。
An order can have multiple products.You can remove the orderProductId from OrderProuct table and make the productId as the primary key.Also have the OrderId as the foriegn key in the OrderProduct table so that you can have a mapping between the Order and the products in the respective order.