Erste Schritte mit mysql Der Umgang mit einer relationalen Datenbank
Relationale Datenbanken Prinzip: Daten sind in Tabellen gespeichert Tabellen können verknüpft sein alter Name: RDBMS - Relational Database Management System erlaubt das geordnete Speichern und Abrufen großer Datenmengen dazu wird die Sprache SQL (Structured Query Language) benutzt
Structured Query Language SQL ist eine einfache Sprache Beispiel: mysql starten: mysql -uusername -p Datenbank erzeugen: mysql> CREATE DATABASE meinedb; Query OK, 1 row affected (0.01 sec)
Verfügbare Datentypen INT[(SIZE)] [UNSIGNED] [ZEROFILL] A simple integer between -2147483648 and +2147483647 or, if the UNSIGNED attribute is provided, between zero and 4294967295. The ZEROFILL attribute indicates that the number should be prefixed with zeros until the number is SIZE digits in length. VARCHAR[(SIZE)] [BINARY] A variable-length string that is a maximum of SIZE characters in length (where SIZE cannot exceed 255). Unless the BINARY attribute is provided, this data type is considered case-insensitive and obviously cannot hold binary data. TEXT A case-sensitive string that is a maximum of 65,535 characters in length. DATETIME A date and time ranging from 1000-01-01 00:00:00 to 9999-12-31 23:59:59 (dates are in YYYY-MM-DD HH: MM:SS format). DATE Similar to the DATETIME data type, except without the time in YYYY-MM-DD format.
Tabellen erzeugen festlegen, mit welcher Datenbank gearbeitet werden soll: mysql> USE meinedb; Database Changed 2 Tabellen anlegen: mysql> CREATE TABLE books( book_id INT AUTO_INCREMENT PRIMARY KEY, author_id INT, title VARCHAR(255), pub_date DATE); Query OK, 0 rows affected (0.02 sec) mysql> CREATE TABLE authors( author_id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), state VARCHAR(2)); Query OK, 0 rows affected (0.02 sec)
Tabellenstruktur ansehen mysql> DESC books; Field Type Collation Null Key Default Extra book_id int(11) binary YES PRI NULL auto_increment author_id int(11) binary YES NULL title varchar(255) latin1_swedish_ci YES NULL pub_date date latin1_swedish_ci YES NULL 4 rows in set (0.00 sec)
Daten einfügen mysql> INSERT INTO books VALUES(1, 1, "PHP Unleashed", "2003-11-01"); Query OK, 1 row affected (0.01 sec) mysql> INSERT INTO authors VALUES(1, "John Coggeshall","MI"); Query OK, 1 row affected (0.01 sec)
Alle Tabellen einer DB anzeigen mysql> SHOW TABLES; +-----------------------------+ Tables in fundamentals +-----------------------------+ books authors +-----------------------------+ 2 rows in set (0.03 sec)
Alle Daten auslesen mysql> select * from books; +---------+-----------+------------------+------------+ book_id author_id title pub_date +---------+-----------+------------------+------------+ 1 1 PHP Unleashed 2003-11-01 2 1 PHP4 Programming 2002-10-01 3 2 Cool Stuff 2002-03-23 4 3 Another Book 2001-02-01 +---------+-----------+------------------+------------+ 4 rows in set (0.00 sec)
Bestimmte Spalten auslesen mysql> SELECT title FROM books; +------------------+ title +------------------+ PHP Unleashed PHP4 Programming Cool Stuff Another Book +------------------+ 4 rows in set (0.00 sec) mysql> SELECT title, pub_date FROM books; +------------------+------------+ title pub_date +------------------+------------+ PHP Unleashed 2003-11-01 PHP4 Programming 2002-10-01 Cool Stuff 2002-03-23 Another Book 2001-02-01 +------------------+------------+ 4 rows in set (0.00 sec)
Bestimmte Daten in bestimmten Spalten auswählen mysql> SELECT * FROM books WHERE author_id=1; +---------+-----------+------------------+------------+ book_id author_id title pub_date +---------+-----------+------------------+------------+ 1 1 PHP Unleashed 2003-11-01 2 1 PHP4 Programming 2002-10-01 +---------+-----------+------------------+------------+ 2 rows in set (0.00 sec)
Selektionskriterien Expression A = B A!= B A <= B A > B A < B A > B A <=> B A IS NULL Evaluation True if A equals B True if A does not equal B True if A is less than or equal to B True if A is greater than or equal to B True if A is less than B True if A is greater than B True if A is equal to B (NULL Safe) True if A is NULL A IS NOT NULL True if A is not NULL A BETWEEN M AND N True if A is between values M and N A NOT BETWEEN M AND N True if A is not between values M and N A IN(value, value2,...) True if A is one of the listed values A NOT IN (value, value2,...) True if A is not one of the listed values
Suchen mit Wildcards mysql> SELECT * FROM books WHERE title LIKE "%PHP%"; +---------+-----------+------------------+------------+ book_id author_id title pub_date +---------+-----------+------------------+------------+ 1 1 PHP Unleashed 2003-11-01 2 1 PHP4 Programming 2002-10-01 +---------+-----------+------------------+------------+ 2 rows in set (0.04 sec)
Einen Datensatz aktualisieren mysql> UPDATE authors SET state='fl' WHERE author_id=1; Query OK, 1 row affected (0.05 sec) Rows matched: 1 Changed: 1 Warnings: 0 allgemeine Syntax: UPDATE <table> SET <column>=<value>, <column>=<value>,... WHERE <conditions>
Datensatz löschen mysql> DELETE FROM authors WHERE author_id=1; Query OK, 1 row affected (0.06 sec) allgemeine Syntax: DELETE FROM <table> WHERE <conditions>
Tabelle erweitern mysql> ALTER TABLE books ADD isbn VARCHAR(25) AFTER pub_date; Query OK, 4 rows affected (0.10 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> DESC books; Field Type Collation Null Key Default Extra book_id int(11) binary YES PRI NULL auto_increment author_id int(11) binary YES NULL title varchar(255) latin1_swedish_ci YES NULL pub_date date latin1_swedish_ci YES NULL isbn varchar(25) latin1_swedish_ci YES NULL 5 rows in set (0.00 sec)
Tabelle verkleinern mysql> ALTER TABLE books DROP isbn; Query OK, 4 rows affected (0.00 sec) Records: 4 Duplicates: 0 Warnings: 0 allgemeine Syntax des ALTER-Befehls: ALTER TABLE <table> [ADD DROP] <column definition> [FIRST AFTER <column>]
DBs und Tabellen löschen DROP [TABLE DATABASE] <table or database name>
Datensätze sortieren mysql> SELECT name FROM authors ORDER BY name; +-----------------+ name +-----------------+ Jennifer Author Joe Coolguy John Coggeshall +-----------------+ 3 rows in set (0.00 sec) mysql> SELECT name FROM authors ORDER BY name DESC; +-----------------+ name +-----------------+ John Coggeshall Joe Coolguy Jennifer Author +-----------------+ 3 rows in set (0.00 sec) mysql> SELECT name, state FROM authors ORDER BY name, state; +-----------------+-------+ name state +-----------------+-------+ Jennifer Author KS Joe Coolguy AZ John Coggeshall MI +-----------------+-------+ 3 rows in set (0.00 sec)
Datensätze aus mehreren Tabellen zusammenstellen mysql> SELECT books.title, authors.name FROM books, authors WHERE books.author_id=authors.author_id; +------------------+-----------------+ title name +------------------+-----------------+ PHP Unleashed John Coggeshall PHP4 Programming John Coggeshall Cool Stuff Joe Coolguy Another Book Jennifer Author +------------------+-----------------+ Joins 4 rows in set (0.09 sec) mysql> DESC books; Field Type Collation Null Key Default Extra book_id int(11) binary YES PRI NULL auto_increment author_id int(11) binary YES NULL title varchar(255) latin1_swedish_ci YES NULL pub_date date latin1_swedish_ci YES NULL 4 rows in set (0.00 sec) mysql> DESC authors; Field Type Collation Null Key Default Extra author_id int(11) binary YES PRI NULL auto_increment name varchar(255) latin1_swedish_ci YES NULL state char(2) latin1_swedish_ci YES NULL 3 rows in set (0.01 sec)
Anzahl Datensätze in einer Tabelle zählen SQL-Funktion COUNT: mysql> SELECT COUNT(title) FROM books; +--------------+ COUNT(title) +--------------+ 4 +--------------+ 1 row in set (0.00 sec) mysql> SELECT authors.name, COUNT(books.title) AS books FROM authors,books WHERE books.author_id = authors.author_id GROUP BY name; +-----------------+-------+ name books +-----------------+-------+ Jennifer Author 1 Joe Coolguy 1 John Coggeshall 2 +-----------------+-------+ 3 rows in set (0.01 sec)