Eric REMY & David BOYER - IUT d'Arles
Eric REMY & David BOYER - IUT d'Arles
Eric REMY & David BOYER - IUT d'Arles
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
Plan<br />
●<br />
Introduction aux bases de données<br />
● PostgreSQL ?<br />
●<br />
●<br />
●<br />
●<br />
Outils d'accès facile à une base<br />
PostgreSQL<br />
PostgreSQL et la programmation<br />
Usages avancés de PostgreSQL<br />
Questions<br />
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
Bases de données ?<br />
●<br />
Objectifs : stocker des données pour...<br />
– Assurer leur archivage ;<br />
– Les étudier ultérieurement<br />
●<br />
●<br />
●<br />
●<br />
contrôles<br />
statistiques<br />
fouille de données<br />
etc.<br />
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
Faire des feuilles de calcul ?<br />
●<br />
●<br />
●<br />
Avantage : facile et simple (ou plutôt<br />
simpliste !)<br />
Inconvénient : peu robuste, peu évolutif,...<br />
Exemple<br />
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
Unified Modeling Language<br />
●<br />
●<br />
Techniques pour organiser la conception<br />
Schémas standardisés pour exprimer<br />
l'organisation du système<br />
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
Conception de bases de données<br />
●<br />
Identifier les tables et leurs champs (colonnes)<br />
– Informations pertinentes<br />
– Pas de redondance, pas de contradictions<br />
●<br />
●<br />
Identifier les relations entre elles<br />
Identifier les contraintes<br />
– Clé primaire (unique et toujours renseignée)<br />
– Contraintes référentielles, clés étrangères<br />
– Autres contraintes<br />
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
SQLite<br />
A l'opposé de PostgreSQL car<br />
●<br />
●<br />
●<br />
●<br />
Idéal pour une base locale ou pour l'embarqué<br />
Très léger<br />
Simple et efficace<br />
Démonstration<br />
– Collection de CD-Rom de photos (Perl)<br />
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
PostgreSQL<br />
●<br />
●<br />
●<br />
●<br />
●<br />
●<br />
University of California at Berkeley<br />
Jeux de mots : après le système « Ingres »<br />
15 ans de développement actif<br />
Totalement Libre / OpenSource (licence BSD)<br />
Moins connu que MySQL (licence GPL)<br />
Plus avancé que ce dernier... plus de détails en fin<br />
de cet exposé.<br />
Respect de la norme ANSI SQL<br />
Le plus proche d'un « Oracle » libre...<br />
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
Qualités de PostgreSQL<br />
●<br />
●<br />
●<br />
●<br />
●<br />
●<br />
Aucun coût de licence<br />
Tous Un*x et Windows (depuis la version 8.x)<br />
Architecture client / serveur...<br />
... donc multi-utilisateur<br />
Stabilité et pérennité<br />
Excellentes documentations (dont en français)<br />
Fonctionnalités avancées : contraintes<br />
d'intégrité, views, triggers, transactions, etc.<br />
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
PGAdmin III<br />
●<br />
●<br />
Mêmes développeurs que PostgreSQL<br />
Outil en C++ installé sur le poste client<br />
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
phppgadmin<br />
●<br />
Outil en PHP ; accès par un navigateur Web<br />
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
OpenOffice Base<br />
●<br />
●<br />
S'améliore régulièrement<br />
Sérieux manque d'un générateur d'état valable<br />
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
Structured Query Language (SQL)<br />
●<br />
●<br />
●<br />
Un peu comme un langage de programmation<br />
Langage de définition de données<br />
Langage de manipulation de données<br />
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
psql<br />
●<br />
●<br />
Ligne de commande livrée avec PostgreSQL<br />
Le couteau suisse de l'accès à PostgreSQL<br />
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
Programmation + PostgreSQL<br />
●<br />
●<br />
●<br />
Possibilité d'interfacer pratiquement tous les<br />
langages de programmation avec un serveur<br />
PostgreSQL afin de retirer et traiter de<br />
l'information<br />
Exemples : JAVA, PHP, C, C++, Perl, Qt4,...<br />
Démonstration<br />
– Emploi du temps : édition (JAVA)<br />
– Emploi du temps : consultation (PHP)<br />
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
PostgreSQL avancé : Introduction<br />
●<br />
Langages de procédures : PL/pgSQL, mais<br />
aussi PL/Tcl, PL/Perl, PL/Python<br />
● Recherche « plein texte »<br />
●<br />
●<br />
●<br />
SIG avec PostGIS<br />
Types de données ENUM et XML, fonctions<br />
XML (table_to_xml)<br />
Tablespaces, Héritage entre tables<br />
(partitionnement...)<br />
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
PostgreSQL avancé : Déclencheurs (1)<br />
●<br />
●<br />
●<br />
« Démon » qui se réveille lors d'une<br />
modification d'une table, avant ou après, au<br />
niveau ligne ou instruction<br />
Provoque l'exécution d'une fonction stockée<br />
Utiles pour imposer des contraintes<br />
complexes, propager des MAJ...<br />
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
PostgreSQL avancé : Déclencheurs (2)<br />
● On modifie un peu le MCD d'exemple :<br />
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
PostgreSQL avancé : Déclencheurs (3)<br />
● On crée d'abord la procédure à exécuter :<br />
CREATE OR REPLACE FUNCTION avant_achat() RETURNS trigger AS $avant_achat$<br />
DECLARE<br />
solde_actuel client.solde%TYPE;<br />
nom_client client.nom%TYPE;<br />
prix_un article.prix%TYPE;<br />
prix_total article.prix%TYPE;<br />
BEGIN<br />
select into solde_actuel solde from client where id = NEW.id_client;<br />
select into nom_client nom from client where id = NEW.id_client;<br />
select into prix_un prix from article where ref_art = NEW.ref_article;<br />
prix_total := prix_un * NEW.quantite;<br />
IF (solde_actuel - prix_total) < 0 THEN<br />
RAISE EXCEPTION 'Le client % a dépassé son solde achat', nom_client;<br />
END IF;<br />
RETURN NEW;<br />
END;<br />
$avant_achat$ LANGUAGE plpgsql;<br />
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
PostgreSQL avancé : Déclencheurs (4)<br />
● Puis on crée le déclencheur « avant » :<br />
CREATE TRIGGER avant_achat BEFORE INSERT ON achat<br />
FOR EACH ROW EXECUTE PROCEDURE avant_achat();<br />
●<br />
●<br />
On a bien interdit l'achat si le solde est<br />
dépassé...<br />
TP : écrire le déclencheur « après » pour la<br />
MAJ du solde !<br />
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
PostgreSQL avancé : Système de règles (1)<br />
●<br />
●<br />
●<br />
Réécriture des requêtes parvenant au SGBD<br />
En lecture, utilisé par PostgreSQL pour<br />
implémenter les vues<br />
En écriture, puissante alternative aux<br />
déclencheurs<br />
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>
PostgreSQL avancé : Système de règles (2)<br />
●<br />
On crée une règle « on update » sur la table<br />
article :<br />
CREATE OR REPLACE RULE log_article AS ON UPDATE TO article<br />
WHERE NEW.prix OLD.prix DO INSERT INTO surveille_article<br />
VALUES (NEW.ref_art, current_timestamp, current_user);<br />
●<br />
Dès que l'on modifie le prix, une ligne s'ajoute<br />
dans surveille_article<br />
<strong>Eric</strong> <strong>REMY</strong> & <strong>David</strong> <strong>BOYER</strong>