基于属性 B 的属性 A (Microsoft SQL Server 2014)

Attribute A based on Attribute B (Microsoft SQL Server 2014)

例如,我有两个表,分别名为 Manager_OrderBook_Inventory

Manager_Order(
  ID (PK),
  Book_Name,
  Quantity (Attribute A)
)

Book_Inventory(
  ID (PK),
  Book_Name (FK on Manager_Order),
  Quantity (Attribute B)
)

每次经理下订单时,我希望 Book_Inventory 中的 数量 自动更新。

例如,您可以创建一个触发器。

https://msdn.microsoft.com/en-us/library/ms189799(v=sql.110).aspx

考虑使用更新后触发器。

这是一个例子(未测试):

CREATE TRIGGER [dbo].Update_Book_Inventory
ON [dbo].Manager_Order  
FOR  UPDATE
AS
BEGIN
    SET NOCOUNT ON;
      UPDATE [dbo].Book_Inventory
        SET Quantity = i.Quantity
            FROM [dbo].Book_Inventory
            INNER JOIN INSERTED i ON [dbo].Book_Inventory.Book_Name = i.Book_Name
END

参考: https://www.mssqltips.com/sqlservertip/4024/sql-server-trigger-after-update-for-a-specific-value/