0% ont trouvé ce document utile (0 vote)
114 vues65 pages

Chap 4

Le document présente le langage SQL, qui est divisé en langage de manipulation de données et langage de définition de données. Il décrit des opérations SQL comme la projection, le produit cartésien, la restriction et la jointure.

Transféré par

Ahmed El Mokadem
Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PDF, TXT ou lisez en ligne sur Scribd
0% ont trouvé ce document utile (0 vote)
114 vues65 pages

Chap 4

Le document présente le langage SQL, qui est divisé en langage de manipulation de données et langage de définition de données. Il décrit des opérations SQL comme la projection, le produit cartésien, la restriction et la jointure.

Transféré par

Ahmed El Mokadem
Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PDF, TXT ou lisez en ligne sur Scribd

Introduction

Langage SQL : Langage de Manipulation de Données


Langage SQL : Langage de Définition de Données

Introduction aux Bases de Données


Chapitre 4: Langages de requêtes / SQL

Prof. Abdeslam Aannaque


[email protected]

Ecole Mohammedia d’Ingénieurs

2012

Prof. Aannaque Introduction aux Bases de Données


Introduction
Langage SQL : Langage de Manipulation de Données
Langage SQL : Langage de Définition de Données

Plan

1 Introduction

Prof. Aannaque Introduction aux Bases de Données


Introduction
Langage SQL : Langage de Manipulation de Données
Langage SQL : Langage de Définition de Données

Plan

1 Introduction

2 Langage SQL : Langage de Manipulation de Données

Prof. Aannaque Introduction aux Bases de Données


Introduction
Langage SQL : Langage de Manipulation de Données
Langage SQL : Langage de Définition de Données

Plan

1 Introduction

2 Langage SQL : Langage de Manipulation de Données

3 Langage SQL : Langage de Définition de Données

Prof. Aannaque Introduction aux Bases de Données


Introduction
Langage SQL : Langage de Manipulation de Données
Langage SQL : Langage de Définition de Données

Plan

1 Introduction

Prof. Aannaque Introduction aux Bases de Données


Introduction
Langage SQL : Langage de Manipulation de Données
Langage SQL : Langage de Définition de Données

Le Langage SQL

Le langage SQL se divise en deux parties :


Langage de Manipulation de Données (DML),
Langage de Définition de Données (DDL).

Prof. Aannaque Introduction aux Bases de Données


Introduction
Langage SQL : Langage de Manipulation de Données
Langage SQL : Langage de Définition de Données

Schéma

Tables
Departement (DeptID,NomDept)
Etudiant (EtudiantID,NomEtudiant, PrenomEtudiant, Email,
DeptID,Sexe,MoyenneCNC)

Prof. Aannaque Introduction aux Bases de Données


Introduction
Langage SQL : Langage de Manipulation de Données
Langage SQL : Langage de Définition de Données

Outils

Microsoft Access
Database Master 4
http://www.nucleonsoftware.com/
MySQL

Prof. Aannaque Introduction aux Bases de Données


Introduction
Langage SQL : Langage de Manipulation de Données
Langage SQL : Langage de Définition de Données

Données

F IGURE : Table Departement

Prof. Aannaque Introduction aux Bases de Données


Introduction
Langage SQL : Langage de Manipulation de Données
Langage SQL : Langage de Définition de Données

Données

F IGURE : Table Etudiant

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Plan

2 Langage SQL : Langage de Manipulation de Données

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Le Langage SQL

REQUETE SQL DE LA FORME :


SELECT <Liste_Résultats>
FROM <Liste de Tables>
WHERE <Condition> ;

Pour avoir la liste de tous les champs de la table :


SELECT *
FROM <Liste de Tables>
WHERE <Condition> ;

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Projection

SELECT [DISTINCT] liste_des_attributs_de_projection


FROM Nom_Relation ;
Exemple 1 : Liste des DeptIDs dans la la table Etudiant
SELECT DISTINCT DeptID
FROM Etudiant ;

Exemple 2 : Cotenu de la table Etudiant


SELECT * FROM Etudiant ;

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Projection

SELECT DISTINCT DeptID FROM Etudiant ;

F IGURE : Liste des DeptIDs dans la la table Etudiant

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Projection
SELECT * FROM Etudiant ;

F IGURE : Cotenu de la table Etudiant

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Produit cartésien

SELECT *
FROM Nom_de_relation1, Nom_de_relation2 ;
Exemple : Produit cartésien des tables Etudiant et Departement
SELECT *
FROM Etudiant, Departement ;

Prof. Aannaque Introduction aux Bases de Données


Produit cartésien
SELECT * FROM Etudiant, Departement ;

F IGURE : Produit cartésien


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Restriction

SELECT *
FROM Nom_De_Relation
WHERE <Condition de restriction> ;
Exemple 1 : Liste des étudiants du département dont le DeptId
est 3
SELECT * FROM Etudiant WHERE DeptId=3 ;

Exemple 2 : Liste des étudiants n’ayant pas d’email


SELECT * FROM Etudiant WHERE Email IS NULL ;

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Restriction

SELECT * FROM Etudiant WHERE DeptId=3 ;

F IGURE : Liste des étudiants du département dont le DeptId est 3

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Restriction

SELECT * FROM Etudiant WHERE Email IS NULL ;

F IGURE : Liste des étudiants n’ayant pas d’email

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Jointure
SELECT *
FROM Nom_de_relation1, Nom_de_relation2
WHERE Condition_de_jointure ;
Exemple : Première forme de jointure
SELECT *
FROM Etudiant E, Departement D
WHERE E.DeptID=D.DeptID ;
SELECT E.NomEtudiant, E.PrenomEtudiant, D.NomDept
FROM Etudiant E, Departement D
WHERE E.DeptID=D.DeptID ;

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Jointure
SELECT * FROM Etudiant E, Departement D WHERE
E.DeptID=D.DeptID ;

F IGURE : Première forme de jointure

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Jointure
SELECT *
FROM Nom_de_relation1
WHERE Nom_Attribut1 IN
(SELECT Nom_attribut2
FROM Relation2) ;
Exemple : Liste de départements ayant des étudiants
SELECT *
FROM Departement
WHERE DeptID IN
(SELECT DeptID FROM Etudiant) ;

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Jointure

SELECT * FROM Departement WHERE DeptID IN (SELECT


DeptID FROM Etudiant) ;

F IGURE : Liste de départements ayant des étudiants

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Jointure
SELECT *
FROM Nom_de_relation1
WHERE Nom_Attribut1 NOT IN
(SELECT Nom_attribut2
FROM Relation2) ;
Exemple : Liste de départements n’ayant pas des étudiants
SELECT *
FROM Departement
WHERE DeptID NOT IN
(SELECT DeptID FROM Etudiant) ;

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Jointure

SELECT * FROM Departement WHERE DeptID NOT IN


(SELECT DeptID FROM Etudiant) ;

F IGURE : Liste de départements n’ayant pas d’étudiant

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Jointure

SELECT FROM <table gauche> [INNER]JOIN <table droite>


ON <condition de jointure>
Exemple : Autre forme de jointure
SELECT E.NomEtudiant, E.PrenomEtudiant, D.NomDept
FROM Etudiant E INNER JOIN Departement D
ON E.DeptID=D.DeptID ;

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Jointure
SELECT E.NomEtudiant, E.PrenomEtudiant, D.NomDept FROM Etudiant E INNER JOIN Departement D ON
E.DeptID=D.DeptID ;

F IGURE : Autre forme de jointure

Prof. Aannaque Introduction aux Bases de Données


Jointure

SELECT ... FROM <table gauche> LEFT | RIGHT | FULL


OUTER JOIN <table droite> ON condition de jointure
Exemple 1 : Liste de tous les étudiants
SELECT E.NomEtudiant, E.PrenomEtudiant, D.NomDept
FROM Etudiant E LEFT JOIN Departement D
ON E.DeptID=D.DeptID ;

Exemple 2 : Liste de tous les départements


SELECT E.NomEtudiant, E.PrenomEtudiant, D.NomDept
FROM Departement D LEFT JOIN Etudiant E
ON E.DeptID=D.DeptID ;
Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Jointure
SELECT E.NomEtudiant, E.PrenomEtudiant, D.NomDept FROM Etudiant E LEFT JOIN Departement D ON
E.DeptID=D.DeptID ;

F IGURE : Liste de tous les étudiants

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Jointure
SELECT E.NomEtudiant, E.PrenomEtudiant, D.NomDept FROM Etudiant E RIGHT JOIN Departement D ON
E.DeptID=D.DeptID ;

F IGURE : Liste de tous les départements

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Différence
SELECT Liste_attributs
FROM Nom_De_Relation1
MINUS
SELECT Liste_attributs
FROM Nom_De_Relation2
Exemple : Liste de départements n’ayant pas d’étudiant
SELECT DeptID
FROM Departement
MINUS
SELECT DeptID FROM Etudiant E ;

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Différence
SELECT Liste_attributs FROM Nom_De_Relation1 R1
WHERE NOT EXISTS
(SELECT * FROM Nom_De_Relation2 R2
WHERE R2.Nom_attribut =R1.Nom_attribut)
Exemple : Liste de départements n’ayant pas d’étudiant
SELECT NomDept
FROM Departement D
WHERE NOT EXISTS
(SELECT DeptID FROM Etudiant E
WHERE D.DeptID =E.DeptID) ;

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Différence
SELECT NomDept FROM Departement D WHERE NOT
EXISTS (SELECT DeptID FROM Etudiant E WHERE D.DeptID
=E.DeptID) ;

F IGURE : Liste de départements n’ayant pas d’étudiant

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Différence
SELECT Liste_attributs FROM Nom_De_Relation1
WHERE Nom_attribut
NOT IN
( SELECT Nom_attribut FROM Nom_De_Relation2
[WHERE <Condition de restriction>]) ;
Exemple : Liste de départements n’ayant pas des étudiants
SELECT NomDept FROM Departement D
WHERE DeptID NOT IN
(SELECT DeptID
FROM Etudiant E WHERE E.DeptID=D.DeptID) ;

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Différence
SELECT NomDept FROM Departement D WHERE DeptID
NOT IN (SELECT DeptID FROM Etudiant E WHERE
E.DeptID=D.DeptID) ;

F IGURE : Liste de départements n’ayant pas d’étudiant

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Union
SELECT Liste_attributs FROM Nom_De_Relation1 [WHERE
<Condition de restriction> ]
UNION [ALL]
SELECT Liste_attributs FROM Nom_De_Relation2 [WHERE
<Condition de restriction>]
Exemple : Liste de toutes les factures (Client et Fournisseur)
SELECT NumFact, DateFact, Montant,’Facture Client’
FROM FClient
UNION
SELECT NumFact, DateFact, Montant,’Facture Fournisseur’
FROM FFournisseur
Prof. Aannaque Introduction aux Bases de Données
Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Union

Exemple : Liste étudiants Mineral ou Civil


SELECT * FROM Etudiant WHERE DeptID=1 OR DeptID=3

Exemple : Liste étudiants Mineral ou Civil


SELECT ’Civil’ as Departement,* FROM Etudiant
WHERE DeptID=1
UNION
SELECT ’Minéral’,* FROM Etudiant
WHERE DeptID=3 ;

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Union
SELECT * FROM Etudiant WHERE DeptID=1 OR DeptID=3
E.DeptID=D.DeptID) ;

F IGURE : Liste de départements n’ayant pas d’étudiant

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Union
SELECT ’Civil’ as Departement,* FROM Etudiant
WHERE DeptID=1
UNION
SELECT ’Minéral’,* FROM Etudiant
WHERE DeptID=3 ;

F IGURE : Liste de départements n’ayant pas d’étudiant

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Union
Quelle différence entre
Exemple :
SELECT Sexe FROM Etudiant WHERE DeptID=1
UNION ALL
SELECT Sexe FROM Etudiant WHERE DeptID=3 ;

Exemple :
SELECT Sexe FROM Etudiant WHERE DeptID=1
UNION
SELECT Sexe FROM Etudiant WHERE DeptID=3 ;

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Intersection

SELECT Liste_attributs FROM Nom_De_Relation1


[WHERE <Condition de restriction> ]
INTERSECT
SELECT Liste_attributs FROM Nom_De_Relation2
[ WHERE <Condition de restriction> ]
Exemple : Dates ou il y a eu vente sur Internet et dans les magasins
SELECT Date FROM Ventes_Magasin
INTERSECT
SELECT Date FROM Ventes_Internet

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Fonctions
Les fonctions suivantes s’appliquent sur les ensembles :
Moyenne : AVG([DISTINCT|ALL] expr)
Comptage : COUNT([DISTINCT|ALL]*|expr)
Maximum : MAX([DISTINCT|ALL] expr)
Minimum : MIN([DISTINCT|ALL] expr)
SUM([DISTINCT|ALL] expr)
Exemple :
SELECT COUNT(*) FROM Etudiant ;
SELECT COUNT(DISTINCT DeptId) FROM Etudiant ;
SELECT MAX(MoyenneCNC) FROM Etudiant ;
Prof. Aannaque Introduction aux Bases de Données
Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Fonctions

Exemple : Etudiant ayant eu la plus grande moyenne obtenue


au CNC
SELECT NomEtudiant , PrenomEtudiant, NomDept
FROM Etudiant E, Departement D
WHERE E.DeptID = D.DeptID AND
MoyenneCNC =
(SELECT MAX(MoyenneCNC)
FROM Etudiant) ;

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Fonctions
SELECT NomEtudiant , PrenomEtudiant, NomDept
FROM Etudiant E, Departement D
WHERE E.DeptID = D.DeptID AND
MoyenneCNC =
(SELECT MAX(MoyenneCNC)
FROM Etudiant) ;

F IGURE : Etudiant ayant eu la plus grande moyenne obtenue au CNC

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Fonctions

Exemple : Etudiant ayant eu la plus grande moyenne obtenue


au CNC
SELECT EtudiantID,Moyennecnc FROM Etudiant E1 WHERE
NOT EXISTS (SELECT EtudiantID FROM etudiant E2 WHERE
E1.MoyenneCnc<E2.MoyenneCnc)

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Fonctions

SELECT EtudiantID,Moyennecnc FROM Etudiant E1 WHERE NOT EXISTS (SELECT EtudiantID FROM etudiant
E2 WHERE E1.MoyenneCnc<E2.MoyenneCnc)

F IGURE : Etudiant ayant eu la plus grande moyenne obtenue au CNC

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Regroupement
SELECT Liste_attributs
FROM Nom_De_Relation
GROUP BY Liste_attributs_de_regroupement
Exemple 1 : Nombre d’étudiants par département
SELECT DeptID, Count(*) FROM Etudiant
GROUP BY DeptID ;

Exemple 2 : Moyenne CNC par département


SELECT DeptID, Avg(MoyenneCNC) FROM Etudiant
GROUP BY DeptID ;

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Fonctions

SELECT DeptID, Count(*) FROM Etudiant GROUP BY DeptID ;

F IGURE : Nombre d’étudiants par département

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Fonctions

SELECT DeptID, Avg(MoyenneCNC) FROM Etudiant GROUP


BY DeptID ;

F IGURE : Moyenne CNC par département

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Regroupement

SELECT Liste_attributs
FROM Nom_De_Relation
GROUP BY Liste_attributs_de_regroupement
HAVING Condition
Exemple : Moyenne CNC par département supérieures à 10
SELECT DeptID, avg(moyennecnc)
FROM Etudiant
GROUP BY DeptID
HAVING AVG(moyennecnc)>10 ;

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Fonctions

SELECT DeptID, avg(moyennecnc) FROM Etudiant GROUP


BY DeptID HAVING AVG(moyennecnc)>10 ;

F IGURE : Moyenne CNC par département supérieures à 10

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Exemple

Différence entre la moyenne du département génie Civil et le


département génie Minéral
SELECT civil.Moyenne - Mineral.Moyenne AS DiffCivMin
FROM
(SELECT AVG(MoyenneCNC) AS Moyenne
FROM Etudiant WHERE DeptId=1) AS Civil,
(SELECT AVG(MoyenneCNC) AS Moyenne
FROM Etudiant WHERE DeptId=3) AS Mineral

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Exemple
SELECT civil.Moyenne - Mineral.Moyenne AS DiffCivMin
FROM
(SELECT AVG(MoyenneCNC) AS Moyenne
FROM Etudiant WHERE DeptId=1) AS Civil,
(SELECT AVG(MoyenneCNC) AS Moyenne
FROM Etudiant WHERE DeptId=3) AS Mineral

F IGURE : Différence entre la moyenne du département génie Civil et


le département génie Minéral

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Exemple

Différence entre la moyenne du département génie Civil et le


département génie Minéral
SELECT(
(SELECT (AVG(MoyenneCNC) AS Moyenne
FROM Etudiant
WHERE DeptId=1) –
(SELECT AVG(MoyenneCNC) AS Moyenne
FROM Etudiant
WHERE DeptId=3)
);

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Insertion

INSERT INTO <Nom table >


VALUES (<value 1>, ... <value n>) ;
Exemple :
INSERT INTO Produit
VALUES(’2000’,’Huile de Table’,60,10)

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Mise à jour

UPDATE <Nom table >


SET <attribute> = <expression>
WHERE <condition> ;
<expression> peut être une requête
Exemple : Augmenter les prix de 10% de tous les produits
moins de 1000 DH
UPDATE Produit
SET Prix=prix*1.1
WHERE Prix<1000

Prof. Aannaque Introduction aux Bases de Données


Projection
Produit cartésien
Restriction
Introduction
Jointure
Langage SQL : Langage de Manipulation de Données
Différence
Langage SQL : Langage de Définition de Données
Union
Intersection
Regroupement

Suppression

DELETE FROM <name table>


WHERE <condition> ;
Exemple : Supprimer tous les produits dont Qte=0
DELETE FROM Produit
WHERE Qte=0
! !Attention si aucune condition n’est spécifiée tous les
enregistrements seront supprimés

Prof. Aannaque Introduction aux Bases de Données


Création de table
Introduction Contraintes d’Intégrité
Langage SQL : Langage de Manipulation de Données Index
Langage SQL : Langage de Définition de Données Modification de table
Suppression

Plan

3 Langage SQL : Langage de Définition de Données

Prof. Aannaque Introduction aux Bases de Données


Création de table
Introduction Contraintes d’Intégrité
Langage SQL : Langage de Manipulation de Données Index
Langage SQL : Langage de Définition de Données Modification de table
Suppression

Création de table
CREATE TABLE Nom_de_table
( Nom_attribut1 TYPE [NOT NULL]
[, Nom_attribut2 TYPE [NOT NULL]]
...
[, Nom_attributn TYPE [NOT NULL]]
[CONSTRAINT nomcontrainte1 Typecontrainte1]
[CONSTRAINT nomcontrainte2 Typecontrainte2]
... );
Types de données : CHAR (n [BYTE | CHAR]), VARCHAR2 (n [
BYTE | CHAR]), CLOB, NLOB, LONG, NUMBER ( n[,d]),
DATE, INTERVAL YEAR (an) TO MONTH, INTERVAL DAY (jo)
TO SECOND (fsec), BLOB, BFILE, RAW( taille), LONG RAW
Prof. Aannaque Introduction aux Bases de Données
Création de table
Introduction Contraintes d’Intégrité
Langage SQL : Langage de Manipulation de Données Index
Langage SQL : Langage de Définition de Données Modification de table
Suppression

Contraintes d’Intégrité

CONSTRAINT nomcontrainte PRIMARY KEY (Nom_attribut1[,


Nom_attribut2] ... )
CONSTRAINT nomcontrainte FOREIGN KEY (Nom_attribut1[,
Nom_attribut2] ... )
REFERENCES Nom_table [(nom_attribut)]
[ ON DELETE CASCADE | SET NULL ]
[ ON UPDATE CASCADE]
CONSTRAINT nomcontrainte CHECK ( Condition)
CONSTRAINT nomcontrainte UNIQUE (Nom_attribut1[,
Nom_attribut2]...)

Prof. Aannaque Introduction aux Bases de Données


Création de table
Introduction Contraintes d’Intégrité
Langage SQL : Langage de Manipulation de Données Index
Langage SQL : Langage de Définition de Données Modification de table
Suppression

Index

CREATE [UNIQUE] INDEX Nom_Index


ON Nom_de_Table ( Nom_attribut1,....)

Prof. Aannaque Introduction aux Bases de Données


Création de table
Introduction Contraintes d’Intégrité
Langage SQL : Langage de Manipulation de Données Index
Langage SQL : Langage de Définition de Données Modification de table
Suppression

Modification de table
Ajout, modification, suppression de colonnes ou de
contraintes :
ALTER TABLE Nom_table ADD ( Nom_attribut TYPE [NOT
NULL] [,. . . .]) ;
ALTER TABLE Nom_table ADD [CONSTRAINT
Nomcontrainte] typecontrainte ;
ALTER TABLE Nom_table DROP CONSTRAINT
Nomcontrainte [CASCADE] ;
ALTER TABLE Nom_table MODIFY (Nom_attribut TYPE
[NOT NULL] [, .]....) ;

Prof. Aannaque Introduction aux Bases de Données


Création de table
Introduction Contraintes d’Intégrité
Langage SQL : Langage de Manipulation de Données Index
Langage SQL : Langage de Définition de Données Modification de table
Suppression

Modification de table

Ajout, modification, suppression de colonnes ou de


contraintes :
ALTER TABLE Nom_table
DROP COLUMN Nom_col ;
ALTER TABLE Nom_table1
ADD FOREIGN KEY (Nom_attr1 [, Nom_attr2]..)
REFERENCES Nom_table2 ;
ALTER TABLE Nom_table1
ADD PRIMARY KEY (Nom_attr1 [, Nom_attr2]..) ;

Prof. Aannaque Introduction aux Bases de Données


Création de table
Introduction Contraintes d’Intégrité
Langage SQL : Langage de Manipulation de Données Index
Langage SQL : Langage de Définition de Données Modification de table
Suppression

Suppression

DROP TABLE Nom_table ;


DROP INDEX Nom_Index ;
DROP VIEW Nom_Vue ;

Prof. Aannaque Introduction aux Bases de Données

Vous aimerez peut-être aussi