简体   繁体   中英

How to make column of one table increment based on the value of other table?

I am making a project on the online shopping site. there i am having on table for product details and other for stock details

the two tables are like this

ProductInfo
Column Name   Data Type   Constraint
ProductID     Int             Primary key , auto increment
ProductName   Varchar(100)  
SubCategoryID Int             Foreign key with ProductSubCategory
CompanyID     Varchar(20)     Foreign key with CompanyInfo
Price         Float 
Quantity      Int   
Description   Varchar(1000) 

ProductStock
Column Name Data Type     Constraint
StockID     Int         Primary key,auto increment
ProductID   Int         Foreign key with ProductInfo
Quantity    Int 
StockType   Varchar     Check(‘IN’,’OUT’)   
StockDate   Datetime

now initially i have kept the value of quantity of productinfo = 0 and want increment or decreement it on the basis of value of quantity of ProductStock depending upon StockType

If stock is 'In' then increment the quantity of productinfo by the number=value of quantity of ProductStock

If stock is 'Out' then decrement the quantity of productinfo by the number=value of quantity of ProductStock

What query should i made to made this operation?

Use a trigger "AFTER INSERT" to increment-decrements the desired values.

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

Try this,

Just you need to update the ProductInfo table, when you insert the products in ProductStock table. When the stock is decrements in ProductStock table just put update productInfo table for the column name Quantity. For that match the ProductID on both tables.

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