You are on page 1of 7

create database TPSQL;

use TPSQL;

create table ETUDIANT(


matricule char (10) not null,
nom varchar (20) not null,
prenom varchar (30) not null,
tel int not null,
date_naiss date not null,
primary key (matricule)
);

create table COURS (


code_cours char (10) not null,
libelle varchar (30) not null,
volume_horaire tinyint not null,
primary key (code_cours)
);

desc COURS;
desc ETUDIANT;

create table INSCRIPTION (


matricule char (10) not null,
code_cours char (10) not null,
note double not null,
primary key (matricule, code_cours),
constraint F_KEY foreign key (matricule) references ETUDIANT (matricule),
constraint F_HEY foreign key (code_cours) references COURS (code_cours)
);

insert into ETUDIANT


values ('2000245', 'BAH', 'IBRAHIMA SORY', '624-34-38-32', '2003-04-29');

insert into ETUDIANT


values ('2100213', 'SOW', 'ALPHA OUMAR', '620-40-43-22', '2001-04-01');

insert into ETUDIANT


values ('2100076', 'DIALLO', 'ALHASSANE', '620-57-43-75', '2002-04-03');

insert into ETUDIANT


values ('2100107', 'DIALLO', 'MAMADOU SITA', '625-76-32-94', '2002-01-01');

insert into ETUDIANT


values ('2100060', 'BAH', 'IBRAHIMA', '624-10-07-39', '2002-06-18');

insert into ETUDIANT


values ('ET210002', 'BAH', 'null', '624-10-07-39', '2002-06-18');

alter table ETUDIANT


modify column tel varchar(15) unique not null;

select * from ETUDIANT;

desc ETUDIANT;

-- POUR LA MISE A JOUR 'UPDATE'


update ETUDIANT
set nom='DIALLO'
where matricule='2100245';

select nom, prenom


from ETUDIANT;

create database vente;

use vente;

create table produit(


codep char(10) not null primary key,
lib varchar(30) not null,
pu decimal(8,3) not null,
qtes int not null,
seuil int not null);

desc produit;

create table clientc(


codc char(10) not null primary key,
nomc varchar(30) not null,
creditc numeric not null,
adrc varchar(30) not null);

create table commande(


numc char(10) not null primary key,
codc char(10) not null,
montc numeric(7,3) not null,
datc date not null,
constraint A_KEY foreign key (codc) references clientc(codc));

create table facture(


numf char(10) not null primary key,
codc char(10) not null,
montf decimal(8,3),
datf date,
constraint B_KEY foreign key (codc) references clientc(codc));

create table PC(


codep char(10) not null,
numc char(10) not null,
qtec int not null,
primary key (codep, numc),
constraint C_KEY foreign key (codep) references produit(codep),
constraint D_KEY foreign key (numc) references commande(numc));

insert into produit values ('2','Ecran',400,15,5);


insert into produit values ('5','Clavier',25,40,10);
insert into produit values ('3','CD-ROM',150,20,3);
insert into produit values ('9','Souris',5,100,20);
insert into produit values ('10','Imprimante',500,50,8);

insert into clientc values ('1250','Mohamed',50,'Tunis');


insert into clientc values ('1360','Ali',400,'Sousse');
insert into clientc values ('1580','Adel',250,'Adel');
insert into clientc values ('1210','Fatma',20,'Sfax');
insert into clientc values ('1000','Slim',120,'Kef');
insert into clientc values ('1200','Sami',50,'Monastir');
insert into clientc values ('1400','Mahmoud',200,'Zagouan');

insert into commande values ('10','1250',0,'1999-07-14');


insert into commande values ('220','1210',0,'2000-11-10');
insert into commande values ('40','1200',0,'2001-08-15');
insert into commande values ('100','1400',0,'2003-10-20');
insert into commande values ('300','1250',0,'2001-11-20');
insert into commande values ('50','1400',0,'2002-09-12');

insert into facture values ('10','1250',null,'1999-07-16');


insert into facture values ('220','1210',null,'2000-11-12');
insert into facture values ('40','1200',null,'2001-08-17');
insert into facture values ('100','1400',null,'2003-10-22');
insert into facture values ('300','1250',null,'2001-11-22');
insert into facture values ('50','1400',null,'2002-09-14');

insert into PC values ('2','10',200);


insert into PC values ('5','10',100);
insert into PC values ('9','10',300);
insert into PC values ('2','220',100);
insert into PC values ('9','40',500);
insert into PC values ('10','40',100);
insert into PC values ('3','40',300);
insert into PC values ('10','100',100);
insert into PC values ('5','300',70);
insert into PC values ('10','300',100);
insert into PC values ('3','50',40);

select * from produit;


select * from clientc;
select * from commande;
select * from facture;
select * from PC;

-- select codep, lib, pu*qtes as tab


-- from produit;

select codep, lib, pu*qtes


from produit;
select codc from facture;

select distinct codc from facture;

-- Pour la concaténation

select concat (codep, lib) from produit;

-- En renommant

select concat (codep, lib) as lib from produit;

select codc, datf from facture where codc = '1250';

select * from clientc where adrc = 'TuNis';

-- select * from facture where datf = '1999/07/16';

-- select * from clientc where creditc between 50 and 250;


-- oubien where creditc >= 50 and creditc <= 250;

select * from produit where qtes in (20, 40, 50);

-- Pour la seconde lettre


select * from clientc where nomc like '_A%';

-- Client dont le nom se termine par m


select * from clientc where nomc like '%m';

-- Client qui n'ont pas d'adresse

select * from clientc where adrc is null;

-- APPLICATION

-- 1 facile

-- 2

select * from clientc;

select * from clientc where adrc = 'Tunis' or codc mod 1000 = 0;

select * from clientc where adrc = 'Tunis' and codc mod 1250 = 0;

-- 3

select codc from commande where numc = 1;

-- 4
select * from produit;
select * from produit where qtes between 500 and 700 and seuil is not null;

-- 5
select * from clientc where nomc like '%ed';

-- 6
select * from produit where lib like '__p%';
-- 7
select * from facture;

select * from facture where datf >= '1999-01-01' and datf <= '1999-12-31';
-- oubien
select * from facture where (year(datf) = 1999);

-- 8

select codc from commande;

-- 9

select * from commande where datc >= '1998-03-20';

-- 10

select * from produit order by lib;

-- 11

-- select * from produit order by pu, qtes(desc);

-- 12

select * from produit;

select concat (codep, lib) from produit;

select concat(codc, ' ne sont pas ', nomc) from clientc;

select concat('La facture numéro ', numf, ' du client ', codc, ' est de 1000 $')
from facture;

-- FONCTIONS SUR LES CHAINES DE CARACTERE


-- Nbre de bits de la chaine
select bit_length('victoire ');

select char_length('victoire ');

select length('victoire ');

select strcmp('beta', 'Beta');

-- APPLICATION
-- 1
select * from clientc where char_length(nomc) > 5;

select repeat('BA',3);

select lpad('alpha', 3, '#');


-- Ajouter à gauche ce qui manque
select lpad('alpha', 7, '#');

-- Ajouter à droite ce qui manque

select rpad('alpha', 7, '#');

desc clientc;

-- 4

select trim(' temperature ');


-- leading supprime avant la chaine
-- select trim (leading from ' temperature ');

-- trailing supprimer arprès la chaine


-- select trim (trailing from ' temperature ');

-- select trim (leading 't' from ' temperature ');

select substring('alpha',2);
select substring('alpha',3);

select substring('alpha',-2);
select substring('alpha',-3);

select substring('alpha',2,2);

select substring('alpha',2,7);

select instr ('alphakaba', 'lp');

select lower('ALPHAKABA');

select upper('alPHAKABA');

select insert('killa',5,3,'one');

-- APPLICATION
use vente;
-- 1
select * from clientc where char_length(nomc) > 5;
-- 2

select * from clientc where substr (nomc,1, 3) is not null;

select substr (nomc,1, 3) from clientc;

select substr('amdoudian',1, 3);

-- 3
select * from produit where instr(lib, 'a') = 4 or instr(lib, 'a') = 3 or
instr(lib, 'a') = 7;

-- 4

select adrc from clientc where lpad(adrc, 3, '#') is not null and rpad(adrc, 12,
'*') is not null;
-- 5

-- select * from clientc where trim (trailing ' ' from adrc) is not null;

-- 6

select adrc from clientc where lower(substring(adrc, 1, 1)) is not null and
upper(substring (adrc)) is not null;

-- 7

select * from clientc where upper(adrc) is not null or lower(adrc) is not null;

-- 8
-- select * from clientc where ();

select substr ('aliou',2,3);

use vente;
-- PRODUIT CARTESIEN
select * from clientc, commande;

-- Jointure

-- Liste des clients qui ont passé des commandes, ainsi que leurs commandes
select * from clientc, commande
where clientc.codc = commande.codc;

-- Liste des factures par clients

select * from clientc, facture


where clientc.codc = facture.codc;

-- autre forme

select * from clientc inner join facture


on clientc.codc = facture.codc;

select * from clientc, facture


where clientc.codc < facture.codc;

-- Pour chaque client donner la liste des produits qu'il a commandé

select C.codc, nomc, P.codep, lib, pu from clientc C, commande CM, produit P, pc
where C.codc = CM.codc and CM.numc = pc.numc and pc.codep = P.codep;

-- SEMI JOINTURE

select C.codc, nomc, adrc from clientc C, commande;

You might also like