Xây dựng và Tối ưu hóa Trigger trong SQL Server
Florin Angelescu
Instructor
INSERT, UPDATE, DELETE cho bảng hoặc viewKết quả từ bảng Products dùng cho ví dụ này:
| 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 |
ProductsDELETETrackRetiredProductsKhi các hàng bị xóa khỏi bảng Products...
Lưu thông tin cần thiết từ các hàng đó vào bảng 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;
| Bảng đặc biệt | INSERT | UPDATE | DELETE |
|---|---|---|---|
| inserted | hàng mới | hàng mới | N/A |
| deleted | N/A | hàng cập nhật | hàng bị xóa |
CREATE TRIGGER TrackRetiredProducts
ON Products
AFTER DELETE
AS
INSERT INTO RetiredProducts (Product, Measure)
SELECT Product, Measure
FROM deleted;
Xây dựng và Tối ưu hóa Trigger trong SQL Server