购物车的 ERD
我对我的应用程序的实体关系图有点困惑。
到目前为止,我已经为产品、客户、类别创建了表。
我应该为 UserAccounts 创建一个表来保存客户的 ID 和密码,还是直接将 ID 和密码放在客户表中?
其次,每个顾客都有自己的购物车。所以我做了一个CartItem表
CartItemId, 产品编号, 类别ID, 描述, 单价, TotalPrice
但此表未将客户与购物车中的 cartItem 关联起来。那么我也应该在这里添加 CustomerId 吗?
是否需要描述和单价,因为这些已经在产品表中定义了?
对于购物车表, 购物车 ID, 购物车商品 ID, CustomerId
在我的应用程序中发现任何错误之前,我需要澄清一些事情。
数据定义语言:
CREATE DATABASE ShoppingCart
Create Table Customer(
CustomerId int PRIMARY KEY,
Firstname varchar(50),
Lastname varchar(50),
Address varchar(50),
City varchar(50),
State varchar(50),
Country varchar(50),
Mobile varchar(50),
Phone varchar(50),
Email varchar(50)
)
Create Table UserAccount(
UserName varchar(50),
PasswordHash varchar(50)
)
Create Table Category(
CategoryId int PRIMARY KEY,
CategoryName varchar(50),
CategoryDescription varchar(50),
CategoryImage varchar(50)
)
Create Table Products(
ProductId int PRIMARY KEY,
ProductName varchar(50),
Description varchar(50),
CategoryId int,
UnitPrice money,
DateAdded datetime,
thumn varchar(50),
CONSTRAINT fk_CategoryId FOREIGN KEY (CategoryId)
REFERENCES Category(CategoryId)
)
Create Table CartItem(
CartItemId int,
ProductId int,
CategoryId int,
Description varchar(50),
UnitPrice money,
TotalPrice money,
PRIMARY KEY (CartItemId),
CONSTRAINT fk_ProductId FOREIGN KEY (ProductId)
REFERENCES Products(ProductId),
CONSTRAINT fk_CartItem_CategoryId FOREIGN KEY (CategoryId)
REFERENCES Category(CategoryId)
)
Create Table Cart(
CartId int,
CartItemId int,
ProductId int,
CategoryId int,
Description varchar(50),
UnitPrice money,
TotalPrice money,
PRIMARY KEY (CartId),
CONSTRAINT fk_CartItemId FOREIGN KEY (CartItemId)
REFERENCES CartItem(CartItemId),
CONSTRAINT fk_CartItem_CategoryId FOREIGN KEY (CategoryId)
REFERENCES Category(CategoryId),
PRIMARY KEY (ProductId),
CONSTRAINT fk_CartItemId FOREIGN KEY (ProductId)
REFERENCES CProducts(productId)
)
I'm a bit confused about the entity relationship diagram for my application.
So far I have made tables for Product,Customer,Category
Should I make a table for UserAccounts that holds the id and password for the Customer or directly place the the id and password in the Customer table?
Secondly, each customer will have his own cart. So I have made a CartItem table
CartItemId,
ProductId,
CategoryId,
Description,
UnitPrice,
TotalPrice
But this table is not associating a customer with the cartItem in the shoppingcart. So Should i add the CustomerId here also?
Is there a need for Description and unit price, because those are already defined in the Product Table?
and for Cart Table,
CartId,
CartItemId,
CustomerId
I need to clear up a few things, before I find any mistakes in my application.
DDL:
CREATE DATABASE ShoppingCart
Create Table Customer(
CustomerId int PRIMARY KEY,
Firstname varchar(50),
Lastname varchar(50),
Address varchar(50),
City varchar(50),
State varchar(50),
Country varchar(50),
Mobile varchar(50),
Phone varchar(50),
Email varchar(50)
)
Create Table UserAccount(
UserName varchar(50),
PasswordHash varchar(50)
)
Create Table Category(
CategoryId int PRIMARY KEY,
CategoryName varchar(50),
CategoryDescription varchar(50),
CategoryImage varchar(50)
)
Create Table Products(
ProductId int PRIMARY KEY,
ProductName varchar(50),
Description varchar(50),
CategoryId int,
UnitPrice money,
DateAdded datetime,
thumn varchar(50),
CONSTRAINT fk_CategoryId FOREIGN KEY (CategoryId)
REFERENCES Category(CategoryId)
)
Create Table CartItem(
CartItemId int,
ProductId int,
CategoryId int,
Description varchar(50),
UnitPrice money,
TotalPrice money,
PRIMARY KEY (CartItemId),
CONSTRAINT fk_ProductId FOREIGN KEY (ProductId)
REFERENCES Products(ProductId),
CONSTRAINT fk_CartItem_CategoryId FOREIGN KEY (CategoryId)
REFERENCES Category(CategoryId)
)
Create Table Cart(
CartId int,
CartItemId int,
ProductId int,
CategoryId int,
Description varchar(50),
UnitPrice money,
TotalPrice money,
PRIMARY KEY (CartId),
CONSTRAINT fk_CartItemId FOREIGN KEY (CartItemId)
REFERENCES CartItem(CartItemId),
CONSTRAINT fk_CartItem_CategoryId FOREIGN KEY (CategoryId)
REFERENCES Category(CategoryId),
PRIMARY KEY (ProductId),
CONSTRAINT fk_CartItemId FOREIGN KEY (ProductId)
REFERENCES CProducts(productId)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
#1 不要存储密码 - 存储加盐哈希。
如果已经通过购物车表获取了客户 ID,则不要将其添加到购物车项目表中。
现在,我认为您对几乎相同的 Cart 和 CartItem 表感到困惑。
UserAccount 似乎是孤立的。它需要位于客户表中或以某种方式链接。
我会从 CartItem 中删除 CategoryId、Description、UnitPrice 和 TotalPrice(因为它们位于 Product 中),除非将产品定制到每个客户的购物车中。您还面临着让用户以旧价格结账的风险(除非您将购物车视为报价,这是一个完全不同的业务领域)。
CartItem 需要一种方法来到达其购物车或客户。
我会这样做:
当购物车变成订单时,通常订单详细信息或发票详细信息表将锁定价格,也许还有产品名称,并且购物车将为客户清空。这在很大程度上取决于进一步的业务需求。
#1 Do not store the password - store a salted hash.
Do not make the customer Id in the cart item table if it's already obtained through going through Cart table.
Right now, I think you are confused with having a Cart and CartItem table which are nearly identical.
UserAccount appears to be orphaned. It needs to be either in the Customer table or somehow linked.
I would drop CategoryId, Description, UnitPrice and TotalPrice from CartItem (since they are in Product) unless products are customized into the shopping cart for each customer. You also run the risk of letting a user check out with a product at an old price (unless you treat carts as quotes, which is a whole other business domain).
A CartItem needs a way to get to its Cart or Customer.
Here's what I would do:
When a cart becomes an order, typically an order detail or invoice detail table will lock in the price, and perhaps the name of the product, and the cart will be emptied for a custoemr. A lot of this depends on further business needs.