SQL Server에서 트리거 구축 및 최적화
Florin Angelescu
Instructor
INSERT, UPDATE, DELETE와 함께 사용이 예제에 사용된 Products 테이블의 출력:
| Product | Price | Currency | Quantity | Measure |
|---------|-------|----------|----------|---------|
| Apple | 2.00 | USD | 25000 | kg |
| Apricot | 1.55 | USD | 2500 | kg |
| Avocado | 3.00 | USD | 1000 | kg |
| Banana | 1.80 | USD | 35000 | kg |
ProductsDELETETrackRetiredProductsProducts 테이블에서 행이 삭제되면...
해당 행의 필요한 정보를 RetiredProducts 테이블에 저장합니다.
| Product | Price | Currency | Quantity | Measure |
|---------|-------|----------|----------|---------|
| Apple | 2.00 | USD | 25000 | kg |
| Apricot | 1.55 | USD | 2500 | kg | X
| Avocado | 3.00 | USD | 1000 | kg |
| Banana | 1.80 | USD | 35000 | kg |
| Product | Measure | RemovalDate |
|---------|---------|-------------|
| Apricot | kg | 19.04.2019 |
CREATE TRIGGER TrackRetiredProductsON ProductsAFTER DELETEAS INSERT INTO RetiredProducts (Product, Measure) SELECT Product, Measure FROM deleted;
| 특수 테이블 | INSERT | UPDATE | DELETE |
|---|---|---|---|
| inserted | 새 행 | 새 행 | 해당 없음 |
| deleted | 해당 없음 | 변경된 행 | 삭제된 행 |
CREATE TRIGGER TrackRetiredProducts
ON Products
AFTER DELETE
AS
INSERT INTO RetiredProducts (Product, Measure)
SELECT Product, Measure
FROM deleted;
SQL Server에서 트리거 구축 및 최적화