0

in sql i have a table of items. An item can have multiples prices. But when i come to using the data, most of the time i only need the currentPrice that i can find with the date.

My question : Would it be wrong to have 2 relation on price where and Item would have a relation with the currentPrice and all the price. Do there is a solution for this kind of problem to prevent complication in my mvc. Im currently using a viewModel with and the current price but if i could handle this in the database it seem better to me. (tell me if its not).

Note: I don't think this exact methode would work since I would need to create my price table before my item table and the same for the item table. But this show my problem.

Thanks alot for your help.

CREATE TABLE Item.Items
(
    ItemId INT NOT NULL 
        CONSTRAINT PK_Items_ItemId 
        PRIMARY KEY
        IDENTITY,
    --------------------------------------------------
    --CurrentPriceId INT NOT NULL
        CONSTRAINT FK_Prices_Items
        FOREIGN KEY REFERENCES Item.Items (ItemId),
    --------------------------------------------------
    Name VARCHAR(100) NOT NULL,
    Points INT NOT NULL DEFAULT 0,
    Description VARCHAR(5000) NULL
)

CREATE TABLE Item.Prices
(
    PriceId INT NOT NULL 
        CONSTRAINT PK_ItemPrices_ItemPriceId
        PRIMARY KEY
        IDENTITY,
    ItemId INT NOT NULL
        CONSTRAINT FK_ItemPrices_Item
        FOREIGN KEY REFERENCES Item.Items (ItemId),
    EffectiveDate DATETIME NOT NULL,
    Value MONEY NOT NULL
)
4

2 回答 2

1

更好的是:

CREATE TABLE Item.Items
(
  ItemId INT NOT NULL 
      CONSTRAINT PK_Items_ItemId 
      PRIMARY KEY IDENTITY,
  --------------------------------------------------
  CurrentPrice MONEY NOT NULL,  -- optional optimization, redundant as
                                -- current price is available from item.Prices
  --------------------------------------------------
  Name VARCHAR(100) NOT NULL,
  Points INT NOT NULL DEFAULT 0,
  Description VARCHAR(5000) NULL
)

CREATE TABLE Item.Prices
(
   ItemId INT NOT NULL
      CONSTRAINT FK_ItemPrices_Item
      FOREIGN KEY REFERENCES Item.Items (ItemId),
   EffectiveUtc DATETIME NOT NULL,
   Price MONEY NOT NULL,
   Constraint [ItemPricesPK] PRIMARY KEY CLUSTERED 
   (
      [ItemId] ASC,
      [EffectiveUtc] ASC
   )
)

正如@popovitz 所提到的,您始终可以使用相关子查询获取任何特定日期的价格......

Select i.name, p.price 
From Item.Items i
   Join Item.Prices p 
      ON i.ItemId = p.ItemId
Where p.EffectiveUtc = 
   (Select Max(EffectiveUtc ) 
    From Item.Prices
    Where ItemId = i.ItemId 
       And EffectiveUtc <= @asOfDate)
于 2014-05-04T15:11:16.957 回答
0

我不建议添加一currentPrice列,因为您必须不断确保在新价格生效时更新项目表。当您有一个包含价格的表时,查询当前价格并不难EffectiveDate

SELECT i.name, p.price FROM Items i
INNER JOIN Prices p ON i.ItemId = p.ItemId
WHERE p.EffectiveDate = 
     (SELECT MAX(EffectiveDate) FROM Price
     WHERE EffectiveDate <= SYSDATE
     AND ItemId = i.ItemId)

这将为当前系统时间选择正确的价格,假设 (EffectiveDate, ItemId) 对于表价格是唯一的。

于 2014-05-04T15:12:37.037 回答