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
| CREATE TABLE UM_unite_mesure(
UM_ident INT IDENTITY,
UM_code CHAR(3) NOT NULL,
UM_libelle VARCHAR(50) NOT NULL,
PRIMARY KEY(UM_ident),
UNIQUE(UM_code)
);
insert into UM_unite_mesure
(UM_code, UM_libelle)
values('KGM', 'kilogramme')
,('GRM', 'gramme')
,('PCE', 'piece')
,('CEN', 'centaine')
,('MIL', 'millier')
;
select * from UM_unite_mesure
;
CREATE TABLE PR_produit(
PR_ident INT IDENTITY,
PR_reference CHAR(8) NOT NULL,
PR_designation VARCHAR(50) NOT NULL,
UM_ident INT NOT NULL,
PRIMARY KEY(PR_ident),
UNIQUE(PR_reference),
FOREIGN KEY(UM_ident) REFERENCES UM_unite_mesure(UM_ident)
);
insert into PR_produit
(PR_reference, PR_designation, UM_ident)
values('P01R9000', 'truc 01 vendu à la pièce', 3)
,('M01R9000', 'truc 01 vendu à la centaine', 4)
,('C01R9000', 'truc 01 vendu au millier', 5)
,('K02V1510', 'truc 02 vendu au kilo', 1)
,('G02V1510', 'truc 02 vendu au gramme', 2)
;
select * from PR_produit
;
CREATE TABLE CN_convertir(
UM_ident_de INT,
UM_ident_vers INT,
CN_facteur DECIMAL(9,3) NOT NULL,
PRIMARY KEY(UM_ident_de, UM_ident_vers),
FOREIGN KEY(UM_ident_de) REFERENCES UM_unite_mesure(UM_ident),
FOREIGN KEY(UM_ident_vers) REFERENCES UM_unite_mesure(UM_ident)
);
insert into CN_convertir
(UM_ident_de, UM_ident_vers, CN_facteur)
values(1, 2, 1000)
,(2, 1, 0.001)
,(3, 4, 0.01)
,(4, 3, 100)
,(4, 5, 0.001)
,(5, 4, 1000)
;
select UM1.UM_code
, UM1.UM_libelle
, CN.CN_facteur
, UM2.UM_code
, UM2.UM_libelle
from CN_convertir as CN
left join UM_unite_mesure as UM1
on UM1.UM_ident = UM_ident_de
left join UM_unite_mesure as UM2
on UM2.UM_ident = UM_ident_vers
;
CREATE TABLE CO_contenir(
PR_ident INT,
PR_ident_1 INT,
CO_quantite DECIMAL(9,3) NOT NULL,
PRIMARY KEY(PR_ident, PR_ident_1),
FOREIGN KEY(PR_ident) REFERENCES PR_produit(PR_ident),
FOREIGN KEY(PR_ident_1) REFERENCES PR_produit(PR_ident)
);
insert into CO_contenir
(PR_ident, PR_ident_1, CO_quantite)
values (2, 1, 100)
;
select * from CO_contenir |
Partager