MYSQL
MYSQL
MYSQL
MySQL
MySQL is a very popular, open source database.
Officially pronounced “my Ess Que Ell” (not my
sequel).
Handles very large databases; very fast
performance.
Why are we using MySQL?
Free (much cheaper than Oracle!)
Each student can install MySQL locally.
Easy to use Shell for creating tables, querying tables, etc.
Easy to use with Java JDBC
2
Basic Queries
Once logged in, you can try some simple queries.
For example:
3
Basic Queries
Keywords may be entered in any lettercase.
The following queries are equivalent:
4
Basic Queries
Here's another query. It demonstrates that
you can use mysql as a simple calculator:
5
Basic Queries
You can also enter multiple statements on a single
line. Just end each one with a semicolon:
6
Multi-Line Commands
mysql determines where your statement ends
by looking for the terminating semicolon, not
by looking for the end of the input line.
Here's a simple multiple-line statement:
mysql> SELECT
-> USER()
-> ,
-> CURRENT_DATE;
+--------------------+--------------+
| USER() | CURRENT_DATE |
+--------------------+--------------+
| joesmith@localhost | 1999-03-18 |
+--------------------+--------------+
7
Canceling a Command
mysql> SELECT
-> USER()
-> \c
mysql>
8
Using a Database
To get started on your own database, first check
which databases currently exist.
Use the SHOW statement to find out which
databases currently exist on the server:
9
Using a Database
10
Creating a Table
11
Creating a Table
12
Creating a Table
To create a table, use the CREATE TABLE
command:
13
Showing Tables
14
Describing Tables
To view a table structure, use the DESCRIBE
command:
mysql> describe pet;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| owner | varchar(20) | YES | | NULL | |
| species | varchar(20) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| birth | date | YES | | NULL | |
| death | date | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.02 sec)
15
Deleting a Table
16
SQL Select
SELECT what_to_select
FROM which_table
WHERE conditions_to_satisfy
17
Selecting All Data
The simplest form of SELECT retrieves everything
from a table
mysql> select * from pet;
+----------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+--------+---------+------+------------+------------+
| Fluffy | Harold | cat | f | 1999-02-04 | NULL |
| Claws | Gwen | cat | f | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Fang | Benny | dog | m | 1999-08-27 | NULL |
| Bowser | Diane | dog | m | 1998-08-31 | 1995-07-29 |
| Chirpy | Gwen | bird | f | 1998-09-11 | NULL |
| Whistler | Gwen | bird | | 1997-12-09 | NULL |
| Slim | Benny | snake | m | 1996-04-29 | NULL |
+----------+--------+---------+------+------------+------------+
8 rows in set (0.00 sec)
18
Selecting Particular Rows
You can select only particular rows from your
table.
For example, if you want to verify the change
that you made to Bowser's birth date, select
Bowser's record like this:
mysql> SELECT * FROM pet WHERE name = "Bowser";
+--------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+-------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1998-08-31 | 1995-07-29 |
+--------+-------+---------+------+------------+------------+
1 row in set (0.00 sec)
19
Selecting Particular Rows
20
Selecting Particular Columns
21
Selecting Particular Columns
mysql> select name, birth from pet;
+----------+------------+
| name | birth |
+----------+------------+
| Fluffy | 1999-02-04 |
| Claws | 1994-03-17 |
| Buffy | 1989-05-13 |
| Fang | 1999-08-27 |
| Bowser | 1998-08-31 |
| Chirpy | 1998-09-11 |
| Whistler | 1997-12-09 |
| Slim | 1996-04-29 |
+----------+------------+
8 rows in set (0.01 sec)
22
Sorting Data
To sort a result, use an ORDER BY clause.
For example, to view animal birthdays, sorted by
date:
mysql> SELECT name, birth FROM pet ORDER BY birth;
+----------+------------+
| name | birth |
+----------+------------+
| Buffy | 1989-05-13 |
| Claws | 1994-03-17 |
| Slim | 1996-04-29 |
| Whistler | 1997-12-09 |
| Bowser | 1998-08-31 |
| Chirpy | 1998-09-11 |
| Fluffy | 1999-02-04 |
| Fang | 1999-08-27 |
+----------+------------+
8 rows in set (0.02 sec)
23
Sorting Data
25
Working with NULLs
For example, to find all your dead pets (what a
morbid example!)
26
Counting Rows
Databases are often used to answer the question,
"How often does a certain type of data occur in a
table?"
For example, you might want to know how many pets
you have, or how many pets each owner has.
Counting the total number of animals you have is the
same question as “How many rows are in the pet
table?” because there is one record per pet.
The COUNT() function counts the number of non-
NULL results.
27
Counting Rows Example
28