Prüfungsvorbereitung

2.1 Using MySQL interactively

Wenn man vom interaktiven Mode spricht, meint man nichts anderes, wenn man manuell über die Konsole Befehle eintippt, um beispielsweise mit dem Programm mysql Daten ausliest, Werte ändert oder Statusinformationen anzeigen lässt. Im Gegensatz zum interaktiven Mode gibt es noch den Batchmode. Hier werden Befehle in Dateien gespeichert, um automatisiert mehrere Befehle ausführen zu lassen.


Jeder Befehl im interaktiven Mode muss mit einem Terminator, in der Regel einem Semikolon, abgeschlossen werden. Es ist nämlich möglich, zwei oder mehrere Befehle in einer Zeile zu schreiben.


mysql> SELECT * FROM table1; STATUS();


Wenn man kein Semikolon eintippt und anschließend die Enter-Taste betätigt, ist der Befehl noch nicht beendet und man kann den Befehl vervollständigen. Dies ist übersichtliche. Mit einem \c kann man den aktuellen Befehl „löschen“:


mysql> SELECT haus, nr, plz FROM table1

mysql> WHERE \c // Löschen des aktuellen Befehls

mysql>


mysql kann man mit \q, QUIT oder Exit beenden.


Damit man nicht immer in den interaktiven Modus wechseln muss, kann man die Option -e / --execute nutzen. Dieser Befehl ermöglicht es, dass man Befehle direkt von der Konsole eingeben kann:


shell > mysql --execute: Befehle direkt über die Konsole ausführen

~ $ mysql -u root -p -e "show databases;"
Enter password:
+---------------------------+
| Database |
+---------------------------+
| information_schema |
| lokal |

2.2 Statement Terminators

mysql> SELECT * FROM table1; Standard-Terminator

mysql> SELECT * FROM table1 \g Entspricht dem Semikolon

mysql> SELECT * FROM table1 \G Terminator, mysql zeigt die Ausgabe nicht in Tabellenform an

2.3 MySQL Prompts

mysql> Wartet auf neue Eingaben

mysql-> Wartet auf die nächste Eingabe in einer neuen Zeile

mysql'> Wartet auf das Ende des Blocks beginnend mit '

mysql“> Wartet auf das Ende des Blocks beginnend mit "

mysql`> Wartet auf das Ende des Blocks beginnend mit `

mysql/*> Wartet auf das Ende des Kommentars beginnend mit /*

2.5 Using Script Files with MySQL

Anstatt alle Befehele nach und nach per Hand über die Konsole einzutippen, kann die Befehle in einer einfachen Textdatei schreiben, um sie anschließend in MySQL zu importieren. Somit wäre es beispielsweise möglich, bestimmte Routineaufgaben zeitgesteuert ausführen zu lassen, ohne selbst am PC zu sitzen.


mysql> SOURCE update.txt // Ausführen der Befehle mittels Befehl SOURCE in MySQL.

mysql> SOURCE /tmp/update.txt // Wenn sich die Datei nicht im selben Verzeichniss befindet.


shell> mysql DATABASE < input.txt // Import direkt über die Konsole

2.6 MySQL Output Format

Wenn man sich mit mysql mit einer Datenbank verbindet, wird die Ausgabe auf der Konsole standardmäßig in Tabellenform angezeigt. Dieses kann man aber ändern:


shell> mysql --batch / -b // Batchmode-Anzeige (TABs)

shell> mysql --table / -t // Anzeige in Tabellenform

shell> mysql --html / -h // Produziert HTML-Code

shell> mysql --xml / -X // Produziert XML-Code


Beispiel:


$ mysql -u root -p --xml 
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.

mysql> show databases;
<?xml version="1.0"?>

<resultset statement="show databases;" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
<field name="Database">information_schema</field>
</row>

2.7 Client Commands and SQL Statements

Über die bekannten Befehle SELECT, UPDATE, INSERT, etc schickt das Programm mysql die Anfragen an den MySQL-Server. Es gibt aber auch mysql-spezifische Befehle. Eine Übersicht dieser Befehle erhält man mit \HELP:


? (\?) Synonym for `help'.

clear (\c) Clear command.

connect (\r) Reconnect to the server. Optional arguments are db and host.

delimiter (\d) Set statement delimiter. NOTE: Takes the rest of the line as new delimiter.

edit (\e) Edit command with $EDITOR.

ego (\G) Send command to mysql server, display result vertically.

exit (\q) Exit mysql. Same as quit.

go (\g) Send command to mysql server.

help (\h) Display this help.

nopager (\n) Disable pager, print to stdout.

notee (\t) Don't write into outfile.

pager (\P) Set PAGER [to_pager]. Print the query results via PAGER.

print (\p) Print current command.

prompt (\R) Change your mysql prompt.

quit (\q) Quit mysql.

rehash (\#) Rebuild completion hash.

source (\.) Execute an SQL script file. Takes a file name as an argument.

status (\s) Get status information from the server.

system (\!) Execute a system shell command.

tee (\T) Set outfile [to_outfile]. Append everything into given outfile.

use (\u) Use another database. Takes database name as argument.

charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.

warnings (\W) Show warnings after every statement.

nowarning (\w) Don't show warnings after every statement.

2.8 Using Server-Side Help

Wenn man einmal nicht weiter weiß oder die Syntax eines bestimmten Befehls vergessen hat, kann man die interne Hilfe nutzen. Einen Überblick über die Kategorien erhält man folgendermaßen:

mysql> HELP contents;
You asked for help about help category: "Contents"
For more information, type 'help <item>', where <item> is one of the following
categories:
Administration
Column Types
Data Definition
Data Manipulation
Functions
Geographic features
Transactions

Wenn man sich nicht mehr erinnern kann, wie ein Befehl genau lautet, kann man auch HELP für die Suche verwenden. Beispiel: man sucht einen Befehl in Verbindung mit Status:

mysql> HELP Status;
Many help items for your request exist.
To make a more specific request, please type 'help <item>',
where <item> is one of the following
topics:
SHOW
SHOW MASTER STATUS
SHOW SLAVE STATUS

Oder man lässt sich einfach die Syntax für einen bestimmten Befehl anzeigen:

mysql> HELP INSERT;
Name: 'INSERT'
Description:
INSERT(str,pos,len,newstr)
Returns the string str, with the substring beginning at position
pos and len characters long replaced by the string
newstr. Returns the original string if pos is not within
the length of the string. Replaces the rest of the string from position
pos is len is not within the length of the rest of the string.
Returns NULL if any argument is null.
Examples:
mysql> SELECT INSERT('Quadratic', 3, 4, 'What');
-> 'QuWhattic'
mysql> SELECT INSERT('Quadratic', -1, 4, 'What');
-> 'Quadratic'
mysql> SELECT INSERT('Quadratic', 3, 100, 'What');
-> 'QuWhat'

2.9 Using the --safe-updates Option

Man kann den Mysql-Monitor mit der Option --safe-updates oder --i-am-a-dummy (kein Witz!) starten, so dass Änderungen an den Datenbanken nur beschränkt möglich sind. Dies ist vor allem dann von Nutzen, wenn man Anfänger ist oder wenn man an hochverfügbaren Datenbanken arbeitet:


shell> mysql --safe-updates DATABASE


Die Option --safe-updates wirkt sich unter anderem folgendermaßen aus:


- Die Befehle UPDATE und DELETE werden nur in Verbindung mit WHERE und LIMIT ausgeführt

- Die Ausgabe von SELECT ist auf 1.000 beschränkt