Laboratoriya ishi Mavzu: sql triggerlar yaratish. Ishdan maqsad
Download 211.75 Kb. Pdf ko'rish
|
Laboratoriya ishi Mavzu sql triggerlar yaratish. Ishdan maqsad
- Bu sahifa navigatsiya:
- Keyingi misol
- Triggega misollar: № 1
select
@ sum ; 1 row in set (0.00 sec) Quyidagi hisoblandi: 14.98 + 1937.50 - 100, yoki 1852 Triggerni quyidagicha o’chirish mumkin: DROP TRIGGER test.ins_sum; OLD va NEW Keling, OLD va NEW nima ekanligini batafsil ko'rib chiqaylik. Ushbu ko'rsatmalar trigger bilan ishlaydigan ustunlarga kirishga imkon beradi. Ushbu ko'rsatmalar faqat MySQL-ga tegishli. Ular registrga sezgir emas. INSERT triggerida faqat NEW.col_name ishlatilishi mumkin. Bu tushunarli, OLD.col_name hali mavjud emas. DELETE triggerida faqat OLD.col_name ishlatilishi mumkin. UPDATE triggerida ikkalasini ham ishlatishingiz mumkin. Keyingi misol Quyidagi misol bir nechta ko'rsatmalardan trigger yaratishga imkon beradi. Faqatgina ularni BEGIN ... ... END ichiga qo'shish va triggerning davomiyligini belgilovchi delimiter e'lon qilish kerak. \d // CREATE TRIGGER upd_check BEFORE UPDATE ON hisob FOR EACH ROW BEGIN IF NEW.middori < 0 THEN SET NEW.middori = 0; ELSEIF NEW.middori > 100 THEN SET NEW.middori = 100; END IF; END;// mysql> \d ; Trigger ichida siz CALL usulidan foydalangan holda saqlangan protsedurani chaqirishingiz mumkin, ammo ko’plikda emas. Shuningdek, triggerlardan tranzaksiyalarda foydalanib bo'lmaydi. Triggega misollar: № 1 mysql> create table Buyurtma -> (id int auto_increment primary key, -> Nomi varchar(30), -> Ketgan_sana date, -> Kelgan_sana date)$$ Query OK, 0 rows affected (0.53 sec) mysql> CREATE trigger buyurtma_vaqti BEFORE INSERT -> ON buyurtma -> FOR EACH ROW BEGIN -> SET NEW.ketgan_sana = NOW(); -> END// Query OK, 0 rows affected (0.19 sec) mysql> insert into buyurtma -> (Nomi) -> values -> ('Kitob'), -> ('O`yinchoq'), -> ('Portfel')// Query OK, 3 rows affected (0.15 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from buyurtma// +----+-----------+-------------+-------------+ | id | Nomi | Ketgan_sana | Kelgan_sana | +----+-----------+-------------+-------------+ | 1 | Kitob | 2019-11-25 | NULL | | 2 | O`yinchoq | 2019-11-25 | NULL | | 3 | Portfel | 2019-11-25 | NULL | +----+-----------+-------------+-------------+ 3 rows in set (0.00 sec) № 2 01 mysql> DELIMITER // 02 mysql> CREATE TRIGGER `test_user_pass` BEFORE INSERT ON `test`.`user` 03 -> FOR EACH ROW 04 -> BEGIN -> SET NEW.name = LEFT(NEW.name,1); 5 -> SET NEW.otch = LEFT(NEW.otch,1); 6 -> SET NEW.pass = md5(NEW.pass); 7 -> END// 8 Query OK, 0 rows affected (0.09 sec) 9 10 mysql> DELIMITER ; 11 Ma’lumot kiritamiz: 12 mysql> INSERT INTO `user` SET `fam`='Нагайченко', `name`='Максим', 13 1 14 `otch` = 'Валерьевич', `pass` = 'password', `login` = 'maxnag'; 15 2 16 Query OK, 1 row affected (0.00 sec) 17 № 3 18 mysql> DELIMITER // 19 01 mysql> CREATE TRIGGER `test_user_pass2` BEFORE UPDATE ON 20 02 21 `test`.`user` 22 03 23 -> FOR EACH ROW 24 04 25 -> BEGIN 26 05 27 -> SET NEW.name = LEFT(NEW.name,1); 28 06 29 -> SET NEW.otch = LEFT(NEW.otch,1); 30 07 31 -> SET NEW.pass = md5(NEW.pass); 32 08 33 -> END// 34 09 35 Query OK, 0 rows affected (0.09 sec) 36 10 37 38 11 mysql> DELIMITER ; 39 Ма’lumotni yangilaymiz. 40 mysql> UPDATE `user` SET `fam`='Иванов', `name`='Иван', `otch` = 'Иванович', 41 1 42 `pass` = 'пароль', `login` = 'ivan' WHERE id=1; 43 2 44 Query OK, 1 row affected (0.00 sec) 45 Natija 1 mysql> SELECT * FROM `user`; 2 +----+-------------+------+------+----------------------------------+--------+ 3 | id | fam | name | otch | pass | login | 4 +----+-------------+------+------+----------------------------------+--------+ 5 | 1 | Иванов | И | И | e242f36f4f95f12966da8fa2efd59992 | ivan 6 +----+-------------+------+------+----------------------------------+--------+ 7 8 1 row in set (0.00 sec) | Download 211.75 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling