Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente |
technique:mysql [2018/04/06 23:14] – francoisa | technique:mysql [2024/04/02 15:07] (Version actuelle) – modification externe 127.0.0.1 |
---|
====== Commandes MySQL ====== | **Cette page est obsolète. Veuillez accéder au contenu à son nouvel emplacement : [[https://bibliotech.cemea.org/books/outils-technique/page/mysql|mysql]]** |
| |
===== Connexion ===== | |
| |
mysql ‐u nomutilisateur ‐p SHOW DATABASES; USE nomdelabdd; SHOW TABLES; DESCRIBE nomdelatable; | |
| |
===== Actions sur bases ===== | |
| |
CREATE DATABASE nomdelabdd; DROP DATABASE nomdelabdd; | |
| |
CREATE TABLE nomdelatable ( nomcolonne1 INT, nomcolonne2 FLOAT, nomcolonne3 VARCHAR(50), nomcolonne3 CHAR(50) ); | |
| |
DROP TABLE nomdelatable; | |
| |
''DELETE FROM nom_de_la_table WHERE nom_de_l'entree="valeur de l'entre"'' | |
| |
Se connecter à MySQL en ligne de commande sous Linux : | |
| |
<code> | |
mysql ‐u nomutilisateur ‐p | |
</code> | |
| |
Afficher les bases de données MySQL existantes : | |
| |
<code> | |
SHOW DATABASES; | |
</code> | |
| |
Se placer et utiliser une base de données : | |
| |
<code> | |
USE nomdelabdd; | |
</code> | |
| |
Afficher les tables de cette base de données : | |
| |
<code> | |
SHOW TABLES; | |
</code> | |
| |
Afficher la structure (colonnes) d'une table de la base de données : | |
| |
<code> | |
DESCRIBE nomdelatable; | |
</code> | |
| |
=== Manipulation d'une base de données MySQL === | |
| |
Création de la base de données : | |
| |
<code> | |
CREATE DATABASE nomdelabdd; | |
</code> | |
| |
Supprimer la base de données : | |
| |
<code> | |
DROP DATABASE nomdelabdd; | |
</code> | |
| |
=== Manipulation des tables d'une base de données MySQL === | |
| |
Création d'une table : | |
| |
<code> | |
CREATE</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> nomdelatable</font> | |
<font inherit/inherit;;#000000 font-weight: bold;;inherit>(</font> | |
nomcolonne1</font> <font inherit/inherit;;#204a87;;inherit>INT</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>,</font> | |
nomcolonne2</font> <font inherit/inherit;;#204a87;;inherit>FLOAT</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>,</font> | |
nomcolonne3</font> <font inherit/inherit;;#204a87;;inherit>VARCHAR</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>(</font><font inherit/inherit;;#0000cf font-weight: bold;;inherit>50</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>),</font> | |
nomcolonne3</font> <font inherit/inherit;;#204a87;;inherit>CHAR</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>(</font><font inherit/inherit;;#0000cf font-weight: bold;;inherit>50</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>)</font> | |
<font inherit/inherit;;#000000 font-weight: bold;;inherit>);</font> | |
</code> | |
| |
Supprimer une table : | |
| |
<code> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>DROP</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> nomdelatable</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font> | |
</code> | |
| |
== Modification d'une table == | |
| |
Ajouter une colonne à une table : | |
| |
<code> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> nomdelatable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>ADD</font> nomdecolonne</font> <font inherit/inherit;;#204a87;;inherit>INT</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> nomdelatable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>ADD</font> nomdecolonne1</font> <font inherit/inherit;;#204a87;;inherit>VARCHAR</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>(</font><font inherit/inherit;;#0000cf font-weight: bold;;inherit>50</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>);</font> | |
</code> | |
| |
<code> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> nomdelatable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>ADD</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>COLUMN</font> nomdelacolonne</font> <font inherit/inherit;;#204a87;;inherit>INT</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font> | |
</code> | |
| |
Supprimer une colonne d'une table : | |
| |
<code> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> nomdelatable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>DROP</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>COLUMN</font> nomdecolonne1</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font> | |
</code> | |
| |
Renommer / changer le nom d'une colonne de la table : | |
| |
<code> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> nomdelatable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>RENAME</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>AS</font> lenouveaunomdelatable</font> | |
</code> | |
| |
<code> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> nomancientable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>RENAME</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TO</font> nomnouveautable</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font> | |
</code> | |
| |
<code> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>RENAME</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> nomancientable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TO</font> nomnouveautable</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font> | |
</code> | |
| |
Renommer / changer le type d'une colonne : | |
| |
<code> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> nomdelatable</font> CHANGE</font> nomdelacolonne</font> nouveaunomdelacolonne</font> <font inherit/inherit;;#204a87;;inherit>INT</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font> | |
</code> | |
| |
Ajouter une contrainte de clé primaire sur une colonne d'une table : | |
| |
<code> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> nomdelatable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>ADD</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>PRIMARY</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>KEY</font> <font inherit/inherit;;#000000 font-weight: bold;;inherit>(</font>nomcolonne</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>);</font> | |
</code> | |
| |
<code> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> nomdelatable</font> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ADD</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>CONSTRAINT</font> pk_nomdelacontrainte</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>PRIMARY</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>KEY</font> <font inherit/inherit;;#000000 font-weight: bold;;inherit>(</font>nomcolonne</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>);</font> | |
</code> | |
| |
Supprimer une contrainte de clé primaire sur une colonne : | |
| |
<code> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> nomdelatable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>DROP</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>PRIMARY</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>KEY</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font> | |
</code> | |
| |
Ajouter une contrainte de clé étrangère sur une colonne d'une table : | |
| |
<code> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> nomdelatable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>ADD</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>CONSTRAINT</font> fk_nomdelacontrainte</font> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>FOREIGN</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>KEY</font> <font inherit/inherit;;#000000 font-weight: bold;;inherit>(</font>nomcolonne</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>)</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>REFERENCES</font> tabledereference</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>(</font>nomcolonne</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>);</font> | |
</code> | |
| |
Supprimer une contrainte de clé étrangère sur une colonne d'une table : | |
| |
<code> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ALTER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>TABLE</font> nomdelatable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>DROP</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>CONSTRAINT</font> fk_nomdelacontrainte</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font> | |
</code> | |
| |
=== Requêtes SQL de base === | |
| |
Afficher / sélectionner des enregistrements d'une table SQL : | |
| |
<code> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>SELECT</font> nomcolonne</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>FROM</font> nomtable</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font> | |
</code> | |
| |
Afficher / sélectionner des enregistrements d'une table SQL de façon distincte : | |
| |
<code> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>SELECT</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>DISTINCT</font> nomcolonne</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>FROM</font> nomtable</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font> | |
</code> | |
| |
Afficher / sélectionner des enregistrements d'une table SQL avec une condition : | |
| |
<code> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>SELECT</font> nomcolonne</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>FROM</font> nomtable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>WHERE</font> nomcolonne</font> <font inherit/inherit;;#ce5c00 font-weight: bold;;inherit>=</font> <font inherit/inherit;;#4e9a06;;inherit>'1'</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>SELECT</font> nomcolonne</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>FROM</font> nomtable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>WHERE</font> nomcolonne</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>LIKE</font> <font inherit/inherit;;#4e9a06;;inherit>'%.com'</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>;</font> | |
</code> | |
| |
Afficher / sélectionner des enregistrements de plusieurs tables via une jointure internet : | |
| |
<code> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>SELECT</font> colonne1</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>,</font> colonne2</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>,</font> colonne3</font> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>FROM</font> table1</font> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>INNER</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>JOIN</font> table2</font> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>ON</font> table1</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>.</font>colonne1</font><font inherit/inherit;;#ce5c00 font-weight: bold;;inherit>=</font>table2</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>.</font>colonne3</font> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>WHERE</font> colonne1</font> <font inherit/inherit;;#ce5c00 font-weight: bold;;inherit>=</font> <font inherit/inherit;;#4e9a06;;inherit>'cequejecherche'</font> | |
</code> | |
| |
Mise à jour de l'ensemble des enregistrements sur une colonne d'une table : | |
| |
<code> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>UPDATE</font> nomtable</font> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>SET</font> nomcolonne1</font><font inherit/inherit;;#ce5c00 font-weight: bold;;inherit>=</font><font inherit/inherit;;#4e9a06;;inherit>'mavaleur'</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>,</font>nomcolonne2</font><font inherit/inherit;;#ce5c00 font-weight: bold;;inherit>=</font><font inherit/inherit;;#4e9a06;;inherit>'mavaleur2'</font> | |
</code> | |
| |
Mise à jour de l'ensemble des enregistrements sur une colonne d'une table avec une condition : | |
| |
<code> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>UPDATE</font> nomtable</font> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>SET</font> nomcolonne1</font><font inherit/inherit;;#ce5c00 font-weight: bold;;inherit>=</font><font inherit/inherit;;#4e9a06;;inherit>'mavaleur'</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>,</font>nomcolonne2</font><font inherit/inherit;;#ce5c00 font-weight: bold;;inherit>=</font><font inherit/inherit;;#4e9a06;;inherit>'mavaleur2'</font> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>WHERE</font> nomcolonne3</font> <font inherit/inherit;;#ce5c00 font-weight: bold;;inherit>=</font><font inherit/inherit;;#4e9a06;;inherit>'0'</font> | |
</code> | |
| |
Insérer un enregistrement dans une table : | |
| |
<code> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>INSERT</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>INTO</font> nomtable</font> <font inherit/inherit;;#000000 font-weight: bold;;inherit>(</font>colonne1</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>,</font>colonne2</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>,</font>colonne3</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>)</font> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>VALUES</font> <font inherit/inherit;;#000000 font-weight: bold;;inherit>(</font>valeu1</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>,</font>valeur2</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>,</font>valeur3</font><font inherit/inherit;;#000000 font-weight: bold;;inherit>);</font> | |
</code> | |
| |
Supprimer un enregistrement dans une table : | |
| |
<code> | |
<font inherit/inherit;;#204a87 font-weight: bold;;inherit>DELETE</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>FROM</font> nomtable</font> <font inherit/inherit;;#204a87 font-weight: bold;;inherit>WHERE</font> nomcolonne</font><font inherit/inherit;;#ce5c00 font-weight: bold;;inherit>=</font><font inherit/inherit;;#4e9a06;;inherit>'lavaleur'</font> | |
</code> | |