Exercice final PHP-MySQL Base de données Animaux
Transcription
Exercice final PHP-MySQL Base de données Animaux
Exercice final PHP-MySQL Base de données Animaux Olivier BOEBION 26/02/2004 1 Création de la base dans MySQL mysql -u admin -p use mysql CREATE DATABASE 2 BDBoebion; Création des utilisateurs 1 pour la gestion, 1 pour l’interrogation simple: INSERT INTO USER(Host,User,Password) VALUES (’localhost’,’bobdba’,PASSWORD(’**********’)); INSERT INTO USER(Host,User,Password) VALUES (’localhost’,’bob’,PASSWORD(’*********’)); INSERT INTO USER(Host,User,Password) VALUES (’neptune.obs-vlfr.fr’,’bobdba’,PASSWORD(’*********’)); INSERT INTO USER(Host,User,Password) VALUES (’neptune.obs-vlfr.fr’,’bob’,PASSWORD(’********’)); 1 3 Création des privilèges des utilisateurs GRANT SELECT,FILE ON BDBoebion.* TO bob@"localhost"; GRANT SELECT,FILE ON BDBoebion.* TO bob@"marine.obs-vlfr.fr"; GRANT ALL ON BDBoebion.* TO bobdba@"localhost"; GRANT ALL ON BDBoebion.* TO bobdba@"marine.obs-vlfr.fr"; 4 Connexion MySQL avec l’utilisateur ”bobdba” mysql -u bobdba -p use mysql <- impossible !!! use BDBoebion 5 Création des tables ”proprietaire” ”generique” et ”animal” CREATE TABLE proprietaire (id MEDIUMINT NOT NULL AUTO_INCREMENT, titre TINYINT, nom char(30) NOT NULL, prenom char(30), PRIMARY KEY(id)); CREATE TABLE generique (id MEDIUMINT NOT NULL AUTO_INCREMENT, nom char (30), PRIMARY KEY(id)); CREATE TABLE animal (id MEDIUMINT NOT NULL AUTO_INCREMENT, nom char(20), photo char(255) DEFAULT ’aucune.gif’ , id_generique MEDIUMINT , id_proprietaire MEDIUMINT, PRIMARY KEY(id)); 6 Ajout de propriétaires, especes et animaux 4 propriétaires: • Mlle Delphine BROWN • Mme Mireille DARK • M. Lipton YELLOW • M. Submarine YELLOW 2 INSERT INTO proprietaire(titre,nom,prenom) VALUES (1,’BROWN’,’Delphine’); INSERT INTO proprietaire(titre,nom,prenom) VALUES (0,’DARK’,’Mireille’); INSERT INTO proprietaire(titre,nom,prenom) VALUES (2,’YELLOW’,’Lipton’); INSERT INTO proprietaire(titre,nom,prenom) VALUES (2,’YELLOW’,’Submarine’); 3 espèces génériques: • cheval • chien • chat INSERT INTO generique (nom) VALUES (’cheval’,’chien’,’chat’); 5 animaux: • Touffue le chat appartenant à Delphine BROWN • Fend la Bise le cheval appartenant à Mireille DARK • Toupie le chat appartenant à Lipton YELLOW • Waf le chien appartenant à Submarine YELLOW • Fidele le chien appartenant à Delphine BROWN INSERT INTO animal (nom,photo,id_generique,id_proprietaire) VALUES (’Touffue’,’touffue.gif’,3,1); INSERT INTO animal (nom,photo,id_generique,id_proprietaire) VALUES (’Fend la bise’,’flb.gif’,1,2); INSERT INTO animal (nom,photo,id_generique,id_proprietaire) VALUES (’Toupie’,’toupie.gif’,3,3); INSERT INTO animal (nom,photo,id_generique,id_proprietaire) VALUES (’Waf’,’waf.gif’,2,4); INSERT INTO animal (nom,photo,id_generique,id_proprietaire) VALUES (’Fidele’,’fidele.gif’,2,1); 3 7 Interrogation de la base Quel est le nom générique de chaque animal ?: SELECT animal.nom,generique.nom FROM animal,generique WHERE id_generique=generique.id; +--------------+--------+ | nom | nom | +--------------+--------+ | Fend la bise | cheval | | Waf | chien | | Fidele | chien | | Touffue | chat | | Toupie | chat | +--------------+--------+ 5 rows in set (0.00 sec) Quel le propriétaire de chaque animal ?: SELECT animal.nom,proprietaire.titre,proprietaire.nom,proprietaire.prenom FROM animal,proprietaire WHERE id_proprietaire=proprietaire.id; +--------------+-------+--------+-----------+ | nom | titre | nom | prenom | +--------------+-------+--------+-----------+ | Touffue | 1 | BROWN | Delphine | | Fidele | 1 | BROWN | Delphine | | Fend la bise | 0 | DARK | Mireille | | Waf | 2 | YELLOW | Lipton | | Toupie | 2 | YELLOW | Submarine | +--------------+-------+--------+-----------+ 5 rows in set (0.00 sec) 4