2. Agenda. • Installation Review. • Accessing the MySQL Server. > Command
Line. > GUI Tools/MySQL Workbench 5.2. • Creating Your First Database.
Creating Your First MySQL Database Scott Seighman Sales Consultant Oracle
Agenda • Installation Review • Accessing the MySQL Server > Command Line > GUI Tools/MySQL Workbench 5.2
• Creating Your First Database > Adding/Importing Data > Basic Commands/Queries
2
Getting Started • Download MySQL > http://mysql.com/downloads
• Extensive OS Support • Documentation > http://dev.mysql.com/doc/
• Installation > Screen shots …
• Basic Administration Tools > MySQL Administrator, MySQL Query Browser EOL > Workbench 5.2 3
MySQL Binary Distributions • Available for several operating systems • Precompiled binaries • Windows > Essentials, complete with Configuration Wizard > No-install
• Linux (Red Hat, SuSE, Ubuntu, etc) > RPM files, tar files, non-RPM
• Solaris/OpenSolaris > pkg, tar
• See website for more specific packages 4
MySQL Distributions • Advantages of binary distributions > Good selecting config options > High quality commercial compilers > Provided with extensive libraries
• Advantages with source distributions > May not have a binary for your OS > Can enable features not included with precompiled > Can compile latest source code without waiting for binary
release > Get latest bug fixes without waiting for next release
• Use MySQL Reference Manual for more information 5
Getting Started Installation - Windows
6
Getting Started Installation - Windows
7
Getting Started Installation - Windows
8
Getting Started Installation - Windows
9
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
Starting & Stopping • Windows (+ Admin GUI) shell> mysqld shell> mysqld –console shell> mysqladmin shutdown
• Linux (+ Admin GUI) shell> sudo /etc/init.d/mysql start shell> sudo /etc/init.d/mysql start # /sbin/service mysqld start # /sbin/service mysqld stop # /sbin/service mysqld restart
• 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 ( 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
Creating Your First Database
30
Creating Your First Database • Step by Step > > > > >
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
Prompt
Meaning
mysql>
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
Step 1. Creating the Database
36
Creating Your First Database mysql> create database mysqlug; Query OK, 1 row affected (0.04 sec) mysql> show databases; +--------------------+ | Database
|
+--------------------+ | mysqlug
|
+--------------------+ 1 rows in set (0.01 sec) mysql> use mysqlug; Database changed mysql> GRANT ALL ON mysqlug.* TO 'seighman'@'localhost'; 37
Step 2. Creating Tables
38
Creating Your First Database mysql> show tables; Empty set (0.00 sec)
39
Creating Your First Database mysql> create table members -> (lastname varchar(20), -> firstname varchar(20), -> address varchar(30), -> city varchar(20), -> state varchar(2), -> zip int(5), -> phone int(12), -> email varchar (40)); Query OK, 0 rows affected (0.17 sec) mysql>
40
Creating Your First Database mysql> show tables; +-------------------+ | Tables_in_mysqlug | +-------------------+ | members
|
+-------------------+ 1 row in set (0.00 sec)
41
Creating Your First Database mysql> describe members; +-----------+-------------+------+-----+---------+-------+ | Field
| Type
| Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+ | lastname
| varchar(20) | YES
|
| NULL
|
|
| firstname | varchar(20) | YES
|
| NULL
|
|
| address
| varchar(30) | YES
|
| NULL
|
|
| city
| varchar(20) | YES
|
| NULL
|
|
| state
| varchar(2)
| YES
|
| NULL
|
|
| zip
| int(5)
| YES
|
| NULL
|
|
| phone
| int(12)
| YES
|
| NULL
|
|
| email
| varchar(40) | YES
|
| NULL
|
|
+-----------+-------------+------+-----+---------+-------+ 8 rows in set (0.04 sec) mysql>
42
Step 3. Loading Data
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
mysql> LOAD DATA LOCAL INFILE 'members.txt' INTO TABLE members;
• 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.
Commands end with ; or \g.
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
|
| CountryLanguage | +-----------------+ 3 rows in set (0.00 sec) mysql>
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
MySQL Workbench 5.2
49
MySQL Workbench 5.2
50
Step 4. Retrieving Data
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;
• what_to_select indicates what you want to see > List of columns, or * to indicate “all columns.”
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
| Art
| 32573 Captain's Galley | Avon Lake | OH
| Lengen
| John & Lisa | 718 Sawmill Drive
| Avon Lake | OH
| 44012 | 440-552-5470 |
[email protected] | | 44012 | 440-933-5488 |
|
+----------+-------------+------------------------+-----------+-------+-------+--------------+------------------+
2 rows in set (0.00 sec) mysql>
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
• MySQL Performance Blog > www.mysqlperformanceblog.com
• Planet MySQL Blogs > www.planetmysql.org 56
Thank You! Scott Seighman
[email protected]