Vous êtes sur la page 1sur 4

create database bdd_achat;

use bdd_achat;

-- I.2.1 Creation des tables :

create table client(numcli int primary key auto_increment,


nomcli varchar(12), ville varchar(12),
categorie char(1),compte decimal(7,2));
-- auto_increment vient après PRIMARY KEY !!!
create table commande(numcom int primary key auto_increment, numcli int, datecom
date);
create table produit(numpro int primary key auto_increment, typepro varchar(12),
nompro varchar (12), prix int, qstock int);

-- On crée DETAIL avec clé etrangères ssi déjà défini les clés primaires
-- des autres tables auparavant !!!!!
create table detail(numcom int, numpro int, qcom int,
primary key(numcom,numpro),
foreign key (numcom) references commande(numcom),
foreign key (numpro) references produit(numpro));

Show tables;

--------- Nomcli et Ville sont non nuls ---------------


Alter table client modify nomcli varchar(12) not null;
Alter table client modify ville varchar(12) not null;

-------- Le prix est supérieur ou égal à 0 ---------


alter table produit add constraint check(prix>=0);

------- Le domaine des valeurs de qtock et qcom s'étend de 0 à 1000 ------


alter table produit add constraint check(qtock between 0 and 1000);
alter table detail add constraint check(qcom between 0 and 1000);

--- I.2.2 °° Ajout, Retrait, Modif, d'une colonne :

alter table produit add Poids int; alter table produit drop Poids;

alter table produit modify nompro varchar(25); --Changer type colonne


alter table produit modify qstock int default 0; --Ajout valeur par défaut
alter table client change categorie cat char(1); --Changer de nom colonne
Rename table CLIENT to Customer; ------------------Renommer une Table

Describe Customer;

--Insertion des données :

INSERT INTO Produit(typepro,nompro,prix,qstock)


values ('Audio','CasqueM',750,35),
('Informatique','Ordinateur fixe',5400,960),
('Mobilité','Tablettes',105,830),
('Informatique','PC portables',9500,134),
('Audio','casque Hi-Fi',1500,82),
('Informatique','Disques Durs',985,129),
('Mobilité','Smartphones',220,540);

INSERT INTO customer(nomcli,ville,cat,compte)


values ('Amine','Rabat','A',11250),('Driss','Casablanca','A',12300),
('Imane','Marrakech','D',20),('Yassine','Tanger','C',100),
('Zineb','Casablanca','B',1000),('Marouane','Kenitra','D',15),
('Houda','Rabat','A',15000),('Ali','Tanger',null,0),
('Imane','Rabat','B',1500),('Driss','Casablanca',null,0);

INSERT INTO commande(numcom,numcli,datecom) values


(1,3,'2015-01-01'),(2,9,'2015-05-10'),(3,3,'2015-09-19'),
(4,1,'2015-09-17'),(5,3,'2015-10-27'),(6,6,'2015-10-09'),
(7,2,'2015-01-01'),(8,8,'2015-05-10'),(9,5,'2015-07-19'),
(10,2,'2015-09-17'),(11,6,'2015-10-27'),(12,10,'2015-10-09');

INSERT INTO detail(numcom,numpro,qcom) values (1,1,33),(2,2,25),


(2,3,70),(3,2,40),(4,1,133),(4,4,20),(5,1,26),(5,2,10),(5,5,60),
(6,4,157),(7,1,18),(7,2,75),(7,4,2),(7,6,920);

----Modification des données : --------------------------------

Update produit set nompro='Clé USB', prix=100 WHERE numpro=6;


Update client set compte=compte+1000 WHERE cat='A';

Rename table customer to client; --plus à l'aise à manipuler


Delete from client WHERE nomcli='Houda';
-- Diminuer de 10% les comptes supérieurs à 5000dh ----
UPDATE client SET compte=compte*0.9
WHERE compte>5000;

-----TP3 : Jointures -----------------------------------------


1.3.1 :
select * from produit;
select numcli,nomcli,ville from client where categorie='B' and ville <>'Rabat';
select * from produit where typepro='Informatique';
select * from produit where nompro like '%casque%';

select numcli,nomcli,compte from client


where ville in('Tanger','Kenitra') and compte>=0;

select distinct categorie from client where ville='Casablanca' and categorie is not
null;
select numcli,nomcli,ville from client where nomcli<ville;
select nomcli from client where compte>10000 order by nomcli asc;
1.3.2
select * from client where ville in ('Rabat','Casablanca');
select * from client where ville='Rabat' or ville='Casablanca';
select * from client where ville not in('Rabat','Casablanca');
select * from client where categorie='D' and ville='Marrakech';
select * from client where categorie='C' or ville='Casablanca';
select * from client where categorie='C' and ville<>'Casablanca';
--Contraire de la question précedente C(AnB)=C(A)UC(B)
select * from client where categorie<>'C' or ville='Casablanca';
-- A ou B ou les deux :
SELECT * from client where categorie in('B','C') or ville in('Rabat','Kenitra');
-- A ou bien B :
select * from client where (categorie in ('B','C') and ville not in
('Rabat','Kenitra'))
or (categorie not in ('B','C') and ville in
('Rabat','Kenitra')) ;

select * from client where categorie in('B','C') and ville


in('Tanger','Casablanca');
--Qui n'ont pas été affiché dans requete précedente :
select * from client where (categorie<>'B' and categorie<>'C') or categorie is null
or ville not in('Tanger','Casablanca');
---Num des client qui ont commmandé produit 4:
select numcli from commande
where numcom in(select numcom from detail
where numpro=4);
---villeclients qui ont commandé prod 6 :
select ville from client
where numcli in(select numcli from commande
where numcom in(select numcom from detail
where numpro=6));
---Casque qui font objet d'une commande :
select nompro from produit
where numpro in(select numpro from detail)
and nompro like '%casqu%';
-- Ville, commande octobre 2015
select ville from client where numcli in (select numcli from commande where
datecom like '2015-10%');

select nomcli from client


where numcli in (select numcli from commande
where numcom in (select numcom from detail
where numpro in (select numpro from produit where prix>700)));

select nompro from produit where numpro


in (select numpro from detail where numcom
in (select numcom from commande where numcli
in (select numcli from client where categorie='B')));

---TP4 : Jointures :
141
select numcli from commande co, detail de
where co.numcom=de.numcom
and numpro=4;

select ville from client cl, commande co, detail de


where cl.numcli=co.numcli
and co.numcom=de.numcom
and numpro=6;
-- Les casques commandés :
select distinct nompro from produit pr,detail de
where pr.numpro=de.numpro
and nompro like '%casq%';
--Ville ou on a passé des commandes en Octobre
select distinct ville from client cl,commande co
where cl.numcli=co.numcli
and datecom like '2015-10%' ;
--Valeur total des stock
select sum(qstock*prix) as 'Valeur Totale' from produit;
--Cb de Ville .. cat D
select count(distinct ville) from client where categorie='D';
--Total,Min,Moyenne, Max comptes :
select sum(compte) as 'Total', min(compte) as 'Minimum',
max(compte) as 'Maximum', avg(compte) as 'Moyenne'
from client;
--Quantificateurs ensembliste
SELECT numpro, nompro,prix from produit
where prix<any(select prix from produit natural join detail where numcom=2);
SELECT numpro, nompro,prix from produit
where prix<all(select prix from produit natural join detail where numcom=3);

SELECT numcli,nomcli from client where numcli not in (select numcli from commande)
and ville='rabat';
select distinct cl.numcli,nomcli from client cl,commande co where
cl.numcli<>all(select numcli from commande)and ville='rabat';

--Les clients qui n'ont jamais commandé de casques :


select distinct numcli,nomcli from client
where numcli not in (select numcli from commande co,detail de ,produit pr
where co.numcom=de.numcom and de.numpro=pr.numpro
and nompro like '%casque%') ;

select distinct ville from client cl where cl.numcli =any(select co.numcli from
commande co,detail de where co.numcom=de.numcom and numpro=3) ;

select ville from client where ---tan rej3o lih !!!

--Montant des produits info commandés ;


select sum(qcom*prix) as 'TOTAL info' from produit pr, detail de
where pr.numpro=de.numpro
and typepro like '%informatiq%';

---Cb y a til de commandes de produit


select count(distinct co.numcom) from commande co,detail de, produit pr
where co.numcom=de.numcom
and de.numpro=pr.numpro
and typepro like '%info%';

Vous aimerez peut-être aussi