Download - Chapitre 1 Introduction - iro.umontreal.ca
![Page 1: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/1.jpg)
Base de données
Chapitre 1Introduction
![Page 2: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/2.jpg)
2
Plan du cours
n Introductionn Modèle relationnel
n SQLn Conception
![Page 3: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/3.jpg)
3
ExempleMAT NOM FONCTION COURS AN_ENT
62945 Gilles Prof_adj MRT1111 1997
34560 Myriam Prof_agr MRT2221 1993
21539 Claudine Prof_adj MRT3331 1999
80200 Bernard Prof_tit MRT1112 1982
75902 Yida Prof_agr MRT1664 1990
n Petite base de données avec 1 seule table (appelons la professeur)– Conserver les données
– Supporter des opérations sur les données
![Page 4: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/4.jpg)
4
Exemple
n SélectionSELECT NOM, FONCTION
FROM PROFESSEUR
WHERE AN_ENT > 1992
MAT NOM FONCTION COURS AN_ENT
62945 Gilles Prof_adj MRT1111 1997
34560 Myriam Prof_agr MRT2221 1993
21539 Claudine Prof_adj MRT3331 1999
80200 Bernard Prof_tit MRT1112 1982
75902 Yida Prof_agr MRT1664 1990
NOM FONCTION
Gilles Prof_adj
Myriam Prof_agr
Claudine Prof_adj
![Page 5: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/5.jpg)
5
Exemple
n InsertionINSERT INTOPROFESSEUR (MAT, NOM, FONCTION, COURS, AN_ENT)VALUES (66231, ‘Jian’, ‘Prof_adj’, ‘MRT2323’, 1996)
MAT NOM FONCTION COURS AN_ENT
62945 Gilles Prof_adj MRT1111 1997
34560 Myriam Prof_agr MRT2221 1993
21539 Claudine Prof_adj MRT3331 1999
80200 Bernard Prof_tit MRT1112 1982
75902 Yida Prof_agr MRT1664 1990
![Page 6: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/6.jpg)
6
Exemple
n InsertionINSERT INTOPROFESSEUR (MAT, NOM, FONCTION, COURS, AN_ENT)VALUES (66231, ‘Jian’, ‘Prof_adj’, ‘MRT2323’, 1996)
MAT NOM FONCTION COURS AN_ENT
62945 Gilles Prof_adj MRT1111 1997
34560 Myriam Prof_agr MRT2221 1993
21539 Claudine Prof_adj MRT3331 1999
80200 Bernard Prof_tit MRT1112 1982
75902 Yida Prof_agr MRT1664 1990
66231 Jian Prof_adj MRT2323 1996
![Page 7: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/7.jpg)
7
Exemple
n Mise à jourUPDATE PROFESSEURSET COURS = ‘MRT2325’WHERE MAT = 66231
MAT NOM FONCTION COURS AN_ENT
62945 Gilles Prof_adj MRT1111 1997
34560 Myriam Prof_agr MRT2221 1993
21539 Claudine Prof_adj MRT3331 1999
80200 Bernard Prof_tit MRT1112 1982
75902 Yida Prof_agr MRT1664 1990
66231 Jian Prof_adj MRT2323 1996
![Page 8: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/8.jpg)
8
Exemple
n Mise à jourUPDATE PROFESSEURSET COURS = ‘MRT2325’WHERE MAT = 66231
MAT NOM FONCTION COURS AN_ENT
62945 Gilles Prof_adj MRT1111 1997
34560 Myriam Prof_agr MRT2221 1993
21539 Claudine Prof_adj MRT3331 1999
80200 Bernard Prof_tit MRT1112 1982
75902 Yida Prof_agr MRT1664 1990
66231 Jian Prof_adj MRT2325 1996
![Page 9: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/9.jpg)
9
Exemple
n DestructionDELETE FROM PROFESSEURWHERE MAT = 66231
MAT NOM FONCTION COURS AN_ENT
62945 Gilles Prof_adj MRT1111 1997
34560 Myriam Prof_agr MRT2221 1993
21539 Claudine Prof_adj MRT3331 1999
80200 Bernard Prof_tit MRT1112 1982
75902 Yida Prof_agr MRT1664 1990
66231 Jian Prof_adj MRT2325 1996
![Page 10: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/10.jpg)
10
Exemple
n DestructionDELETE FROM PROFESSEURWHERE MAT = 66231
MAT NOM FONCTION COURS AN_ENT
62945 Gilles Prof_adj MRT1111 1997
34560 Myriam Prof_agr MRT2221 1993
21539 Claudine Prof_adj MRT3331 1999
80200 Bernard Prof_tit MRT1112 1982
75902 Yida Prof_agr MRT1664 1990
![Page 11: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/11.jpg)
11
Système de gestion de bases de données
n Un système informatique de conservation de l’information nécessite :– Données– Matériel– Logiciel– Utilisateurs
Base de données
Système de gestion de base de données
App
licat
ions
Uti
lisat
eurs
![Page 12: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/12.jpg)
12
Base de données
n Définitions– Données persistantes
• Durée de vie dépasse celle de l’exécution d’un programme
– Base de données• Collection de données persistantes utilisées par des
systèmes informatiques
n Exemples– Organisations
• banques, hôpitaux, université, compagnies, etc.
– Systèmes• comptabilité, gestion du personnel, gestion de stock, etc.
![Page 13: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/13.jpg)
13
Base de données
n Information– Entités
– Relations
– Propriétés• Employé
– Mat– Nom
– ...
supervise EmployéProjet
Département Client
participe
affectéfinance
![Page 14: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/14.jpg)
14
Base de données
n Acteurs – Concepteur
– Administrateur de données
– Administrateur de la base de données– Utilisateur final
n Une même personne peut occuper plusieurs rôles
n Un même rôle peut être occupé par plusieurs personnes
![Page 15: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/15.jpg)
15
Base de données
n Avantages– Réduit les redondances
• Un fichier du personnel pour la paye/comptabilité• Un fichier du personnel pour les ressources humaines
– Évite l’incohérence• Ajout d’un nouvel employé
– Permet le partage des données• Entre applications existantes et futures
– Permet l’application des normes• ODMG, CORBA, etc.
![Page 16: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/16.jpg)
16
Base de données
n Avantages– Garantie la sécurité
• Utilisateurs (politique)• Opérations (consultation, destruction, insertion)
– Assure l’intégrité• Données exactes
– Permet d’établir des priorités• Optimisation des accès pour certaines applications au
détriment des autres
– Assure l’indépendance des données• Applications, représentation interne, etc.
![Page 17: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/17.jpg)
17
Historiquen Génération 0 (années 60)
– fichiers reliés par des pointeurs• IDS.I (Honeywell) et IMS.I (IBM)• pour les programmes de la conquête spatiale (APOLLO)
n 1ère génération (fin des années 60)– modèles hiérarchique et réseau (travaux de CODASYL)
• TOTAL, IDMS, IDS.2 et IMS.2, etc.
n 2ème génération (depuis 1970)– modèle relationnel
• SGBD commercialisés à partir de 1980• ORACLE, SYSBASE, DB2, etc.
n 3ème génération (début des années 80)– extension du relationnel (Oracle 10i, DB2 Universal Database,
etc.)– à objets (ObjectStore, GemStone, O2, etc.)
n 4ème génération … Internet, les informations non structurées, le multimédia, la découverte des données, XML
![Page 18: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/18.jpg)
Base de données
Chapitre 2Le modèle relationnel
![Page 19: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/19.jpg)
19
Types de modèles
n Modèles conceptuels– Entités-Relations
– UML
– …
n Modèles de bases de données– Réseau
– Hiérarchique
– Relationnel
– À objets
– …
![Page 20: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/20.jpg)
20
Plan du cours
n Introductionn Modèle relationneln SQLn Conception
![Page 21: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/21.jpg)
21
Concepts de base
n Attribut– Colonne d ’une relation caractérisée par un nom
n Relation– Degré
• nombre d ’attributs
– Cardinalité• nombre de n-uplets
![Page 22: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/22.jpg)
22
Concepts de base
n Exemple
MAT NOM GRADE COURS AN_ENT
62945 Gilles Prof_adj MRT1111 1997
34560 Myriam Prof_agr MRT2221 1993
21539 Claudine Prof_adj MRT3331 1999
80200 Bernard Prof_tit MRT1112 1982
75902 Yida Prof_agr MRT1664 1990
Prof_adjProf_agrProf_tit
DomaineAttribut
n-uple
Cardinalité
Degré
![Page 23: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/23.jpg)
23
Concepts de base
n Relation vs. Table
– Relation : définition d ’un objet abstrait (voir plus haut)
– Table : représentation concrète de cet objet abstrait
– Souvent confondues
![Page 24: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/24.jpg)
24
Intégrité des données
n Introduction– A tout instant la base représente une
partie du monde réel à travers une configuration particulière des valeurs des données
– Certaines configurations des valeurs n ’ont pas de sens
• exemple age d ’une personne = -35
– Intégration de règles d ’intégrité pour informer le SGBD de certaines contraintes du monde réel
![Page 25: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/25.jpg)
25
Intégrité des données
n Exemple de contraintes d ’intégrité• Matricule doit être sous la forme nnnnn
• La valeur de GRADE doit appartenir à l ’ensemble {Prof_adj, Prof_agr, Prof_tit}
• L ’année de recrutement doit être supérieure à 1920 (ou pourquoi pas 1878)
• L ’année de recrutement doit être inférieure ou égale à l ’année courante
• Tous les professeurs recrutés avant 1994 doivent être au moins agrégés
n Contraintes spécifiques à une base de données particulière
???
![Page 26: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/26.jpg)
26
Intégrité des données
n Propriétés d ’intégrité générale– clés candidates et clés primaires– clés étrangères
n Clés candidates– Ensemble minimal d ’attributs dont la
connaissance des valeurs permet d ’identifier un n-uplet unique de la relation considérée
– Chaque relation contient au moins une clé candidate
![Page 27: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/27.jpg)
27
Intégrité des données
n Clés candidates– Utilité
• mécanisme d ’adressage au niveau des n-uplets d ’une table
• Exemple
Professeur WHERE MAT = 34560, produit un seul n-uplet en résultat
Professeur WHERE AN_ENT = 1994, va produire un nombre imprévisible de n-uplets en résultat
![Page 28: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/28.jpg)
28
Intégrité des données
n Clé primaire– Une clé choisie parmi l ’ensemble des
clés candidates
– Le choix est arbitraire
– Peut se faire selon des heuristiques• Plus simple par exemple
– Exemple• Clés candidates MAT et NOM - COURS
• Clé primaire MAT
![Page 29: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/29.jpg)
29
Intégrité des données
n Clé primaire– Exemple
Etudiant (Code_per, Nom, Prenom, Adresse, Date_nais, Lieu_nais, NAS)
Quelles sont les clés candidates ?
Quelle clé primaire choisir ?
![Page 30: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/30.jpg)
30
Intégrité des données
n Clés étrangères– Soit R2 une relation, un sous-ensemble
d ’attributs FK dans R2 est dit clé étrangère dans le cas où
* il existe une relation R1 avec une clé candidate CK (R1 peut être R2)
* à tout instant, chaque valeur de FK dans R2 est identique à une valeur de CK dans R1
![Page 31: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/31.jpg)
31
Intégrité des données
n Clés étrangères– Propriétés
• Il n ’est pas nécessaire d ’avoir pour toute valeur de CK dans R1 une valeur identique de FK dans R2
• Une clé étrangère est composée ou simple
• Chaque attribut d ’une clé étrangère doit être défini sur le même domaine que son équivalent dans la clé candidate correspondante
![Page 32: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/32.jpg)
32
Intégrité des données
n Clés étrangères– Propriétés
• Un attribut d ’une clé étrangère peut faire partie d ’une clé candidate de sa relation
Employe (Mat, Nom, …)
Projet (Num, Designation, …)
Role (Mat, Num, role)
![Page 33: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/33.jpg)
33
Intégrité des données
n Clés étrangères– Diagramme référentiel
• représentation graphique des références
Employé
Rôle
Projet
Dépt
![Page 34: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/34.jpg)
34
Intégrité des données
n Exemple Employe (mat# : entier, nom : caractères, adresse : caractères, dept : entier, sup : entier)
Projet (num# : entier, designation : caractères)
Departement (dept# : entier, dir : entier, nom : caractères)
Role (mat# : entier, num# : entier, role_emp : caractères)
![Page 35: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/35.jpg)
35
Intégrité des données
n Clés étrangères– Intégrité référentielle
• La base de données ne doit pas contenir une valeur de clé étrangère non « unifiable »
• Cette contrainte est dite référentielle
• Conséquence : Il faut créer et détruire les n-uplets dans un ordre particulier
• Dans certains SGBDs (version 92 de SQL), on peut différer la vérification d ’une contrainte d ’intégrité référentielle (restreinte vs. cascade)
![Page 36: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/36.jpg)
36
Aperçu de déclaration de table en SQL
n Domaine de base– Entier : INTEGER – Décimal : DECIMAL(m,n)– Réel flottant : FLOAT– Chaîne de caractères : CHAR(n)– Date : DATE– ….
n Création d ’une table (forme simple)CREATE TABLE <nom-table> ( <attribut> <type>,
<attribut> <type>, <attribut> <type>, …)
![Page 37: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/37.jpg)
37
Aperçu de définition de donnéesen SQL
n Création d ’une table (forme simple)– ExempleCREATE TABLE VIN (
NV INTEGER,
CRU CHAR(10),MIL INTEGER)
n Contraintes d ’intégrité• NOT NULL
• UNIQUE ou PRIMARY KEY• FOREIGN KEY• REFERENCES
• CHECK
![Page 38: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/38.jpg)
38
Aperçu de définition de donnéesen SQLn Avec contraintes d ’intégritéExemple (SQL 86)CREATE TABLE COMMANDE (
NC INTEGER UNIQUE NOT NULL, NV INTEGER NOT NULLQUANTITE DECIMAL(6))
Exemple (SQL 89)CREATE TABLE COMMANDE (
NC INTEGER PRIMARY KEY, NV INTEGER NOT NULL REFERENCES VIN,QUANTITE DECIMAL(6) CHECK(QUANTITE > 0))
---------------------------------------------------------------------------------------------------------------
NC INTEGER, PRIMARY KEY (NC), NV INTEGER NOT NULL,FOREIGN KEY (NV) REFERENCES VIN,
SQL 92
![Page 39: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/39.jpg)
39
Aperçu de définition de donnéesen SQL
n Modification du schéma d ’une tableALTER TABLE <table> ADD COLUMN<attribut> <type>, <attribut> <type>, …
– ExempleALTER TABLE COMMANDE
ADD COLUMN DATE_COM DATE
n Suppression d ’une tableDROP TABLE <table>
– ExempleDROP TABLE COMMANDE
![Page 40: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/40.jpg)
40
Aperçu de définition de donnéesen SQL
n Exemple (SQL)Employe (mat# : entier, nom : caractères, adresse : caractères, dept : entier, sup : entier)
Projet (num# : entier, designation : caractères)
Departement (dept# : entier, dir : entier, nom : caractères)
Role (mat# : entier, num# : entier, role_emp : caractères)
![Page 41: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/41.jpg)
41
Aperçu de définition de donnéesen SQL, exemple
Employe (mat# : entier, nom : caractères, adresse : caractères, dept : entier, sup : entier)
CREATE TABLE EMPLOYE (
MAT INTEGER,NOM CHAR(12),
ADRESSE CHAR(25),
DEPT INTEGER NOT NULL,SUP INTEGER NOT NULL,
PRIMARY KEY (MAT),
FOREIGN KEY (DEPT) REFERENCES DEPARTEMENT, FOREIGN KEY (SUP ) REFERENCES EMPLOYE (MAT));
![Page 42: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/42.jpg)
42
Aperçu de définition de donnéesen SQL, exemple
Projet (num# : entier, designation : caractères)
CREATE TABLE PROJET (NUM INTEGER,
DESIGNATION CHAR(20),
PRIMARY KEY (NUM));
![Page 43: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/43.jpg)
43
Aperçu de définition de donnéesen SQL, exemple
Departement (dept# : entier, dir : entier, nom : caractères)
CREATE TABLE DEPARTEMENT (
DEPT INTEGER,DIR INTEGER NOT NULL,
DESIGNATION CHAR(20),
PRIMARY KEY (DEPT),FOREIGN KEY (DIR) REFERENCES EMPLOYE (MAT));
![Page 44: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/44.jpg)
44
Aperçu de définition de donnéesen SQL, exemple
Role (mat# : entier, num# : entier, role_emp : caractères)
CREATE TABLE ROLE (
MAT INTEGER,NUM INTEGER,
ROLE_EMP CHAR(15),
PRIMARY KEY (MAT, NUM), FOREIGN KEY (MAT) REFERENCES EMPLOYE ,
FOREIGN KEY (NUM) REFERENCES PROJET));
![Page 45: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/45.jpg)
Base de données
Chapitre 3SQL
![Page 46: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/46.jpg)
46
Plan du cours
n Introduction
n Modèle relationnel
n SQL
n Conception
![Page 47: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/47.jpg)
47
Introduction
n Permet de retrouver et de manipuler les données sans préciser les détails
n Dérivé de SEQUEL 2 (76), lui-même dérivé de SQUARE (75)
n Proposé par IBM (82 puis 87)
n Première version normalisée SQL1 (ISO89)
n Deuxième version normalisée SQL2 (ISO92)
n SQL3 en cours de normalisation
n Sert de couche basse aux L4G (par exemple Access)
![Page 48: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/48.jpg)
48
Définition des données
n Domaine– Définition de domaines peu utilisée
– Définition à partir des types de base
ANSI SQL Oracle
----------------- ---------------
CHARACTER(n), CHAR(n) CHAR(n)
CHARACTER VARYING(n), CHAR VARYING(n) VARCHAR(n)
NUMERIC(p,s), DECIMAL(p,s), DEC(p,s)[1] NUMBER(p,s)
INTEGER, INT, SMALLINT NUMBER(38) FLOAT(b)[2], DOUBLE PRECISION[3], REAL[4] NUMBER
![Page 49: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/49.jpg)
49
Définition des données
n Domaine– ExempleCREATE DOMAIN COULEUR CHAR(6) DEFAULT ‘vert’
CONSTRAINT VALIDE_COULEURCHECK (VALUE IN ‘rouge’, ‘blanc’, ‘vert’, ‘bleu’, ‘noir’)
– Modification de la définition (ALTER DOMAIN )
– Destruction d’un domaineDROP DOMAIN domaine [RESTRICT | CASCADE]
![Page 50: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/50.jpg)
50
Définition des données
n Table– Création d ’une table (forme simple)
• ExempleCREATE TABLE VIN (
NV NUMBER, CRU CHAR(10),
MIL NUMBER)
– Contraintes d ’intégrité– NOT NULL– UNIQUE ou PRIMARY KEY
– FOREIGN KEY
– REFERENCES– CHECK
![Page 51: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/51.jpg)
51
Définition des donnéesn Table
– Avec contraintes d ’intégritéExemple (SQL 86)CREATE TABLE COMMANDE (
NC NUMBER UNIQUE NOT NULL, NV NUMBER NOT NULLQUANTITE NUMBER(6))
Exemple (SQL 89)CREATE TABLE COMMANDE (
NC NUMBER PRIMARY KEY, NV NUMBER NOT NULL REFERENCES VIN,QUANTITE NUMBER(6) CHECK(QUANTITE > 0))
--------------------------------------------------------------------------------------------------
NC NUMBER, PRIMARY KEY (NC), NV NUMBER NOT NULL,FOREIGN KEY (NV) REFERENCES VIN,
SQL 92
![Page 52: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/52.jpg)
52
Définition des données
n Table– Modification du schéma d ’une table
ALTER TABLE <table> ADD COLUMN<attribut> <type>, <attribut> <type>, …• ExempleALTER TABLE COMMANDE
ADD COLUMN DATE_COM DATE
– Suppression d ’une tableDROP TABLE <table> • ExempleDROP TABLE COMMANDE
![Page 53: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/53.jpg)
53
Accès aux données
n Clause SELECT– FormeSELECT [ ALL | DISTINCT ] {<liste_attributs> | *}
n Clause FROM– FormeFROM <liste_tables>
n ExempleEmploye (mat#, nom, adresse, dept, sup)Projet (num#, designation, budget)
Departement (dept#, dir, appellation)Role (nume#, nump#, role_emp)
![Page 54: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/54.jpg)
54
Accès aux données
n Exemple– Donner les noms et adresses des employésSELECT nom, adresseFROM Employe
– Donner la liste des projetsSELECT *FROM Projet
– Donner les noms et les départements des employés
SELECT nom, appellationFROM Employe, Departement
![Page 55: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/55.jpg)
55
Accès aux donnéesn Clause WHERE
– FormeWHERE <condition>
n Exemple– Donner les noms et adresses des employés qui travaillent au
DIROSELECT nom, adresse
FROM Employe, Departement
WHERE appellation = ‘DIRO’
– Donner les noms des employés responsables de projetsSELECT nomFROM Employe E, Role R
WHERE E.mat = R.nume
AND role_emp = ‘responsable’
![Page 56: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/56.jpg)
56
Accès aux données
n Fonctions de calcul– COUNT, SUM, AVG, MAX, MIN
n Exemple– Donner le nombre d ’employés qui travaillent au DIROSELECT COUNT (*)FROM Employe, Departement
WHERE appellation = ‘DIRO’
– Donner le budget total des projets sous la responsabilité de l’employé numéro 35677
SELECT SUM (budget)
FROM Employe E, Role R, ProjetWHERE E.mat = R.nume
AND role_emp = ‘responsable’
AND mat = 35677
![Page 57: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/57.jpg)
57
Accès aux donnéesn Forme de conditions
– IN (NOT IN), Donner la liste des directeurs de départements
SELECT * FROM Employe
WHERE mat IN
(SELECT dir FROM Departement)
– ANY ou ALL, Donner la liste des employés responsables de projets
SELECT *
FROM Employe
WHERE mat = ANY (SELECT nume
FROM Role
WHERE role_emp = ‘responsable’)
![Page 58: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/58.jpg)
58
Accès aux donnéesn Forme de conditions
– IS (NOT) NULL, Donner la liste des employés sans superviseur
SELECT * FROM Employe
WHERE sup IS NULL
– EXISTS, Donner les numéros de projets dans lesquels intervient au moins un employé autre que le responsable
SELECT DISTINCT nump
FROM Role WHERE EXISTS
(SELECT nump
FROM RoleWHERE role_emp != ‘responsable’)
![Page 59: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/59.jpg)
59
Accès aux données
n Clause GROUP BY– FormeGROUP BY <liste_attributs>
n Exemple– Donner les noms des employés regroupés par projetSELECT nomFROM Employe E, Role RWHERE E.mat = R.nume GROUP BY R.nump
![Page 60: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/60.jpg)
60
Accès aux données
n Clause HAVING– FormeHAVING <condition>
– ExempleDonner les numéros de projets dans lesquels interviennent au moins
dix employés autres que le responsable
SELECT numpFROM Role
WHERE role_emp != ‘responsable’
GROUP BY numpHAVING COUNT(nume) > 9
![Page 61: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/61.jpg)
61
Accès aux données
n Unions– Forme<requete> UNION <requete>
– Exemple, liste des cadresSELECT *
FROM Employe WHERE mat IN ( SELECT dir
FROM Departement)
UNIONSELECT *
FROM Employe
WHERE mat = ANY ( SELECT nume FROM Role
WHERE role_emp = ‘responsable’)
![Page 62: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/62.jpg)
62
Manipulation des données
n Insertion– FormeINSERT INTO <table> [ (<liste_colonnes>)]
{VALUES (<liste_valeurs>) | <requete>}
– Exemple 1INSERT INTO Employe (mat, nom, adresse, dept, sup)VALUES (34098, ‘Gilles’, ‘3456, Gaspe, Mtl’, 456, 68735)
– Exemple 2INSERT INTO Directeur
SELECT * FROM Employe
WHERE mat IN ( SELECT dir
FROM Departement)
![Page 63: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/63.jpg)
63
Manipulation des donnéesn Mise à jour
– FormeUPDATE <table>
SET {<nom_colonne> = <expression>}+
WHERE {<condition> | CURRENT OF <nom_curseur>}
– Exemple 1UPDATE Employe
SET adresse = ‘3456, Gaspe, Mtl’
WHERE mat = 34098
– Exemple 2UPDATE Projet
SET budget = budget + 1000
WHERE num IN ( SELECT nump FROM Role
WHERE nume = 34098
AND role_emp = ‘ responsable ’)
![Page 64: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/64.jpg)
64
Manipulation des données
n Suppression– FormeDELETE FROM <table> [WHERE {<condition> | CURRENT OF <nom_curseur>}]
– Exemple 1DELETE FROM Employe WHERE mat = 34098
– Exemple 2DELETE FROM ProjetWHERE num IN ( SELECT nump
FROM RoleWHERE nume = 68754)
![Page 65: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/65.jpg)
65
SQL intégré (JDBC)
n Introduction– SQL peut être intégré dans un langage
hôte (C, COBOL, PL/1, PASCAL, JAVA, etc.)
– Conflit (opération ensembliste vs. opération séquentielle)
• traitement de plusieurs n-uplets
– Étude du cas du langage Java (JDBC)
![Page 66: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/66.jpg)
Base de données
Chapitre 4Conception de bases de données
![Page 67: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/67.jpg)
67
Plan du cours
n Introduction
n Modèle relationnel
n SQL
n Conception
![Page 68: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/68.jpg)
68
Introduction
n Problème de la conception– Associer une structure logique aux données. En
d'autres termes, trouver les relations et leurs attributs
n Techniques– Dépendances fonctionnelles
– Les formes normales
– Le modèle Entité-Association
![Page 69: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/69.jpg)
69
Modélisation sémantique
n Permet de modéliser les donnéesn En utilisant différents formalismes
– exemples : E/A, UML, …
n Une conception de base de données qui commence par la modélisation sémantique est dite conception descendante (conception allant du plus haut niveau vers le plus bas niveau)
![Page 70: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/70.jpg)
70
Modèle Entité/Association
n Généralités– Appelé également Entité/Relation
– Proposé par Chen en 1976
– Englobe à l'origine un nombre restreint de concepts (entité, association, propriété)
– Diverses extensions ont été proposées par la suite (contraintes, généralisation agrégation, …)
![Page 71: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/71.jpg)
71
Modèle Entité/Association,Concepts
n Entité– "Une chose qui peut être identifiée
distinctement"
– Par abus de langage, on confond entité et type d'entité
– Exemples : • Concrètes : voiture, employé
• Abstraites : projet, cours
– Mais, mariage ???
![Page 72: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/72.jpg)
72
Modèle Entité/Association,Concepts
n Entité (suite)– On distingue souvent
• Entité régulière
• Entité faible dont l'existence dépend d'une autre entité
• Exemple– Employé …. entité régulière– Personne_a_charge …. entité faible dont
l'existence dépend de celle de l'entité Employé
![Page 73: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/73.jpg)
73
Modèle Entité/Association,Concepts
n Entité (suite)– Représentation graphique
Nom de l'entité
Nom de l'entité
ou
Employé
Projet
![Page 74: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/74.jpg)
74
Modèle Entité/Association,Concepts
n Propriété– Les entités possèdent des propriétés qui
les décrivent
– Appelées également Attributs– Exemples :
• Pour Employé: matricule, nom, adresse
• mais pas le numéro de département
![Page 75: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/75.jpg)
75
Modèle Entité/Association,Concepts
n Propriété– Elle peut être
• Simple ou composée– âge (simple)
– adresse (composée)
• Un identificateur (ou clé)– code_permanent
• mono ou multi-valuée– nom (mono-valuée)
– prénom (multi-valuée)
![Page 76: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/76.jpg)
76
Modèle Entité/Association,Concepts
n Propriété– Elle peut être
• Manquante– valeur inconnue
– non applicable (date_mariage pour un célibataire)
• De base ou dérivée– nom (base)– ancienneté (dérivée de la date d'embauche et de
la date d'aujourd'hui)
– Quantité totale (dérivée de la somme des quantités)
![Page 77: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/77.jpg)
77
Modèle Entité/Association,Concepts
n Propriété– Représentation graphique
propriété
Parfois, on utilise cette notation pour les propriétés multi-valuées
propriété
ident
Parfois, on utilise cette notation pour les identificateurs
Employé
MatriculeNom
AdressePrénoms
CPVilleRue
![Page 78: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/78.jpg)
78
Modèle Entité/Association,Concepts
n Association– Est une relation entre entités
– Même abus de langage entre association et type d'associations
– Exemple • Participe_dans entre Employé et Projet
• Récolte entre Producteur et Vin
– Comme une entité, elle peut posséder des propriétés
![Page 79: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/79.jpg)
79
Modèle Entité/Association,Concepts
n Association– Elle est dite binaire si elle relie
uniquement deux entités, ternaire si elle en relie trois, etc.
– Chaque participant (entité) possède un rôle dans l'association
– Elle peut être réflexive• Exemple : est_mariée_à entre personne et
personne
![Page 80: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/80.jpg)
80
Modèle Entité/Association,Concepts
n Association– Cardinalités
• Indiquent le nombre minimal et maximal d'occurrences d'une association pour une occurrence d'un participant
• Par exemple pour l'association contrôle entreDépartement et Projet
– un Département contrôle 0 ou plusieurs projets
– un Projet est contrôlé par un et un seul département
• Les différentes possibilités sont– 0,1 ; 1,1 ; 0,N ; 1,N
![Page 81: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/81.jpg)
81
Modèle Entité/Association,Concepts
n Association– Représentation graphique
association
Parfois, on utilise cette notation pour une association impliquant une entité faible
association
Professeur
A_dirigé
Département
0,1
1,N
Période
Employé
Supervise
0,N 1,1
Pers_à_ch
Est_tuteur
1,1
0,N
![Page 82: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/82.jpg)
82
Modèle Entité/Association,Concepts
n Un exemple complet
Département
Contrôle
Projet
0,1
1,N
NAS
Employé
Supervise
0,N 1,1
Pers_à_ch
Est_tuteur
1,1
0,N
Nom
Adr
RueVille
DDN
Prén.
Lien
Num
Nom
Budget
N_emp localNom
Est_affecté
Dirige
Participe
Date
N_heures
1,1
1,N
0,1
1,1
1,N1,N
![Page 83: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/83.jpg)
83
Conception de la base de donnéesE/A vers relationnel
n Entité– Chaque type d'entité devient une relation.
Son identificateur devient la clé et les propriétés deviennent des attributs. Les propriétés multi-valuées sont transformées conformément à la 1NF
Employé
Matricule NomPrénoms
Employe(Matricule, Nom, Prenom)
![Page 84: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/84.jpg)
84
Conception de la base de donnéesE/A vers relationnel
n Association plusieurs à plusieurs {0,N | 1,N} vers {0,N | 1,N}
– Chaque type d'association devient une relation
– Les identificateurs des participants deviennent la clé de la relation
Inscription(NAS, Sigle, Session)NAS et Sigle sont des clés étrangères également
Etudiant
NAS
Cours
Sigle
Suit0,N 1,N
Session
![Page 85: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/85.jpg)
85
Conception de la base de donnéesE/A vers relationnel
n Association un à plusieurs {0,1 | 1,1} vers {0,N | 1,N}
– Chaque type d'association se traduit par un ajout d'une clé étrangère
Employé(NAS, …, Nom_D)
Employé
NAS
Dept
Nom_D
Est_aff1,1 1,N
Cas particulier d'une association impliquant une entité faible (contrainte CASCADE)
![Page 86: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/86.jpg)
86
Conception de la base de donnéesE/A vers relationnel
n Association un à un {0,1 | 1,1} vers {0,1 | 1,1}
– Chaque type d'association se traduit par • Une fusion des deux relations correspondantes
• Un ajout de clé étrangère dans l'une ou les deux relations
Option 1 :Employé(NAS, …, Num)
Employé
NAS
Bureau
Num
Occupe1,1 1,1
Option 2 :Employé(NAS, …, Num)Bureau(Num, …, NAS)
![Page 87: Chapitre 1 Introduction - iro.umontreal.ca](https://reader034.vdocuments.pub/reader034/viewer/2022042623/6264cadd9c7be61cee06f59e/html5/thumbnails/87.jpg)
87
Conception de la base de donnéesE/A vers relationnel
n Convertir le modèle suivant en Relationnel
Département
Contrôle
Projet
0,1
1,N
NAS
Employé
Supervise
0,N 1,1
Pers_à_ch
Est_tuteur
1,1
0,N
Nom
Adr
RueVille
DDN
Pren.
Lien
Num
Nom
budget
N_emp localNom
Est_affecté
Dirige
Participe
Date
N_heures
1,1
1,N
0,1
1,1
1,N1,N