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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
| --------------
SET AUTOCOMMIT = 0
--------------
--------------
START TRANSACTION
--------------
--------------
DROP DATABASE IF EXISTS `base`
--------------
--------------
CREATE DATABASE `base`
DEFAULT CHARACTER SET `latin1`
DEFAULT COLLATE `latin1_general_ci`
--------------
--------------
DROP TABLE IF EXISTS `categorie`
--------------
--------------
CREATE TABLE `categorie`
( `id` INTEGER UNSIGNED NOT NULL auto_increment PRIMARY KEY,
`categorie` VARCHAR(255) NOT NULL
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `categorie` (`categorie`) values
('Entreprise générale'),('Menuisier'),('Peintre'),('Plafonneur')
--------------
--------------
select * from `categorie`
--------------
+----+---------------------+
| id | categorie |
+----+---------------------+
| 1 | Entreprise générale |
| 2 | Menuisier |
| 3 | Peintre |
| 4 | Plafonneur |
+----+---------------------+
--------------
DROP TABLE IF EXISTS `promotion`
--------------
--------------
CREATE TABLE `promotion`
( `id` INTEGER UNSIGNED NOT NULL auto_increment PRIMARY KEY,
`magasin` VARCHAR(255) NOT NULL,
`nom` VARCHAR(255) NOT NULL,
`datdeb` DATE NOT NULL,
`datfin` DATE NOT NULL,
`catprof` INTEGER UNSIGNED NOT NULL,
CONSTRAINT `FK_CATEGORIE` FOREIGN KEY (`catprof`) REFERENCES `categorie` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `promotion` (`magasin`,`nom`,`datdeb`,`datfin`,`catprof`) values
('magasin 01', 'nom 01', '2017-11-01', '2017-12-31', 1),
('magasin 02', 'nom 02', '2017-11-02', '2017-12-30', 2),
('magasin 03', 'nom 03', '2017-11-03', '2017-12-29', 3),
('magasin 04', 'nom 04', '2017-11-04', '2017-12-28', 4),
('magasin 05', 'nom 05', '2017-11-05', '2017-12-27', 1),
('magasin 06', 'nom 06', '2017-11-06', '2017-12-26', 2),
('magasin 07', 'nom 07', '2017-11-07', '2017-12-25', 3),
('magasin 08', 'nom 08', '2017-11-08', '2017-12-24', 4),
('magasin 09', 'nom 09', '2017-11-09', '2017-12-23', 1),
('magasin 10', 'nom 10', '2017-11-10', '2017-12-22', 2),
('magasin 11', 'nom 11', '2017-11-11', '2017-12-21', 3),
('magasin 12', 'nom 12', '2017-11-12', '2017-12-20', 4)
--------------
--------------
select * from `promotion`
--------------
+----+------------+--------+------------+------------+---------+
| id | magasin | nom | datdeb | datfin | catprof |
+----+------------+--------+------------+------------+---------+
| 1 | magasin 01 | nom 01 | 2017-11-01 | 2017-12-31 | 1 |
| 2 | magasin 02 | nom 02 | 2017-11-02 | 2017-12-30 | 2 |
| 3 | magasin 03 | nom 03 | 2017-11-03 | 2017-12-29 | 3 |
| 4 | magasin 04 | nom 04 | 2017-11-04 | 2017-12-28 | 4 |
| 5 | magasin 05 | nom 05 | 2017-11-05 | 2017-12-27 | 1 |
| 6 | magasin 06 | nom 06 | 2017-11-06 | 2017-12-26 | 2 |
| 7 | magasin 07 | nom 07 | 2017-11-07 | 2017-12-25 | 3 |
| 8 | magasin 08 | nom 08 | 2017-11-08 | 2017-12-24 | 4 |
| 9 | magasin 09 | nom 09 | 2017-11-09 | 2017-12-23 | 1 |
| 10 | magasin 10 | nom 10 | 2017-11-10 | 2017-12-22 | 2 |
| 11 | magasin 11 | nom 11 | 2017-11-11 | 2017-12-21 | 3 |
| 12 | magasin 12 | nom 12 | 2017-11-12 | 2017-12-20 | 4 |
+----+------------+--------+------------+------------+---------+
--------------
drop view if exists `vue`
--------------
--------------
create view `vue` as
select t1.id,
t1.magasin,
t1.nom,
t1.datdeb,
t1.datfin,
t2.id as catprof,
t2.categorie
from `promotion` as t1
inner join `categorie` as t2
on t2.id = t1.catprof
order by t1.id
--------------
--------------
select * from `vue`
--------------
+----+------------+--------+------------+------------+---------+---------------------+
| id | magasin | nom | datdeb | datfin | catprof | categorie |
+----+------------+--------+------------+------------+---------+---------------------+
| 1 | magasin 01 | nom 01 | 2017-11-01 | 2017-12-31 | 1 | Entreprise générale |
| 2 | magasin 02 | nom 02 | 2017-11-02 | 2017-12-30 | 2 | Menuisier |
| 3 | magasin 03 | nom 03 | 2017-11-03 | 2017-12-29 | 3 | Peintre |
| 4 | magasin 04 | nom 04 | 2017-11-04 | 2017-12-28 | 4 | Plafonneur |
| 5 | magasin 05 | nom 05 | 2017-11-05 | 2017-12-27 | 1 | Entreprise générale |
| 6 | magasin 06 | nom 06 | 2017-11-06 | 2017-12-26 | 2 | Menuisier |
| 7 | magasin 07 | nom 07 | 2017-11-07 | 2017-12-25 | 3 | Peintre |
| 8 | magasin 08 | nom 08 | 2017-11-08 | 2017-12-24 | 4 | Plafonneur |
| 9 | magasin 09 | nom 09 | 2017-11-09 | 2017-12-23 | 1 | Entreprise générale |
| 10 | magasin 10 | nom 10 | 2017-11-10 | 2017-12-22 | 2 | Menuisier |
| 11 | magasin 11 | nom 11 | 2017-11-11 | 2017-12-21 | 3 | Peintre |
| 12 | magasin 12 | nom 12 | 2017-11-12 | 2017-12-20 | 4 | Plafonneur |
+----+------------+--------+------------+------------+---------+---------------------+
--------------
select *
from `vue`
where catprof = 2
--------------
+----+------------+--------+------------+------------+---------+-----------+
| id | magasin | nom | datdeb | datfin | catprof | categorie |
+----+------------+--------+------------+------------+---------+-----------+
| 2 | magasin 02 | nom 02 | 2017-11-02 | 2017-12-30 | 2 | Menuisier |
| 6 | magasin 06 | nom 06 | 2017-11-06 | 2017-12-26 | 2 | Menuisier |
| 10 | magasin 10 | nom 10 | 2017-11-10 | 2017-12-22 | 2 | Menuisier |
+----+------------+--------+------------+------------+---------+-----------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager