Beginning MySQL
An introduction to the world's most popular open
source database!
Created by MySQL AB to empower the Created: November 21 2007
community to present on MySQL, the Last Updated: January 8 2008
world's best open source database!
License
● The slides are licensed under a Creative
Commons, by Attribution, Share Alike license.
Read more at:
– http://creativecommons.org/licenses/by-sa/3.0/
● You are encouraged to make changes, but
please keep in mind that you cannot make use
of MySQL trademarks, like certain logos. Read
more at:
– http://www.mysql.com/company/legal/trademark.html
Agenda
● What is MySQL?
● Using the MySQL monitor
● Loading a database
● Learning to use SQL
– SELECT, INSERT, UPDATE, DELETE
● Further resources
What is MySQL?
● A relational database management system
(RDBMS)
● Open source and been around since 1995
● The “M” in LAMP stack
● Popular:
– ease of installation and use
– standards compliant
– vibrant community
– many connectors for various languages
Starting to use the MySQL
monitor
● Post-installation, set the MySQL root password
● Start the monitor by typing mysql -uroot -p
and entering the password when asked
● Try some commands that MySQL understands:
SELECT VERSION();
SHOW DATABASES;
HELP
● All SQL statements are terminated with a semi-
colon (“;”)
On style
● MySQL treats SQL statements such that its
case-insensitive
– SELECT or select do the same thing
● However, beware table/database names on
certain platforms though
SELECT host FROM mysql.user;
● Uppercase: SQL statements and keywords
– SELECT, FROM, USE, etc.
● Lowercase: database, table, column names
Released under a Creative Commons By-Attribution-ShareAlike License by MySQL AB. 6
Read more at: http://creativecommons.org/licenses/by-sa/3.0/
Loading a sample database
● Use the Sakila sample database for examples
– http://dev.mysql.com/doc/
● SOURCE sakila-schema.sql
– Now run a SHOW DATABASES; to see it
● USE sakila;
● SHOW TABLES;
● DESCRIBE actor;
Released under a Creative Commons By-Attribution-ShareAlike License by MySQL AB. 7
Read more at: http://creativecommons.org/licenses/by-sa/3.0/
Introducing SELECT
● Load the sample data
SOURCE sakila-data.sql
● SELECT * FROM actor;
● Choose some columns:
– SELECT first_name, last_name FROM
actor;
– SELECT first_name FROM sakila.actor;
● Using WHERE
– SELECT * FROM actor WHERE
last_name=”Willis”;
Released under a Creative Commons By-Attribution-ShareAlike License by MySQL AB. 8
Read more at: http://creativecommons.org/licenses/by-sa/3.0/
Continuing with SELECT
● Begin with a prefix, contains a string, ends with
a suffix – use %
– SELECT * FROM actor WHERE last_name
LIKE “%will%”;
● Using Boolean expressions (AND, OR, NOT)
– SELECT title FROM film WHERE title >
“a” and title < “c”; - returns A...B (i.e.
Before C)
● SELECT ... ORDER BY
● SELECT ... LIMIT
Released under a Creative Commons By-Attribution-ShareAlike License by MySQL AB. 9
Read more at: http://creativecommons.org/licenses/by-sa/3.0/
Putting data in: INSERT
● First, know the columns
– DESCRIBE actor;
● Next, know what ID (primary key) to provide
– SELECT MAX(actor_id) FROM actor;
● Now, insert data based on column listing
– INSERT INTO actor VALUES (201, “COLIN”,
“CHARLES”, CURRENT_TIMESTAMP);
● Is it there?
– SELECT * FROM actor WHERE
actor_id='201';
Released under a Creative Commons By-Attribution-ShareAlike License by MySQL AB. 10
Read more at: http://creativecommons.org/licenses/by-sa/3.0/
Getting rid of data: DELETE
● Used to delete one or more rows
● Deletes the recently added record
– DELETE FROM actor WHERE last_update >
“2007-01-01”;
● Trying to get rid of a table?
– You can do:
DELETE FROM tablename;
– Its better to do:
DROP tablename;
Released under a Creative Commons By-Attribution-ShareAlike License by MySQL AB. 11
Read more at: http://creativecommons.org/licenses/by-sa/3.0/
Updating records: UPDATE
● UPDATE is used to make changes to data
● An example changing all first_name, last_name to
uppercase:
UPDATE actor SET
first_name=UPPER(first_name),
last_name=UPPER(last_name);
Query OK, 1 row affected (0.04 sec)
Rows matched: 201 Changed: 1 Warnings: 0
Released under a Creative Commons By-Attribution-ShareAlike License by MySQL AB. 12
Read more at: http://creativecommons.org/licenses/by-sa/3.0/
What have we learnt, so far?
● Currently, you now know how to:
– Create (INSERT)
– Read (SELECT)
– Update (UPDATE)
– Delete (DELETE)
● Use basic SQL
Released under a Creative Commons By-Attribution-ShareAlike License by MySQL AB. 13
Read more at: http://creativecommons.org/licenses/by-sa/3.0/
Resources
● Planet MySQL
– http://www.planetmysql.org/
● MySQL Forge & MySQL Forge Wiki
– http://forge.mysql.com/
– http://forge.mysql.com/wiki/
● MySQL DevZone
– http://dev.mysql.com/
Released under a Creative Commons By-Attribution-ShareAlike License by MySQL AB. 14
Read more at: http://creativecommons.org/licenses/by-sa/3.0/
Contributors
● Colin Charles
(MySQL)
● Giuseppe Maxia
(MySQL)
Released under a Creative Commons By-Attribution-ShareAlike License by MySQL AB. 15
Read more at: http://creativecommons.org/licenses/by-sa/3.0/
Questions? Thanks!
● A copy of these slides and speaker notes are
available at http://forge.mysql.com/wiki/
Speaker Name
Speaker Email Address
Speaker URL
Released under a Creative Commons By-Attribution-ShareAlike License by MySQL AB. 16
Read more at: http://creativecommons.org/licenses/by-sa/3.0/