Professional Documents
Culture Documents
A basic introduction
James Brucker
Database Management System
SELECT * FROM
city WHERE name
LIKE Ban%
Database
Client Manager
"mysql" utility
Server
mysqld
Java App
+JDBC client
server controls
access to
Excel client
database
Client Tools
mysql-workbench-gpl-5.x.y-win32.msi
or use older GUI Tools:
mysql-gui-tools-5.1.7-win32.msi
From: se.cpe.ku.ac.th/download/mysql
Add MySQL "bin" to your Path
+------+------------+-------+-------------+---------+
| ID | Name | CCode | District | Populatn
records +------+---------------+------------------+---------+
| 3320 | Bangkok | THA | Bangkok | 6320174 |
(rows) | 3321 | Nonthaburi | THA | Nonthaburi | 292100 |
| 3323 | Chiang Mai | THA | Chiang Mai | 171100 |
+------+------------+-------+-------------+---------+
fields (columns)
Key field for Identifying Rows
A table contains a primary key that uniquely identifies
a row of data.
Each record must have a distinct value of primary key
The primary key is used to relate (join) tables.
ID is the primary key in City table.
+------+------------+-------+-------------+---------+
| ID | Name | CCode | District | Populatn
+------+---------------+------------------+---------+
| 3320 | Bangkok | THA | Bangkok | 6320174 |
| 3321 | Nonthaburi | THA | Nonthaburi | 292100 |
| 3323 | Chiang Mai | THA | Chiang Mai | 171100 |
+------+------------+-------+-------------+---------+
Structure of a Table
Every field has:
a name
a data type and length
To view the structure of a table use:
DESCRIBE tablename
sql> DESCRIBE City;
+-------------+-----------+-----+-----+---------+----------------+
| Field | Type | Null| Key | Default | Extra |
+-------------+-----------+-----+-----+---------+----------------+
| ID | int(11) | NO | PRI | | auto_increment |
| Name | char(35) | NO | | | |
| CountryCode | char(3) | NO | | | |
| District | char(20) | NO | | | |
| Population | int(11) | NO | | 0 | |
+-------------+-----------+-----+-----+---------+----------------+
Structure of a Table
"SHOW columns FROM tablename"
shows the same information.
FROM
world
;
DESCRIBE
DESCRIBE shows the structure of a table.
same as "SHOW columns FROM tablename".
+------+------------+--------------+---------+
| ID | Name | District | Popula..}
+------+------------+--------------+---------+
| 3320 | Bangkok | Bangkok | 6320174 |
records | 3321 | Nonthaburi | Nonthaburi | 292100 |
(rows) | 3323 | Chiang Mai | Chiang Mai | 171100 |
+------+------------+--------------+---------+
fields (columns)
Qualifying Names
SQL uses "." to qualify elements of a hierarchy
just like most O-O languages
World.city "city" table in World db
city.name name field in city table
World.city.name fully qualified name
What is CRUD?
Programmers call these operations "CRUD".
What does CRUD stand for?
Querying Data in a Table
SELECT displays field values from a table:
SELECT field1, field2, field3 FROM table ;
displays ALL rows from the table.
use LIMIT number to limit how many results.
Oops!
Changed every country I forgot "WHERE ..."
in the database!!
Warning: UPDATE is immediate!
Changes occur immediately. (Can't undo w/o trans.)
Be Careful! If you forget the WHERE clause it will
change all the rows in the table!
sql> UPDATE country SET HeadOfState='Obama';
/* Oops! I forgot "WHERE ..." */
+------+----------------+-------------+--------------+
| Code | Name | Continent | HeadOfState |
+------+----------------+-------------+--------------+
| AFG | Afghanistan | Asia | Obama |
| NLD | Netherlands | Europe | Obama |
| ALB | Albania | Europe | Obama |
| DZA | Algeria | Africa | Obama |
| ASM | American Samoa | Oceania | Obama |
| AND | Andorra | Europe | Obama
Obama |
rules!
| AGO | Angola | Africa | Obama |
Exercise
Update the City you added to the database.
Change its population.
Deleting Records
DELETE one or more records
DELETE FROM tablename WHERE condition;
City Country
ID (PK) Code (PK)
Name CountryCode Name
CountryCode (FK) Continent
Population Capital
District ...
Joining Tables
Relate or "join" tables using a condition.
Use "table.field" to qualify a field name:
Country.code Country.name
City Country
ID (PK) Code (PK)
Name * 1 Name
CountryCode (FK) Continent
Population Capital
District ...
City.countrycode = Country.code
Example: Join Country and City
Country City
Code ID
Name Country.Code = City.CountryCode Name
Continent CountryCode
Region District
SurfaceArea Population
Population
GNP
LocalName
Capital
Use Aliases to Reduce Typing
Country City
Code (PK) ID (PK)
Name Name
Continent CountryCode
Region District
SurfaceArea Population
Population
GNP
LocalName
Capital
Exercise Solution
List the country name and capital city name, for all
countries in Asia.
Example:
alias for
you can omit table name CountryLanguage
when there is no ambiguity
Exercise
1. What countries use English?
ORDER the results by percentage spoken, from
largest to smallest %.
2. In how many countries is English the official
language?
Harder
3. In the world, approximately how many people speak
English?
sum( C.population * L.percentage / 100 )
JOIN
Joins tables
Many forms:
INNER JOIN (include only matching columns)
OUTER JOIN (include all columns)
LEFT OUTER JOIN
NATURAL JOIN
CONDITION JOIN
AND
SELECT Name, SurfaceArea FROM Country WHERE
Continent = 'Africa' AND SurfaceArea > 1000000;
NOT
SELECT * FROM Accounts WHERE
NOT AvailableBalance = 0;
Set operations
IN
SELECT * FROM City WHERE
District IN ('Songkhla', 'Bangkok');
Exercise for matching
1. How many countries have a government that is any
form of monarchy?
match any government containing 'Monarchy'
How many are some form of monarchy, but not a
Constitutional Monarchy (like Thailand)?
GROUP BY ... HAVING ...
GROUP BY ... used to apply a function to a group of
rows having a characteristic.
HAVING ... is used to put a condition on the groups.
mysql> HELP
If MySql doesn't have help on SQL commands, then load the "help
tables" data onto your server. Download help table data from:
http://dev.mysql.com/downloads in the "Documentation" section.
Subqueries
Use the result of one query as part of another query.
Subquery
To use subqueries in MySQL you need version 4.1 or newer.
Exercise
In which country do people live the longest?
How long to they live?
SELECT ...
FROM Country
WHERE region = (SELECT region WHERE ...)
ORDER BY ...;
File: langview.sql
Database Game
Vocabulary
"largest" and "smallest" refer to size (surfaceArea).
SELECT ...
FROM Country C
JOIN CountryLanguage L
ON C.code = L.countrycode
WHERE ...
How many people speak English?
2 queries:
how many in each country
total for world
SELECT ...
FROM Country C
JOIN CountryLanguage L
ON C.code = L.countrycode
WHERE ...
History of Empires through Language
Empires are cultures that spread over many countries.
+-----------------+------------+-------------------------+
| name | population | country_name |
+-----------------+------------+-------------------------+
...
| Bangkok | 6320174 | Thailand |
...
Where is Dari the Official Language?
In what country is Dari the official language?
4 official languages?
What country in Europe has 4 official languages?
SELECT ...
FROM Country C
JOIN CountryLanguage L
ON C.code = L.countrycode
WHERE ...
GROUP BY C.code -- group by country
HAVING ...
Resources
MySQL
http://dev.mysql.com/tech-resources/articles/
Learning SQL
http://www.w3schools.com/sql/
nice tutorial and command reference