MySQL

1.  Installation

Dans Mandriva 2006, commençons par installer MySQL :

$ su
Password:
# urpmi mysql
Un des paquetages suivants est nécessaire :
 1- MySQL-4.1.12-3.2.20060mdk.i586 : MySQL: a very fast and reliable SQL database engine (to install)
 2- MySQL-Max-4.1.12-3.2.20060mdk.i586 : MySQL - server with Berkeley DB and Innodb support (to install)
 3- MySQL-NDB-4.1.12-3.2.20060mdk.i586 : MySQL - server with Berkeley DB, Innodb and NDB Cluster support (to install)
Que choisissez-vous ? (1-3) 1

Choisissez le paquetage 1 puis validez

Pour satisfaire les dépendances, les 6 paquetages suivants vont être installés (36 Mo):
MySQL-4.1.12-3.2.20060mdk.i586
MySQL-client-4.1.12-3.2.20060mdk.i586
MySQL-common-4.1.12-3.2.20060mdk.i586
libmysql14-4.1.12-3.2.20060mdk.i586
perl-DBD-mysql-3.0002-1mdk.i586
perl-DBI-1.48-1mdk.i586
Est-ce correct ? (O/n)

Appuyez sur entrée, le téléchargement commence l’installation va suivre

Préparation ...                  #############################################
      1/6: libmysql14            #############################################
      2/6: perl-DBI              #############################################
      3/6: perl-DBD-mysql        #############################################
      4/6: MySQL-client          #############################################
      5/6: MySQL-common          #############################################
      6/6: MySQL                 #############################################
ERROR: 1062  Duplicate entry 'localhost-root' for key 1
ERROR: 1062  Duplicate entry 'localhost-' for key 1
----------------------------------------------------------------------
Plus d'information sur le paquetage MySQL-4.1.12-3.2.20060mdk.i586
As a security measure networking in the mysql server has been disabled per
default, only localhost connections will work. This is because the mysql
root user has no password in a default install and that leaves the MySQL
server open for anyone to use.

To enable networking the user has to edit or delete the
/etc/sysconfig/mysqld file.
----------------------------------------------------------------------

Nous nous occuperons des messages d’erreur et des informations qui suivent plus tard. Essayons plutôt d’utiliser MySQL :

2.  Utilisation

# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

Cela ne fonctionne pas en effet, le démon mysqld n’est pas démarré. Lançons-le :

# /etc/init.d/mysqld start
Lancement de MySQL :                                            [  OK  ]

Maintenant que le programme MySQL tourne, nous allons pouvoir l’utiliser :

# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2 to server version: 4.1.12

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

3.  Création d’une base MySQL

Commencons par créer une base :

mysql> CREATE DATABASE maBase;
Query OK, 1 row affected (0.00 sec)

C’est tout. Pour nous en convaincre, affichons la liste des bases existantes :

mysql> SHOW DATABASES;
+----------+
| Database |
+----------+
| maBase   |
| mysql    |
| test     |
| tmp      |
+----------+
4 rows in set (0.00 sec)

Entrons dans notre base puis créons une table :

mysql> use maBase;
Database changed
mysql> CREATE TABLE ami (
    -> id INT(8) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    -> nom VARCHAR(12) NOT NULL,
    -> prenom VARCHAR(12) NOT NULL
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW TABLES;
+------------------+
| Tables_in_maBase |
+------------------+
| ami              |
+------------------+
1 row in set (0.00 sec)

Comme nous le montre la dernière commande “SHOW TABLES”, la table a bien été créée. Nous pouvons en demander la description détaillée :

mysql> DESC ami;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| id     | int(8)      |      | PRI | NULL    | auto_increment |
| nom    | varchar(12) |      |     |         |                |
| prenom | varchar(12) |      |     |         |                |
+--------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

4.  Remplissage de la table

La table peut maintenant être remplie :

mysql> INSERT INTO ami (nom,prenom) VALUES ('Bidochon','Raymonde');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO ami (nom,prenom) VALUES ('Bidochon','Dud');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO ami (nom,prenom) VALUES ('Medor','Le chien');
Query OK, 1 row affected (0.00 sec)

5.  Affichage

Afficher tout le contenu d’une table est simple :

mysql> SELECT * FROM ami;
+----+----------+----------+
| id | nom      | prenom   |
+----+----------+----------+
|  1 | Bidochon | Raymonde |
|  2 | Bidochon | Dud      |
|  3 | Medor    | Le chien |
+----+----------+----------+
3 rows in set (0.00 sec)

le joker * signifie “tout” mais on peut choisir d’afficher seulement certaines colonnes :

mysql> SELECT id,prenom FROM ami;
+----+----------+
| id | prenom   |
+----+----------+
|  1 | Raymonde |
|  2 | Dud      |
|  3 | Le chien |
+----+----------+
3 rows in set (0.00 sec)

ou encore afficher selon un critère :

mysql> SELECT * FROM ami WHERE nom='Bidochon';
+----+----------+----------+
| id | nom      | prenom   |
+----+----------+----------+
|  1 | Bidochon | Raymonde |
|  2 | Bidochon | Dud      |
+----+----------+----------+
2 rows in set (0.00 sec)

6.  Modification-Suppression de lignes

Pour modifier une ligne précise, il faut donner ä la requête un critère précis :

mysql> UPDATE ami SET prenom='Marcel' WHERE nom='Bidochon';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> SELECT * FROM ami;
+----+----------+----------+
| id | nom      | prenom   |
+----+----------+----------+
|  1 | Bidochon | Marcel   |
|  2 | Bidochon | Marcel   |
|  3 | Medor    | Le chien |
+----+----------+----------+
3 rows in set (0.00 sec)

Deux lignes ont été modifiées, soyons plus précis :

mysql> UPDATE ami SET prenom='Raymonde' WHERE id='2';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> SELECT * FROM ami;
+----+----------+----------+
| id | nom      | prenom   |
+----+----------+----------+
|  1 | Bidochon | Marcel   |
|  2 | Bidochon | Raymonde |
|  3 | Medor    | Le chien |
+----+----------+----------+
3 rows in set (0.00 sec)

De même pour une suppression

mysql> DELETE FROM ami WHERE id='3';
Query OK, 1 row affected (0.01 sec)

mysql> SELECT * FROM ami;
+----+----------+----------+
| id | nom      | prenom   |
+----+----------+----------+
|  1 | Bidochon | Marcel   |
|  2 | Bidochon | Raymonde |
+----+----------+----------+
2 rows in set (0.00 sec)

7.  Tables de liaison

Créons maintenant un seconde table pour les numeros de telephone et remplissons-la :

mysql> CREATE TABLE tel (
    -> tel_Id INT(8) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    -> genre ENUM('Portable','Domicile') NOT NULL,
    -> tel VARCHAR(20) NOT NULL
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO tel (genre,tel) VALUES ('Portable','0612345678');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO tel (genre,tel) VALUES ('Portable','0687654321');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO tel (genre,tel) VALUES ('Domicile','0123456789');
Query OK, 1 row affected (0.00 sec)

Dans la table ami, changeons id en ami_Id afin de bien clarifier les choses :

mysql> ALTER TABLE maBase.ami CHANGE id ami_Id INT(8) NOT NULL AUTO_INCREMENT;
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> DESC ami;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| ami_Id | int(8)      |      | PRI | NULL    | auto_increment |
| nom    | varchar(12) |      |     |         |                |
| prenom | varchar(12) |      |     |         |                |
+--------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

Créons maintenant la table de liaison entre les amis et leur(s) telephone(s)

mysql> CREATE TABLE amiTel (
    -> ami_Ide INT(8),
    -> tel_Ide INT(8)
    -> );
Query OK, 0 rows affected (0.01 sec)

M et Me Bidochon ont chacun leur portable…

mysql> INSERT INTO amiTel (ami_Ide,tel_Ide) VALUES ('1','1');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO amiTel (ami_Ide,tel_Ide) VALUES ('2','2');
Query OK, 1 row affected (0.00 sec)

…mais ils partagent le meme fixe :

mysql> INSERT INTO amiTel (ami_Ide,tel_Ide) VALUES ('1','3');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO amiTel (ami_Ide,tel_Ide) VALUES ('2','3');
Query OK, 1 row affected (0.00 sec)