简体   繁体   English

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

[英]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: 在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)
)

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. 上面的代码仅表示我要实现的目标( Price AS (PurchaseID.PRICE) ),每当指定PurchaseID时都应创建Price以便TotalCost也可以自动运行,我无法解决此问题。 How to do it? 怎么做?

PurchaseID int identity(1,1) Primary key

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用TSQL如何确定哪一个化合物外键的列对应于化合物主/唯一键的哪一列? - Using tsql how to determine which column of a compound foreign key corresponds to which column of a compound primary/unique key? 如何在MySQL中更改表中的主键和表中的外键的列? - How to alter a column which is a primary key in the table and a foreign key in another table in MySQL? 如何更新作为组合键的一部分的列,并充当一个表的主键,并作为oracle SQL中其他表的外键 - How to update a column which is part of a composite key and acting as primary key for one table and as a foreign key in other table in oracle SQL 有没有办法在表中没有主键的情况下创建外键关系 - Is there any way to create foreign key relation without primary key in the table 如何创建一个包含五个外键的表,其中一个作为主键(也是一个VARCHAR) - How can I create a table with five foreign keys and one of them as primary key (which is also a VARCHAR) 哪个先删除? 主键还是外键? - Which gets deleted first? Primary or Foreign key? 拥有由整数和外键组成的主键,或者具有自动递增的主键和外键,哪个更好? - Which is better, have a primary key composed of an integer and a foreign key or have a primary key autoincrement and a foreign key? 外键和主键之间的关系? - Relation between foreign and primary key? 创建两个具有相同主键的子类型(或子)表,该主键也是父表的外键 sql - Create Two Subtype (or child) tables with same primary key which is also a foreign key to the parent table sql 如何用不是主键的另一个表的列来验证列的值 - how to validate values of a column with column of another table which is not a primary key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM