CPSC 421: Database Management Systems: Mysql Basics
CPSC 421: Database Management Systems: Mysql Basics
MySQL Basics
Getting Started
The server
The MySQL Shell
Creating Tables
Loading data
Querying Data
Prepared Statements & Views
Getting Started
MySQL Website
http://www.mysql.com
Download
http://dev.mysql.com
The community server version (free)
I installed version 5.1 (other version avail.)
Almost all platforms supported (OS X, Windows, Linux, )
Binary installers available
Should read installation instructions (if installing on your own)
Documentation
http://dev.mysql.com/doc
Should read about setting up accounts, passwords, etc. in manual
Also, section on database administration is useful
2
1
For this course
About MySQL
Features
Open-source (GPL)
A Real DBMS (e.g., disk storage depending on storage engine)
Supports much of SQL (99) but not all
Many advanced features, e.g., stored procedures, triggers, views,
Different storage engines, per table
This can be a bit confusing because of different choices
MySQL allows new engines to be created (Pluggable API)
Caching
Replication
Transactions (InnoDB, others)
2
Starting and Stopping the Server
Typical installation
Start the server (see installation instructions)
Run mysql command (e.g., /usr/local/mysql/bin/mysql)
Create accounts, set up passwords and priviledges
3
Creating a Database
As root (depending on permissions)
mysql> CREATE DATABASE <dbname>;
Creating Tables
Can issue SQL statements via the monitor, including create table
mysql> USE mydb
mysql> CREATE TABLE ingredients (id INT, name VARCHAR(30));
4
Creating Tables
Creating Tables
MySQL Query Browser exposes more options, e.g., allowing the
storage engine of a table, and much more
InnoDB is (currently) most feature rich (transactions, indices, etc.)
MyISAM is the other major engine
10
5
Loading Data
Again, can issue insert command:
mysql> INSERT INTO ingredients VALUES (1, jalapeno);
mysql> INSERT INTO ingredients VALUES (2, habanero);
The data file should be tab-delimitted with each record on a newline. Nulls values are
denoted by \N, e.g.:
1 jalapeno
2 habanero
3 tomatillo
4 \N
Querying Data
Can issue queries directly in monitor:
mysql> SELECT * FROM ingredients;
+------+---------------+
| id | name |
+------+---------------+
| 1 | 'jalapeno' |
| 2 | 'habanero' |
| 3 | 'tomatillo' |
| 4 | 'bell pepper' |
| 5 | 'onion' |
| 6 | 'cayenne' |
| 7 | NULL |
+------+---------------+
6
Explaining Queries
13
Prepared Statements
Allow you to assign a name to an SQL query, which typically
contains parameters
Stored on the server and executed by passing parameter values
Here is a simple example:
mysql> PREPARE get_ingredient_name FROM
'select name from ingredients where id=?';
mysql> SET @id = 3;
mysql> EXECUTE get_ingredient_name USING @id;
+-------------+
| name |
+-------------+
| 'tomatillo' |
+-------------+
1 row in set (0.00 sec)
mysql> DEALLOCATE PREPARE get_ingredient_name
7
Views
Views provide a way to name a query, and then use that name as
a relation in another query
15
etc.
8
Lots more
MySQL has many features we did not discuss
the MySQL documentation is a great resource
some things you might want to look at are:
Transactions
Stored Procedures
How to access MySQL via JDBC
Crash recovery
Storage engines
How to backup your database
Limitations of SQL support, and workarounds (e.g., minus)
Spatial Extensions
Functions and operators
Data types
PHP support
other tools supporting MySQL, e.g., phpMyAdmin (web-based
database administration)
17