1. Bases de datos relacionales

1.12. Otras utilidades de MySQL

Junto con el inventario de comandos que interactúan con nuestra base de datos empleando el lenguaje SQL, el SGBD MySQL proporciona un conjunto de aplicaciones elementales para asistirnos a la hora de trabajar con el sistema.

Por ejemplo, el comando help muestra por pantalla un breve manual de ayuda sobre cada instrucción de MySQL.

Para poder paginar sobre las entradas del manual, pantalla a pantalla, debemos ejecutar antes la aplicación pager. Dicho comando nos permite vincular una aplicación de paginación del terminal de Linux con el intérprete de MySQL (por ejemplo, el programa more).

mysql> pager more;

PAGER set to 'more'

mysql> help DROP TABLE;

Name: 'DROP TABLE'
Description:
Syntax:
DROP [TEMPORARY] TABLE [IF EXISTS]
    tbl_name [, tbl_name] ...
    [RESTRICT | CASCADE]

DROP TABLE removes one or more tables. You must have the DROP privilege for each table. 
All table data and the table definition are removed, so be careful with this statement!
If any of the tables named in the argument list do not exist, MySQL returns an error 
indicating by name which nonexisting tables it was unable to drop, but it also drops 
all the tables in the list that do exist.
-- More --

El comando source permite ejecutar ficheros de texto que incluyen comandos MySQL con la secuencia de instrucciones precisas para realizar una determinada tarea. Por ejemplo, podemos editar un fichero de texto desde nuestro terminal con los primeros comandos que ejecutamos al entrar en el sistema:

USE catalogo;
SHOW TABLES;
DESCRIBE genes;
---------------------------------------------------------
mysql> source ordres.sql;

Database changed

+--------------------+
| Tables_in_catalogo |
+--------------------+
| anotaciones        |
| funciones          |
| genes              |
| genomas            |
+--------------------+
4 rows in set (0.00 sec)

+--------------+------------------+------+-----+---------+-------+
| Field        | Type             | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+-------+
| nombre       | varchar(20)      | NO   | PRI | NULL    |       |
| cromosoma    | varchar(5)       | YES  |     | NULL    |       |
| hebra        | varchar(1)       | YES  |     | NULL    |       |
| inicio       | int(11)          | YES  |     | NULL    |       |
| final        | int(11)          | YES  |     | NULL    |       |
| proteina     | varchar(20)      | YES  |     | NULL    |       |
| especie      | varchar(100)     | YES  | MUL | NULL    |       |
+--------------+------------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

El comando status permite ver la configuración del sistema:

mysql> status;

mysql  Ver 14.14 Distrib 5.7.17, for Linux (i686) using EditLine wrapper
Connection id:          7
Current database:       cataleg
Current user:           student@localhost
SSL:                    Not in use
Current pager:          more
Using outfile:          ''
Using delimiter:        ;
Server version:         5.7.17-0ubuntu0.16.04.1 (Ubuntu)
Protocol version:       10
Connection:             Localhost via UNIX socket
Server characterset:    latin1
Db characterset:        latin1
Client characterset:    utf8
Conn. characterset:     utf8
UNIX socket:            /var/run/mysqld/mysqld.sock
Uptime:                 4 hours 14 min 12 sec
Threads:                1  Questions: 101  Slow queries: 0  Opens: 129 
Flush tables:           1  Open tables: 42  Queries per second avg: 0.006

Si en algún instante necesitamos acceder al terminal de Linux, podemos emplear el comando system para invocar sus comandos desde el interior de MySQL:

mysql> system (ls /home/student);

Desktop  Documents  Downloads Music  Pictures  Public  Templates  Videos

mysql> system (cat comandos.sql);

USE catalogo;

SHOW TABLES;

DESCRIBE genes;