mysql> CREATE TABLE Hisob (H_id INT, Middori DECIMAL(10,2)); Query
OK, 0 rows affected (2.29 sec)
mysql> CREATE TRIGGER summa BEFORE INSERT ON hisob
-> FOR EACH ROW SET @sum = @sum + NEW.Middori;
Query OK, 0 rows affected (0.36 sec)
O'zgaruvchidan oldingi @ belgisi global o'zgaruvchini anglatadi. Uning
mysql serveri triggerdan chiqqandan keyin eslab qoladi va uni keyinchalik, masalan,
bu kabi ham chaqirish mumkin.
New degani biz eski qiymatni emas, balki yangi qiymatni olishimizni anglatadi
(OLD eskisi uchun ishlatiladi).
FOR EACH ROW- bu siklga o'xshash narsa, ya'ni trigger biriktirilgan jadvalning
har bir qatori uchun keyingi nuqta-vergulga o'tiladi.
mysql> set @sum=0;
Query OK, 0 rows affected (0.08 sec)
mysql> INSERT INTO hisob VALUES(137,14.98),(141,1937.50),(97,-100.00);
Query OK, 3 rows affected (0.29 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT @sum AS 'Umumiy summa';
+-----------------------+
| Umumiy summa |
+-----------------------+
| 1852.48 |
+-----------------------+
Dostları ilə paylaş: