客户与购物车中的订单之间的关系

发布于 2024-10-31 11:22:40 字数 371 浏览 0 评论 0原文

我正在制作一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

§普罗旺斯的薰衣草 2024-11-07 11:22:40

Order 表有一列 orderProductId - 这是没有意义的;如果您考虑一下,这意味着一个 Order 将有一个 OrderProduct

OrderProduct 表应有一列 OrderId - 与 Order 表的主键相关的外键。这样,同一个订单就可以有 10 个 OrderProduct 记录(通常称为订单行)。

对于这些问题,我发现模拟一些测试数据很有帮助——你很快就会看到它是否有效。

The Order table has a column orderProductId - which makes no sense; if you think about it that means an Order will have one OrderProduct.

The OrderProduct table should have a column, OrderId - a foreign key relating to the primary key of the Order table. That way you could have 10 OrderProduct 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.

酒浓于脸红 2024-11-07 11:22:40

这是关于购物车的简单且有点异端的建议。

table cart(
   customer_id not null
  ,create_date not null
  ,primary key(customer_id)
  ,foreign key references customers(customer_id) on delete cascade
);

table cart_item(
   customer_id   not null
  ,product_id    not null
  ,quantity      not null
  ,modified_date not null
  ,primary key(customer_id, product_id)
  ,foreign key(customer_id) references cart(customer_id) on delete cascade
  ,foreign key(product_id)  references products(product_id) on delete cascade
);

以下是设计背后的基本原理:
如果您正在构建的内容与大多数购物网站类似,则每个客户一次只会有 0-1 个活跃购物车。因此,购物车可以继承客户密钥。
通常,一个人不会购买一种产品两次。相反,人们会购买 2 件相同的产品,因此我将产品键设置为购物车项目表中主键的一部分。

要么客户决定不继续购买购物车中的商品。在这种情况下,购物车和商品将保留在表中,直到他回来并决定继续付款,或者直到您决定(例如基于 cart.create_date)是时候删除数据了。

当客户继续结账并完成订单时,您可以创建真实订单和订单行(产品),包括订单日期,该日期不一定与客户开始将商品放入购物车的日期相同。 (我在亚马逊上购买的书籍通常需要一周的时间才能完成,因为我一直在寻找有趣的书籍,并且我必须先阅读一些评论:)

至于您的订单/订单行表应该有哪些键,它实际上取决于很多因素,例如:

  • 是否可以删除有订单的产品?
  • 您是否需要准确表述购买时的产品信息(价格、名称)?
  • 我是否使用订单行表来表示其他内容,例如折扣/活动代码等
  • 我是否需要准确报告购买时的客户送货地址? (客户搬到另一个国家,但你将物品发送到他的老地方)

上述设计使用自然键,在这种情况下恰好提供了出色的性能。

Here is simple and slightly heretic suggestion for a shopping cart.

table cart(
   customer_id not null
  ,create_date not null
  ,primary key(customer_id)
  ,foreign key references customers(customer_id) on delete cascade
);

table cart_item(
   customer_id   not null
  ,product_id    not null
  ,quantity      not null
  ,modified_date not null
  ,primary key(customer_id, product_id)
  ,foreign key(customer_id) references cart(customer_id) on delete cascade
  ,foreign key(product_id)  references products(product_id) on delete cascade
);

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:

  • Is it possible to delete products that have orders?
  • Do you need to accurately represent the product information (price, name) as of the time of purchase?
  • Do I use the orderline table to represent other stuff such as discounts/campaign codes etc
  • Do I need to accurately report the Customer shipping address as of time of purchase? (customer moved to another country, but you sent the items to his old place)

The above design is using natural keys, which happens to provide excellent performance in this case.

铃予 2024-11-07 11:22:40

这对我来说似乎很好,但您需要 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).

小姐丶请自重 2024-11-07 11:22:40

一个订单可以有多个产品。您可以从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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文