简体   繁体   中英

How to create column which is generated automatically in relation to primary key which is the foreign key fro

In SQL Server Management Studio, I am creating this table:

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)
)

Above code only represents what I want to achieve ( Price AS (PurchaseID.PRICE) ), Price should be created whenever the PurchaseID is specified so that TotalCost can also function automatically, I could not solve this problem. How to do it?

PurchaseID int identity(1,1) Primary key

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM