我有一张桌子。
CREATE TABLE StockItem
(
id NUMBER(8) NOT NULL,
book_id NUMBER(8) NOT NULL,
wh_id NUMBER(8) NOT NULL,
quantity NUMBER(4) NOT NULL,
price NUMBER(8,2) NOT NULL
);
ALTER TABLE StockItem
ADD CONSTRAINT UQ_StockItem UNIQUE (book_id, wh_id);
ALTER TABLE StockItem ADD CONSTRAINT PK_StockItem
PRIMARY KEY (id);
ALTER TABLE StockItem ADD CONSTRAINT FK_StockItem_Book
FOREIGN KEY (book_id) REFERENCES Book (id);
ALTER TABLE StockItem ADD CONSTRAINT FK_StockItem_Warehouse
FOREIGN KEY (wh_id) REFERENCES Warehouse (id);
现在假设wh_id = 1
已关闭,我应该将所有书籍移至wh_id = 2
. 的书价wh_id = 2
应该与它们的价格相同,wh_id = 1
并且quantity
应该正确计算(例如book_id
,两个仓库的价格相同)。我怎样才能做到这一点?
升级版:
这是我如何获得书籍的新数量值的方法。
SELECT BOOK_ID, SUM(QUANTITY)
FROM STOCKITEM
WHERE WH_ID IN (1, 2)
GROUP BY BOOK_ID;
现在我应该以某种方式更新它们。
UPDATE STOCKITEM
SET QUANTITY = /* My summed quantity */
WHERE BOOK_ID = /* Book ids from 1 and 2 wh */