Beruflich Dokumente
Kultur Dokumente
Required Software
1. mysql command line client. This is included in the mysql-essentials package or MySQL
Community Edition from www.mysql.com.
2. (Optional) Visual query tool for MySQL. The free MySQL Workbench (requires Visual C++
redistribution pack and .Net 4.0) or MySQL GUI Tools (does not require C++) are GUI tools for
accessing MySQL.
2. World database. You will use the database on se.cpe.ku.ac.th (nothing to install.) But,
if you want your own World database, see separate instructions for how to install.
Format of Exercises
Write the SQL command you use to get the data. If the result is requested, write that, too.
Answer:
4.4 What is the total GNP of the world (in millions USD)?
sql> SELECT SUM(GNP) FROM country;
Answer:
Answer:
6 Modifying a Record
To change some values, use:
UPDATE tablename SET field1=value1, field2=value2 WHERE condition
Example: The "head of state" in the U.S.A. is now Barrack Obama.
UPDATE Country SET HeadOfState='Barrack Obama' WHERE code='USA';
7 Deleting Data
DELETE FROM tablename WHERE condition
Example: delete all cities in Thailand named "Bangsaen":
DELETE FROM City WHERE name='Bangsaen' AND countrycode='THA';
7.1 What is the command to delete the City that you added to the database?
sql> DELETE FROM City WHERE name='Bangsaen' AND ID=5000;
9 JOIN Tables
A more semantic way of combining tables is to use JOIN
SELECT field1, field2, ...
SQL Database Lab Page 9
FROM table1
JOIN table2
ON table1.field = table2.field
WHERE ... ORDER BY ...
You can assign an alias to table names to reduce typing. You can assign an alias to fields or
expressions, too.
Example: Print the capital city of the countries in Europe:
SELECT c.Name, co.Name AS CountryName, co.Region
FROM Country co
JOIN City c
ON co.Capital = c.ID
WHERE co.Continent = 'Europe'