我以前也发过一个类似的问题,但这个问题比较具体。请看下面的图表:

对这一设计的解释如下:
这里的目的是允许商店经理根据所需的任何货物创建订单“篮子”,并允许正在创建的系统根据订单中包含的产品确定当时的最佳价格。
因此,我设想ProductOrders表最初保存productID和关联的orderID,同时保持bakerID和pricingDate的空值(未定值),因为这将由系统确定和更新,这将构成最终的顺序。
现在你已经知道了我想做什么,请告诉我如何最好地建立这些关系。
谢谢!
发布于 2011-02-10 13:43:53
如果我的理解是正确的,一个尚未完成的订单还没有分配给面包师/定价(这意味着当一个订单下,还没有面包师被选中来烘焙该产品)。
在这种情况下,订单可能是针对Products表下的,然后根据BakersProducts表“完成”。
一个解决方案可以是给ProductsOrders 2单独的" ProductId 's",一个是原始有序的ProductId(即不可空的),比如ProductId,第二个是分配给BakersProducts的外键的一部分(例如ProductId2)。这意味着在ProductsOrders中,复合外键BakerId、ProductId2和PricingDate都是可空的,因为只有在订单完成后才会设置它们。
为了消除这种冗余,您还可以考虑使用代理键而不是复合键。这样,BakersProducts将有一个替代项PK (例如,BakersProductId),然后在ProductsOrders中作为一个可空的FK引用它。这也将避免与ProductsOrders到Product.ProductId中的Direct混淆(从上面看,最初的产品线是订单的一部分)。
HTH?
编辑:
CREATE TABLE dbo.BakersProducts
(
BakerProductId int identity(1,1) not null, -- New Surrogate PK here
BakerId int not null,
ProductId int not null,
PricingDate datetime not null,
Price money not null,
StockLevel bigint not null,
CONSTRAINT PK_BakerProducts PRIMARY KEY(BakerProductId),
CONSTRAINT FK_BakerProductsProducts FOREIGN KEY(ProductId) REFERENCES dbo.Products(ProductId),
CONSTRAINT FK_BakerProductsBaker FOREIGN KEY(BakerId) REFERENCES dbo.Bakers(BakerId),
CONSTRAINT U_BakerProductsPrice UNIQUE(BakerId, ProductId, PricingDate) -- Unique Constraint mimicks the original PK for uniqueness ... could also use a unique index
)
CREATE TABLE dbo.ProductOrders
(
OrderId INT NOT NULL,
ProductId INT NOT NULL, -- This is the original Ordered Product set when order is created
BakerProductId INT NULL, -- This is nullable and gets set when Order is finalised with a baker
OrderQuantity BIGINT NOT NULL,
CONSTRAINT FK_ProductsOrdersBakersProducts FOREIGN KEY(BakersProductId) REFERENCES dbo.BakersProducts(BakerProductId)
.. Other Keys here
)https://stackoverflow.com/questions/4957683
复制相似问题