MySQL
MySQL
EL HAMMOUDI HAMZA
[email protected]
2-Tier Architecture
Web
Web
Server
Browser
(Client)
PHP
2
3-Tier Architecture
3
SQL links
Tutorials
http://www.w3schools.com/sql/
http://www.sqlzoo.net
http://sqlcourse.com (part 2)
http://sqlcourse2/com (part 1)
MySQL online reference manual
http://dev.mysql.com/doc/mysql/en/Reference.ht
Make a request
(SQL query)
MySQL Client
Server Get results Program
WARNING
Always assume that everything is case
sensitive, especially table names.
This is not the case in Windows XP but it is
the case in Linux
mysql>
mysql>
Updating a record
UPDATE names SET lastName = 'Stone'
WHERE id=3;
SELECT * FROM names;
mysql> UPDATE names SET lastName = 'Stone' WHERE id=3;
Query OK, 1 row affected (0.28 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM names;
+----+-----------+------------+
| id | firstName | lastName |
+----+-----------+------------+
| 1 | Fred | Flintstone |
| 2 | Barney | Rubble |
| 3 | Ralph | Stone |
+----+-----------+------------+
3 rows in set (0.00 sec)
mysql>
25/03/2018 EL HAMMOUDI Hamza 15
Logging output
String types
CHAR
fixed length string, e.g., CHAR(20)
VARCHAR
variable length string, e.g., VARCHAR(20)
BLOB, TINYBLOB, MEDIUMBLOB, LONGBLOB
same as TEXT, TINYTEXT ...
ENUM
list of items from which value is selected
SHOW
Display databases or tables in current database;
Example (command line client):
show databases;
show tables;
USE
Specify which database to use
Example
use bookstore;
25/03/2018 EL HAMMOUDI Hamza 24
The CREATE Command (1)
USE test;
CREATE TABLE marks (
studentID SMALLINT AUTO_INCREMENT NOT NULL,
first_name VARCHAR(20) NOT NULL,
last_name VARCHAR(20) NOT NULL,
mark SMALLINT DEFAULT 0 NOT NULL,
PRIMARY KEY (studentID)
);
source c:/.........../marks.sql
+-----------+------------+-----------+------+
| studentID | first_name | last_name | mark |
+-----------+------------+-----------+------+
| 1 | Fred | Jones | 78 |
| 2 | Bill | James | 67 |
| 3 | Carol | Smith | 82 |
| 4 | Bob | Duncan | 60 |
| 5 | Joan | Davis | 86 |
+-----------+------------+-----------+------+
5 rows in set (0.00 sec)
+-----------+------------+-----------+------+
| studentID | first_name | last_name | mark |
+-----------+------------+-----------+------+
| 2 | Bill | James | 67 |
| 3 | Carol | Smith | 82 |
| 4 | Bob | Duncan | 60 |
+-----------+------------+-----------+------+
3 rows in set (0.01 sec)
+-----------+------------+-----------+------+
| studentID | first_name | last_name | mark |
+-----------+------------+-----------+------+
| 3 | Carol | Smith | 82 |
| 5 | Joan | Davis | 86 |
+-----------+------------+-----------+------+
2 rows in set (0.00 sec)
+-----------+------------+-----------+------+
| studentID | first_name | last_name | mark |
+-----------+------------+-----------+------+
| 5 | Joan | Davis | 86 |
| 3 | Carol | Smith | 82 |
| 1 | Fred | Jones | 78 |
| 2 | Bill | James | 67 |
| 4 | Bob | Duncan | 60 |
+-----------+------------+-----------+------+
5 rows in set (0.00 sec)
+----------+
| COUNT(*) |
+----------+
| 5 |
+----------+
1 row in set (0.00 sec)
USE web_db;
CREATE TABLE books (
isbn CHAR(15) PRIMARY KEY NOT NULL,
title VARCHAR(100) NOT NULL,
author VARCHAR(100) NOT NULL,
pub VARCHAR(20) NOT NULL,
year YEAR NOT NULL,
price DECIMAL(9,2) DEFAULT NULL
);
25/03/2018 EL HAMMOUDI Hamza 52
books.sql (2)
source c:/.........../books.sql
employeeID hours
jobs
table
source c:/......./employee_db.sql
+------------+------+------------+--------------+------------+-------+
| employeeID | name | position | address | employeeID | hours |
+------------+------+------------+--------------+------------+-------+
| 1001 | Fred | programmer | 13 Windle St | 1001 | 13.50 |
| 1002 | Joan | programmer | 23 Rock St | 1001 | 13.50 |
| 1003 | Bill | manager | 37 Front St | 1001 | 13.50 |
| 1001 | Fred | programmer | 13 Windle St | 1002 | 2.00 |
| 1002 | Joan | programmer | 23 Rock St | 1002 | 2.00 |
| 1003 | Bill | manager | 37 Front St | 1002 | 2.00 |
| 1001 | Fred | programmer | 13 Windle St | 1002 | 6.25 |
| 1002 | Joan | programmer | 23 Rock St | 1002 | 6.25 |
| 1003 | Bill | manager | 37 Front St | 1002 | 6.25 |
+------+-------+
| name | hours |
+------+-------+
| Fred | 13.50 |
Here we are replacing
| Joan | 2.00 | the employeeID
| Joan | 6.25 | numbers in the jobs
| Bill | 4.00 |
| Fred | 1.00 | table by the employee's
| Bill | 7.00 | name
| Bill | 9.50 |
+------+-------+
7 rows in set (0.00 sec)
+------+-------+
| name | hours |
+------+-------+
| Fred | 13.50 |
| Fred | 1.00 |
+------+-------+
2 rows in set (0.00 sec)
+------+------------+
| name | SUM(hours) |
+------+------------+
| Bill | 20.50 |
| Fred | 14.50 |
| Joan | 8.25 |
+------+------------+
3 rows in set (0.00 sec)
+------+------------+
| name | SUM(hours) |
+------+------------+
| Fred | 14.50 |
+------+------------+
1 row in set (0.00 sec)