如何创建相对于作为外键的主键自动生成的列

时间:2017-11-14 05:26:28

标签: sql sql-server

在SQL Server Management Studio中,我正在创建此表:

CREATE TABLE Purchase 
(
     PurchaseID INT NOT NULL 
         FOREIGN KEY REFERENCES Product(ProductID) PRIMARY KEY,
     TotalQuantity INT NOT NULL,
     Price AS (PurchaseID.PRICE), 
     TotalPrice AS (TotalQuantity*Price),
     SalesAssistantID INT NOT NULL 
         FOREIGN KEY REFERENCES SalesAssistant(SalesAssistantID), 
     CashierID INT NOT NULL 
         FOREIGN KEY REFERENCES Cashier(CashierID)
)

以上代码仅代表我想要实现的目标(Price AS (PurchaseID.PRICE)),只要指定Price就应创建PurchaseID,以便TotalCost也可以自动运行,I无法解决这个问题。怎么做?

1 个答案:

答案 0 :(得分:0)

PurchaseID int identity(1,1) Primary key

这使得purchaseID成为主键,(1,1)表示它将从1开始并以1递增。