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
-> 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)
|
Dostları ilə paylaş: |