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
| --------------
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 `produit`
--------------
--------------
CREATE TABLE `produit`
( `id_produit` integer unsigned not null auto_increment primary key,
`marque` varchar(255) not null,
`prix` decimal(15,2) not null,
`stock` integer unsigned not null
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `produit` (`marque`,`prix`,`stock`) values
('Marque Un', 15.25, 30),
('Marque Deux', 17.75, 20),
('Marque Trois', 22.45, 5),
('Marque Quatre', 11.50, 2)
--------------
--------------
select * from `produit`
--------------
+------------+---------------+-------+-------+
| id_produit | marque | prix | stock |
+------------+---------------+-------+-------+
| 1 | Marque Un | 15.25 | 30 |
| 2 | Marque Deux | 17.75 | 20 |
| 3 | Marque Trois | 22.45 | 5 |
| 4 | Marque Quatre | 11.50 | 2 |
+------------+---------------+-------+-------+
--------------
DROP TABLE IF EXISTS `DetailCommande`
--------------
--------------
CREATE TABLE `DetailCommande`
( `id_commande` integer unsigned not null,
`id_produit` integer unsigned not null,
`quantite` integer unsigned not null,
primary key (`Id_commande`,`Id_produit`),
CONSTRAINT `FK_PRODUIT` FOREIGN KEY (`id_produit`) REFERENCES `produit` (`id_produit`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB
DEFAULT CHARSET=`latin1` COLLATE=`latin1_general_ci`
ROW_FORMAT=COMPRESSED
--------------
--------------
insert into `DetailCommande` (`id_commande`,`id_produit`,`quantite`) values
(101, 1, 5),
(102, 2, 2),
(103, 3, 1),
(104, 4, 2),
(105, 1, 9),
(106, 3, 4)
--------------
--------------
select * from `DetailCommande`
--------------
+-------------+------------+----------+
| id_commande | id_produit | quantite |
+-------------+------------+----------+
| 101 | 1 | 5 |
| 102 | 2 | 2 |
| 103 | 3 | 1 |
| 104 | 4 | 2 |
| 105 | 1 | 9 |
| 106 | 3 | 4 |
+-------------+------------+----------+
--------------
select t1.id_produit,
t2.stock,
t1.quantite,
t2.stock - t1.quantite as newstock
from DetailCommande as t1
inner join produit as t2
on t2.id_produit = t1.id_produit
order by id_commande
--------------
+------------+-------+----------+----------+
| id_produit | stock | quantite | newstock |
+------------+-------+----------+----------+
| 1 | 30 | 5 | 25 |
| 2 | 20 | 2 | 18 |
| 3 | 5 | 1 | 4 |
| 4 | 2 | 2 | 0 |
| 1 | 30 | 9 | 21 |
| 3 | 5 | 4 | 1 |
+------------+-------+----------+----------+
--------------
commit
--------------
--------------
update `produit` as t1
set t1.stock = t1.stock - (select sum(t2.quantite)
from `DetailCommande` as t2
where t2.id_produit = t1.id_produit
)
--------------
--------------
select * from `produit`
--------------
+------------+---------------+-------+-------+
| id_produit | marque | prix | stock |
+------------+---------------+-------+-------+
| 1 | Marque Un | 15.25 | 16 |
| 2 | Marque Deux | 17.75 | 18 |
| 3 | Marque Trois | 22.45 | 0 |
| 4 | Marque Quatre | 11.50 | 0 |
+------------+---------------+-------+-------+
--------------
COMMIT
--------------
--------------
SET AUTOCOMMIT = 1
--------------
Appuyez sur une touche pour continuer... |
Partager