1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
   | --------------
START TRANSACTION
--------------
 
--------------
DROP DATABASE IF EXISTS `base`
--------------
 
--------------
CREATE DATABASE IF NOT EXISTS `base`
        DEFAULT CHARACTER SET `latin1`
        DEFAULT COLLATE       `latin1_general_ci`
--------------
 
--------------
DROP TABLE IF EXISTS `test`
--------------
 
--------------
create table `test`
(  `id`          integer unsigned  not null auto_increment primary key,
   `libelle`     varchar(255)      not null,
   `genre`       tinyint unsigned      null default 1,
  `create_date`  TIMESTAMP(6)               default current_timestamp(6)
) ENGINE=InnoDB
  DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
  ROW_FORMAT=COMPRESSED
--------------
 
--------------
drop trigger if exists `verifier`
--------------
 
--------------
create trigger `verifier`
before insert on `test`
for each row
begin
  set new.genre = if (new.genre >=1 and new.genre <=3, new.genre, 1);
end
--------------
 
--------------
insert into `test` (`libelle`,`genre`) values ('un',     default)
--------------
 
--------------
insert into `test` (`libelle`,`genre`) values ('deux',   1)
--------------
 
--------------
insert into `test` (`libelle`,`genre`) values ('trois',  2)
--------------
 
--------------
insert into `test` (`libelle`,`genre`) values ('quatre', 3)
--------------
 
--------------
insert into `test` (`libelle`,`genre`) values ('cinq',   NULL)
--------------
 
--------------
insert into `test` (`libelle`,`genre`) values ('six',    99)
--------------
 
--------------
select * from `test`
--------------
 
+----+---------+-------+----------------------------+
| id | libelle | genre | create_date                |
+----+---------+-------+----------------------------+
|  1 | un      |     1 | 2019-05-10 13:25:46.579260 |
|  2 | deux    |     1 | 2019-05-10 13:25:46.586580 |
|  3 | trois   |     2 | 2019-05-10 13:25:46.606395 |
|  4 | quatre  |     3 | 2019-05-10 13:25:46.617440 |
|  5 | cinq    |     1 | 2019-05-10 13:25:46.641819 |
|  6 | six     |     1 | 2019-05-10 13:25:46.678581 |
+----+---------+-------+----------------------------+
--------------
COMMIT
--------------
 
Appuyez sur une touche pour continuer... | 
Partager