First My SQL DB
First My SQL DB
Agenda
Installation Review Accessing the MySQL Server
> Command Line > GUI Tools/MySQL Workbench 5.2
Getting Started
Download MySQL
> http://mysql.com/downloads
Installation
> Screen shots
Solaris/OpenSolaris
> pkg, tar
MySQL Distributions
Advantages of binary distributions
> Good selecting config options > High quality commercial compilers > Provided with extensive libraries
release > Get latest bug fixes without waiting for next release
Getting Started
Installation - Windows
Getting Started
Installation - Windows
Getting Started
Installation - Windows
Getting Started
Installation - Windows
Getting Started
Installation - Windows
10
Getting Started
Installation - Windows
11
Getting Started
Installation - Windows
12
Getting Started
Server Configuration
13
Getting Started
Server Configuration
14
Getting Started
Server Configuration
15
Getting Started
Server Configuration
16
Getting Started
Server Configuration
17
Getting Started
Server Configuration
18
Getting Started
Server Configuration
19
Getting Started
Server Configuration
20
Getting Started
Server Configuration
21
Getting Started
Server Configuration
22
Getting Started
Server Configuration
23
Getting Started
Server Configuration
24
Getting Started
Server Configuration
25
Solaris/OpenSolaris
shell> /usr/sbin/svcadm enable mysql shell> /usr/sbin/svcadm disable mysql shell> /usr/sbin/svcadm restart mysql
26
Config File
Windows
> my.ini > C:\Program Files\MySQL\MySQL Server 5.1
Linux/Unix
> my.cnf > /etc/mysql/my.cnf
Several Templates
> Small, Medium, Large, Huge, InnoDB-Heavy
27
Config File
Windows
> my.ini > C:\Program Files\MySQL\MySQL Server 5.1
Linux/Unix
> my.cnf > /etc/mysql/my.cnf
Several Templates
> Small, Medium, Large, Huge, InnoDB-Heavy
28
Config File
Several Templates
my-small.cnf
> This is for a system with little memory (<= 64M) where MySQL is only used
from time to time and it's important that the mysqld daemon doesn't use much resources.
my-medium.cnf
> This is for a system with little memory (32M - 64M) where MySQL plays an
important part, or systems up to 128M where MySQL is used together with other programs (such as a web server)
my-large.cnf
> This is for a large system with memory = 512M where the system runs mainly
MySQL.
my-huge.cnf
> This is for a large system with memory of 1G-2G where the system runs
mainly MySQL.
29
30
Command Basics Create a database Create a table Load data into the table Retrieve data from the table in various ways
31
Command Basics
mysql> select version(); +------------------+ | version() | +------------------+ | 5.1.46-community | +------------------+ 1 row in set (0.00 sec)
A command normally consists of an SQL statement followed by a semicolon When you issue a command, mysql sends it to the server for execution and displays the results, then prints another mysql> prompt to indicate that it is ready for another command
32
Command Basics
Keywords may be entered in any lettercase A command need not be given all on a single line, so lengthy commands that require several lines are not a problem MySQL determines where your statement ends by looking for the terminating semicolon, not by looking for the end of the input line.
mysql> select -> user() -> , -> current_date;
33
Command Basics
Meaning Ready for new command Waiting for next line of multiple-line command Waiting for next line, waiting for completion of a string that began with a single quote (') Waiting for next line, waiting for completion of a string that began with a double quote (") Waiting for next line, waiting for completion of an identifier that began with a backtick (`) Waiting for next line, waiting for completion of a comment that began with /*
34
Command Basics
If you decide you do not want to execute a command that you are in the process of entering, cancel it by typing \c mysql> SELECT -> USER() -> \c mysql>
35
36
38
39
40
41
| varchar(40) | YES
43
Loading Data
You could create a text file members.txt containing one record per line, with values separated by tabs, and given in the order in which the columns were listed in the CREATE TABLE statement (use \N for NULL fields):
lastname firstname address city state phone email
If you created the file on Windows with an editor that uses \r\n as a line terminator, you should use this statement instead:
mysql> LOAD DATA LOCAL INFILE 'members.txt' INTO TABLE pet LINES TERMINATED BY '\r\n';
44
Loading Data
You could export a .csv file from an Excel spreadsheet containing one record per line, with values separated by commas, and given in the order in which the columns were listed in the CREATE TABLE statement
mysql> LOAD DATA LOCAL INFILE 'members.csv' INTO TABLE members FIELDS TERMINATED BY ',';
If you created the file on Windows with an editor that uses \r\n as a line terminator, you should use this statement instead:
mysql> LOAD DATA LOCAL INFILE 'members.csv' INTO TABLE members FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n';
45
Sample Databases
$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Your MySQL connection id is 39 Server version: 5.1.31-1ubuntu2 (Ubuntu) Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> create database world; mysql> use world; Database changed mysql> source /home/Documents/world.sql; Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) ... Query OK, 0 rows affected (0.00 sec) mysql>
46
Sample Databases
mysql> show tables; +-----------------+ | Tables_in_world | +-----------------+ | City | Country | |
47
Loading Data
You could use a GUI tool
> > > > > > > > > > > > >
Workbench 5.2 Toad for MySQL Navicat Sequel Pro HeidiSQL phpMyAdmin SQL Maestro MySQL Tools Family SQL Wave dbForge Studio DBTools Manager MyDB Studio SQLYog More ...
48
49
50
51
Retrieving Data
The SELECT statement is used to pull information from a table. The general form of the statement is:
SELECT what_to_select FROM which_table WHERE conditions_to_satisfy;
which_table indicates the table from which you want to retrieve data
The WHERE clause is optional. If it is present, conditions_to_satisfy specifies one or more conditions that rows must satisfy to qualify for retrieval.
52
Retrieving Data
mysql> SELECT * FROM members; Find all data in the table mysql> SELECT COUNT(*) FROM members; Count all rows in the table mysql> SELECT name, email FROM members; Find names and emails mysql> SELECT city, COUNT(*) FROM members GROUP BY city; Display the count of cities
53
Retrieving Data
mysql> SELECT lastname, firstname FROM members ORDER BY firstname; Find first names and last names, sort by first names mysql> SELECT * FROM members WHERE name LIKE 'b%'; Find names beginning with the letter 'b' mysql> SELECT * FROM members WHERE name LIKE '%fy'; Find names ending with the letters 'fy' mysql> SELECT * FROM members WHERE name LIKE '%w%'; Find names containing the letter 'w'
54
Retrieving Data
mysql> SELECT * FROM members WHERE city = 'Avon Lake';
+----------+-------------+------------------------+-----------+-------+-------+--------------+------------------+ | Lastname | Firstname | Address | City | State | Zip | Phone | Email |
+----------+-------------+------------------------+-----------+-------+-------+--------------+------------------+ | Gibbs | Lengen | Art | 32573 Captain's Galley | Avon Lake | OH | Avon Lake | OH | 44012 | 440-552-5470 | [email protected] | | 44012 | 440-933-5488 | |
+----------+-------------+------------------------+-----------+-------+-------+--------------+------------------+
55
Additional Resources
MySQL Tutorial
> http://dev.mysql.com/doc/refman/5.1/en/tutorial.html
MySQL Newsletter
> Register at http://mysql.com
MySQL Magazine
> http://paragon-cs.com/mag
Thank You!
Scott Seighman [email protected]