SQL - Summary
SHOW [ DATABASES | TABLES | COLUMNS FROM TableName ]SHOW [ CREATE [ DATABASE DBName | TABLE TableName ] ]SHOW [ STATUS | GRANTS | ERRORS | WARNINGS ]
SQL - Summary
SHOW [ ... ]
SELECT [ColName, ColName, ...] FROM [ TableName, TableName, ... ]WHERE condition [logic condition ]
SQL - SELECT – ORDER BYCommands can be combined but order is important.
mysql> SELECT * FROM City WHERE ID < 20 ORDER BY CountryCode DESC, Name;
+----+-----------+-------------+---------------+------------+
| ID | Name | CountryCode | District | Population |
+----+-----------+-------------+---------------+------------+
| 17 | Almere | NLD | Flevoland | 142465 |
| 5 | Amsterdam | NLD | Noord-Holland | 731200 |
| 13 | Apeldoorn | NLD | Gelderland | 153491 |
| 18 | Arnhem | NLD | Gelderland | 138020 |
| 12 | Breda | NLD | Noord-Brabant | 160398 |
| 9 | Eindhoven | NLD | Noord-Brabant | 201843 |
| 15 | Enschede | NLD | Overijssel | 149544 |
| 11 | Groningen | NLD | Groningen | 172701 |
| 7 | Haag | NLD | Zuid-Holland | 440900 |
| 16 | Haarlem | NLD | Noord-Holland | 148772 |
...............
mysql> SELECT * FROM City ORDER BY CountryCode DESC, Name WHERE ID < 20;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to use
near ’WHERE ID < 20’ at line 1
SQL - Summary
SHOW [ ... ]
SELECT [ColName, ColName, ...] FROM [ TableName, TableName, ... ]WHERE condition [logic condition ]ORDER BY [ColName, ColName, ... ]
SQL - SELECT – LIMITToo much output can be controlled with LIMIT.
mysql> SELECT * FROM City LIMIT 15;
+----+----------------+-------------+---------------+------------+
| ID | Name | CountryCode | District | Population |
+----+----------------+-------------+---------------+------------+
| 1 | Kabul | AFG | Kabol | 1780000 |
| 2 | Qandahar | AFG | Qandahar | 237500 |
| 3 | Herat | AFG | Herat | 186800 |
| 4 | Mazar-e-Sharif | AFG | Balkh | 127800 |
| 5 | Amsterdam | NLD | Noord-Holland | 731200 |
| 6 | Rotterdam | NLD | Zuid-Holland | 593321 |
| 7 | Haag | NLD | Zuid-Holland | 440900 |
| 8 | Utrecht | NLD | Utrecht | 234323 |
| 9 | Eindhoven | NLD | Noord-Brabant | 201843 |
| 10 | Tilburg | NLD | Noord-Brabant | 193238 |
| 11 | Groningen | NLD | Groningen | 172701 |
| 12 | Breda | NLD | Noord-Brabant | 160398 |
| 13 | Apeldoorn | NLD | Gelderland | 153491 |
| 14 | Nijmegen | NLD | Gelderland | 152463 |
| 15 | Enschede | NLD | Overijssel | 149544 |
+----+----------------+-------------+---------------+------------+
15 rows in set (0.07 sec)
SQL - SELECT – LIMITToo much output can be controlled with LIMIT.
mysql> SELECT * FROM City LIMIT 10 OFFSET 20;
+----+----------------+-------------+---------------+------------+
| ID | Name | CountryCode | District | Population |
+----+----------------+-------------+---------------+------------+
| 21 | Amersfoort | NLD | Utrecht | 126270 |
| 22 | Maastricht | NLD | Limburg | 122087 |
| 23 | Dordrecht | NLD | Zuid-Holland | 119811 |
| 24 | Leiden | NLD | Zuid-Holland | 117196 |
| 25 | Haarlemmermeer | NLD | Noord-Holland | 110722 |
| 26 | Zoetermeer | NLD | Zuid-Holland | 110214 |
| 27 | Emmen | NLD | Drenthe | 105853 |
| 28 | Zwolle | NLD | Overijssel | 105819 |
| 29 | Ede | NLD | Gelderland | 101574 |
| 30 | Delft | NLD | Zuid-Holland | 95268 |
+----+----------------+-------------+---------------+------------+
10 rows in set (0.00 sec)
mysql>
SQL - SELECT – LIMIT... or ... what I find confusing ...
mysql> SELECT * FROM City LIMIT 20,10;
+----+----------------+-------------+---------------+------------+
| ID | Name | CountryCode | District | Population |
+----+----------------+-------------+---------------+------------+
| 21 | Amersfoort | NLD | Utrecht | 126270 |
| 22 | Maastricht | NLD | Limburg | 122087 |
| 23 | Dordrecht | NLD | Zuid-Holland | 119811 |
| 24 | Leiden | NLD | Zuid-Holland | 117196 |
| 25 | Haarlemmermeer | NLD | Noord-Holland | 110722 |
| 26 | Zoetermeer | NLD | Zuid-Holland | 110214 |
| 27 | Emmen | NLD | Drenthe | 105853 |
| 28 | Zwolle | NLD | Overijssel | 105819 |
| 29 | Ede | NLD | Gelderland | 101574 |
| 30 | Delft | NLD | Zuid-Holland | 95268 |
+----+----------------+-------------+---------------+------------+
10 rows in set (0.00 sec)
mysql>
SQL - SELECT – LIMIT
All combined
mysql> SELECT * FROM City WHERE CountryCode="USA"
-> ORDER BY Population DESC LIMIT 5;
+------+--------------+-------------+--------------+------------+
| ID | Name | CountryCode | District | Population |
+------+--------------+-------------+--------------+------------+
| 3793 | New York | USA | New York | 8008278 |
| 3794 | Los Angeles | USA | California | 3694820 |
| 3795 | Chicago | USA | Illinois | 2896016 |
| 3796 | Houston | USA | Texas | 1953631 |
| 3797 | Philadelphia | USA | Pennsylvania | 1517550 |
+------+--------------+-------------+--------------+------------+
5 rows in set (0.00 sec)
mysql>
SQL - Summary
SHOW [ ... ]
SELECT [ColName, ColName, ...] FROM [ TableName, TableName, ... ]WHERE condition [logic condition ]ORDER BY ColName [, ColName, ... ]LIMIT number
SQL - PatternsSQL provides a wildcard in the form of ‘%’. This will match zeroor more characters. A less commonly used wildcard is ‘ ’ whichmatches one (and only one) character (zero characters is not amatch).
mysql> SELECT Name, District FROM City
-> WHERE Name LIKE ’%cate%’;
+---------------------+-----------+
| Name | District |
+---------------------+-----------+
| Zacatecas | Zacatecas |
| Ecatepec de Morelos | Mexico |
+---------------------+-----------+
but
mysql> SELECT Name, District FROM City
-> WHERE Name LIKE ’_cate%’;
+---------------------+-----------+
| Name | District |
+---------------------+-----------+
| Ecatepec de Morelos | Mexico |
+---------------------+-----------+
SQL - Patterns
To match a more general pattern, SQL permits regular expressions.
mysql> SELECT Name, District FROM City
-> WHERE Name REGEXP ’cate’;
+---------------------+-----------+
| Name | District |
+---------------------+-----------+
| Zacatecas | Zacatecas |
| Ecatepec de Morelos | Mexico |
+---------------------+-----------+
mysql> SELECT Name, District FROM City
-> WHERE Name LIKE ’cate’;
Empty set (0.00 sec)
SQL - Patterns
mysql> SELECT Name, District FROM City-> WHERE Name REGEXP ’cate|kate’;
+---------------------+------------+| Name | District |+---------------------+------------+| Zacatecas | Zacatecas || Jekaterinburg | Sverdlovsk || Ecatepec de Morelos | Mexico |+---------------------+------------+3 rows in set (0.00 sec)
SQL - Patterns
mysql> SELECT Name, District FROM City-> WHERE Name REGEXP ’.[c-k]ate’;
+---------------------------+---------------+| Name | District |+---------------------------+---------------+| Zacatecas | Zacatecas || Farrukhabad-cum-Fatehgarh | Uttar Pradesh || Hakodate | Hokkaido || Jekaterinburg | Sverdlovsk || Ecatepec de Morelos | Mexico |+---------------------------+---------------+
SQL - Patterns
mysql> SELECT Name, District FROM City
-> WHERE Name REGEXP ’.[^c-k]ate’;
+---------------------+------------------+
| Name | District |
+---------------------+------------------+
| San Mateo | California |
| Clearwater | Florida |
| Klaten | Central Java |
| San Mateo | Southern Tagalog |
| Berazategui | Buenos Aires |
| Ciudad de Guatemala | Guatemala |
+---------------------+------------------+
6 rows in set (0.01 sec)
SQL - Patterns
mysql> SELECT Name, District FROM City
-> WHERE Name REGEXP ’[^c-k]ate’;
+---------------------+------------------+
| Name | District |
+---------------------+------------------+
| San Mateo | California |
| Clearwater | Florida |
| Klaten | Central Java |
| Waterbury | Connecticut |
| San Mateo | Southern Tagalog |
| Paterson | New Jersey |
| Pate | Taoyuan |
| Berazategui | Buenos Aires |
| Ciudad de Guatemala | Guatemala |
+---------------------+------------------+
9 rows in set (0.00 sec)
SQL - Patternsmysql> SELECT Name, Population FROM City
-> WHERE Population REGEXP ’1{4,6}’;
+----------------+------------+
| Name | Population |
+----------------+------------+
| Sergijev Posad | 111100 |
+----------------+------------+
1 row in set (0.00 sec)
mysql> SELECT Name, Population FROM City
-> WHERE Population REGEXP ’2{4,6}’;
+------------+------------+
| Name | Population |
+------------+------------+
| Rio Grande | 182222 |
| Chalco | 222201 |
| Embu | 222223 |
| Oradea | 222239 |
+------------+------------+
4 rows in set (0.00 sec)
SQL - Patterns
For regular expressions, the statement
{n,m} means at least n to m matches{n,} means at least n{n} means exactely n+ means one or more, {1,}* means zero or more
(each of the above appliesto the previous character)
^ anchor to the line beginning$ anchor to the end of line
SQL - Patterns
Regular expressions can also match the unmatchable ...
simply put a double slash in front of the unmatchable.
For example, ‘\\.’ will match an actual period rather than anycharacter. ‘\\^’ will match a caret while ‘\\t’ will match atab.
SQL - Patterns
You can match patterns within a list of things usingIN (a comma separated list)
or alternativelyNOT IN (a comma separated list)
mysql> SELECT * FROM City WHERE Name IN (’Ottawa’,
-> ’Toronto’, ’Guelph’);
+------+----------+-------------+----------+------------+
| ID | Name | CountryCode | District | Population |
+------+----------+-------------+----------+------------+
| 1850 | Guelph | CAN | Ontario | 103593 |
| 1822 | Ottawa | CAN | Ontario | 335277 |
| 1812 | Toronto | CAN | Ontario | 688275 |
+------+----------+-------------+----------+------------+
3 rows in set (0.00 sec)
mysql> SELECT * FROM City WHERE (Name NOT IN (’Ottawa’,
-> ’Toronto’, ’Guelph’) AND District = ’Ontario’);
SQL - Patterns
You can match patterns within a range usingBETWEEN x AND y
One can also do “full text matching” but that is beyond thescope of this course.
SQL - JOINCombining information from different tables.
mysql> SELECT District, Language FROM City JOIN
-> CountryLanguage USING(CountryCode) LIMIT 10;
+----------+------------+
| District | Language |
+----------+------------+
| Kabol | Balochi |
| Kabol | Dari |
| Kabol | Pashto |
| Kabol | Turkmenian |
| Kabol | Uzbek |
| Qandahar | Balochi |
| Qandahar | Dari |
| Qandahar | Pashto |
| Qandahar | Turkmenian |
| Qandahar | Uzbek |
+----------+------------+
10 rows in set (0.18 sec)
mysql>
SQL - JOIN
This comes from ...
mysql> SELECT District,
-> CountryCode FROM City;
+---------------+-------------+
| District | CountryCode |
+---------------+-------------+
| Kabol | AFG |
| Qandahar | AFG |
| Herat | AFG |
| Balkh | AFG |
| Noord-Holland | NLD |
...............
mysql>
mysql> SELECT Language, CountryCode
-> FROM CountryLanguage;
+------------+-------------+
| Language | CountryCode |
+------------+-------------+
| Dutch | ABW |
| English | ABW |
| Papiamento | ABW |
| Spanish | ABW |
| Balochi | AFG |
...............
mysql>
SQL - JOIN
Joins – Combining data from multiple tables.
The exact way in which the JOIN is done can vary (more on this later).
It works only when two tables share a column with the same name (thereis a different way to combine info from tables without this condition).
With the exception of this column (identified by USING) the columnnames must be unambiguous. To specify names that may not beunambiguous use TABLE NAME . COLUMN NAME.
You can join information from more than one column byUSING(column 1, column 2).
WARNING: if you leave off the USING statement you will not get an error
but nor do you get a JOIN. Instead you get a Cartesian product of the
tables.
SQL - Summary
SHOW [ ... ]SELECT [ColName, ColName, ...] FROM [ TableName, TableName, ... ]
WHERE condition [logic condition ]ORDER BY ColName [, ColName, ... ]LIMIT number
SELECT ColName, ColName, ... FROM TableNameJOIN TableName USING(..)
SQL - one of INSERTs
We can add individual data sets to tables. This is usually donethrough a browser so that the database operator does not do thiswork. But you must know it to program it and you must know theproblems.
mysql> SELECT * FROM City WHERE Name="Dundas";
Empty set (0.00 sec)
mysql> SELECT * FROM City WHERE Name="Grimsby";
+-----+---------+-------------+----------+------------+
| ID | Name | CountryCode | District | Population |
+-----+---------+-------------+----------+------------+
| 534 | Grimsby | GBR | England | 89000 |
+-----+---------+-------------+----------+------------+
1 row in set (0.00 sec)
mysql>
SQL - one of INSERTsSo lets add them to the database.
mysql> DESC City;
+-------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+----------------+
| ID | int(11) | | PRI | NULL | auto_increment |
| Name | char(35) | | | | |
| CountryCode | char(3) | | | | |
| District | char(20) | | | | |
| Population | int(11) | | | 0 | |
+-------------+----------+------+-----+---------+----------------+
5 rows in set (0.06 sec)
mysql> INSERT INTO City VALUES (NULL, "Dundas", "CAN", "Ontario", 21000);
Query OK, 1 row affected (0.50 sec)
mysql> SELECT * FROM City WHERE Name="Dundas";
+------+--------+-------------+----------+------------+
| ID | Name | CountryCode | District | Population |
+------+--------+-------------+----------+------------+
| 4080 | Dundas | CAN | Ontario | 21000 |
+------+--------+-------------+----------+------------+
1 row in set (0.08 sec)
mysql>
SQL - one of INSERTs
To add Grimsby ...
mysql> INSERT INTO City VALUES (NULL, "Grimsby", "CAN", "Ontario", NULL);
ERROR 1048 (23000): Column ’Population’ cannot be null
mysql> INSERT INTO City VALUES (NULL, "Grimsby", "CAN", "Ontario");
ERROR 1136 (21S01): Column count doesn’t match value count at row 1
mysql> INSERT INTO City VALUES (NULL, "Grimsby", "CAN", "Ontario", 0);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM City WHERE Name="Dundas" OR Name="Grimsby";
+------+---------+-------------+----------+------------+
| ID | Name | CountryCode | District | Population |
+------+---------+-------------+----------+------------+
| 534 | Grimsby | GBR | England | 89000 |
| 4080 | Dundas | CAN | Ontario | 21000 |
| 4081 | Grimsby | CAN | Ontario | 0 |
+------+---------+-------------+----------+------------+
3 rows in set (0.00 sec)
mysql>
SQL - one of INSERTs
... but ...
mysql> INSERT INTO City VALUES (NULL, "Ancaster",
-> "CAN", "Ontario", DEFAULT);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM City WHERE ID >= 4080;
+------+----------+-------------+----------+------------+
| ID | Name | CountryCode | District | Population |
+------+----------+-------------+----------+------------+
| 4080 | Dundas | CAN | Ontario | 21000 |
| 4081 | Grimsby | CAN | Ontario | 0 |
| 4082 | Ancaster | CAN | Ontario | 0 |
+------+----------+-------------+----------+------------+
3 rows in set (0.00 sec)
mysql>
SQL - one of INSERTs
These can done all at once.
mysql> INSERT INTO City VALUES
-> (NULL, "Dundas", "CAN", "Ontario", 21000),
-> (NULL, "Grimsby", "CAN", "Ontario", 0);
Query OK, 2 rows affected (0.50 sec)
mysql>
SQL - one of INSERTs
An alternate syntax would be ...
mysql> INSERT INTO City (Name, Population, Region, Country) VALUES
-> (NULL, "Dundas", "CAN", "Ontario", 21000),
-> (NULL, "Grimsby", "CAN", "Ontario", 0);
Query OK, 2 rows affected (0.50 sec)
mysql>
Or if you needed to add only one entry ...
mysql> INSERT INTO City SET Name = "Dundas", Population = 21000,
-> Region ="Ontario", Country = "CAN";
Query OK, 1 row affected (0.50 sec)
mysql>
SQL - Summary
SHOW [ ... ]SELECT [ColName, ColName, ...] FROM [ TableName, TableName, ... ]
WHERE condition [logic condition ]ORDER BY ColName [, ColName, ... ]LIMIT number
SELECT ColName, ColName, ... FROM TableName JOIN TableName USING(..)
INSERT INTO TableName VALUES (.., .., ..), (), ()INSERT INTO TableName (ColName, ColName, ...) VALUES (.., .., ..), (), ()INSERT INTO TableName SET ColName=.., ColName=.., ...
SQL - DELETE
To delete ...
mysql> DELETE FROM City WHERE ID >= 4080;
Query OK, 2 rows affected (0.50 sec)
mysql>
DELETE will remove rows from a table. If thereis no WHERE or LIMIT clause it will happily re-move all rows (but will not delete the table).
SQL - UPDATE
To change just a value of a column ...
mysql> UPDATE City SET Population = "24000" WHERE Name ="Grimsby";
Query OK, 2 rows affected (0.10 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql>
Hmmmm, ... there is something wrong here ... canyou spot it?
SQL - UPDATE
So what about error management?
Well MySQL has NONE ! So rules for both UPDATE and DELETE.
1. Never execute without a WHERE or LIMIT clause.
2. Use the primary key in your WHERE clause.
3. Test your selection with a SELECT statement before execution.
4. Use enforced referential integrity to ensure data required inother tables is not affected.
There is no UNDO button†
† Not strictly true ... transactions can be ROLLBACK’ed to the start of a transaction... more on this later.
SQL - Summary
SHOW [ ... ]SELECT [ColName, ColName, ...] FROM [ TableName, TableName, ... ]
WHERE condition [logic condition ]ORDER BY ColName [, ColName, ... ]LIMIT number
SELECT ColName, ColName, ... FROM TableName JOIN TableName USING(..)INSERT INTO TableName VALUES (.., .., ..), (), ()INSERT INTO TableName (ColName, ColName, ...) VALUES (.., .., ..), (), ()INSERT INTO TableName SET ColName=.., ColName=.., ...
DELETE FROM TableName [WHERE ... | LIMIT .. ]UPDATE TableName SET ColName=.. [WHERE ... | LIMIT .. ]
SQL - Creation
To create a new database ...
mysql> CREATE DATABASE species;
Query OK, 1 row affected (0.08 sec)
mysql> USE species;
Database changed
mysql> CREATE DATABASE species;
ERROR 1007 (HY000): Can’t create database ’species’; database exists
mysql> CREATE DATABASE IF NOT EXISTS species;
Query OK, 0 rows affected (0.00 sec)
mysql>
SQL - CreationTo create a new database ...
mysql> CREATE TABLE sample (
-> sample_id SMALLINT(10) NOT NULL AUTO_INCREMENT,
-> date_collected TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
-> collection_site_id SMALLINT(10) DEFAULT NULL,
-> sample_description TEXT DEFAULT NULL,
-> PRIMARY KEY(sample_id) );
Query OK, 0 rows affected (0.42 sec)
mysql> DESC sample;
+--------------------+--------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+-------------------+----------------+
| sample_id | smallint(10) | | PRI | NULL | auto_increment |
| date_collected | timestamp | YES | | CURRENT_TIMESTAMP | |
| collection_site_id | smallint(10) | YES | | NULL | |
| sample_description | text | YES | | NULL | |
+--------------------+--------------+------+-----+-------------------+----------------+
4 rows in set (0.03 sec)
mysql> INSERT INTO sample VALUE (NULL, NULL, 1, "Some suitably slimmy thingy");
Query OK, 1 row affected (0.17 sec)
mysql> SELECT * FROM sample;
+-----------+---------------------+--------------------+-----------------------------+
| sample_id | date_collected | collection_site_id | sample_description |
+-----------+---------------------+--------------------+-----------------------------+
| 1 | 2009-01-25 14:50:08 | 1 | Some suitably slimmy thingy |
+-----------+---------------------+--------------------+-----------------------------+
1 row in set (0.01 sec)
mysql>
SQL - Creation
To create a new database ...
mysql> SHOW CREATE TABLE sample;
+--------+------------------------------------------------------+
| Table | Create Table |
+--------+------------------------------------------------------+
| sample | CREATE TABLE ‘sample‘ (
‘sample_id‘ smallint(10) NOT NULL auto_increment,
‘date_collected‘ timestamp NOT NULL default CURRENT_TIMESTAMP,
‘collection_site_id‘ smallint(10) default NULL,
‘sample_description‘ text,
PRIMARY KEY (‘sample_id‘)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
+--------+------------------------------------------------------+
1 row in set (0.04 sec)
mysql> SHOW CHARSET;
+----------+-----------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
..................