First My SQL DB

download First My SQL DB

of 57

Transcript of First My SQL DB

  • 7/29/2019 First My SQL DB

    1/57

    Creating YourFirst MySQLDatabase

    Scott SeighmanSales ConsultantOracle

  • 7/29/2019 First My SQL DB

    2/57

    2

    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

  • 7/29/2019 First My SQL DB

    3/57

    3

    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

    http://mysql.com/downloadshttp://dev.mysql.com/doc/http://dev.mysql.com/doc/http://mysql.com/downloads
  • 7/29/2019 First My SQL DB

    4/574

    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

  • 7/29/2019 First My SQL DB

    5/575

    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 binaryrelease

    > Get latest bug fixes without waiting for next release

    Use MySQL Reference Manual for more information

  • 7/29/2019 First My SQL DB

    6/576

    Getting StartedInstallation - Windows

  • 7/29/2019 First My SQL DB

    7/577

    Getting StartedInstallation - Windows

  • 7/29/2019 First My SQL DB

    8/578

    Getting StartedInstallation - Windows

  • 7/29/2019 First My SQL DB

    9/579

    Getting StartedInstallation - Windows

  • 7/29/2019 First My SQL DB

    10/57

    10

    Getting StartedInstallation - Windows

  • 7/29/2019 First My SQL DB

    11/57

    11

    Getting StartedInstallation - Windows

  • 7/29/2019 First My SQL DB

    12/57

    12

    Getting StartedInstallation - Windows

  • 7/29/2019 First My SQL DB

    13/57

    13

    Getting StartedServer Configuration

  • 7/29/2019 First My SQL DB

    14/57

    14

    Getting StartedServer Configuration

  • 7/29/2019 First My SQL DB

    15/57

    15

    Getting StartedServer Configuration

  • 7/29/2019 First My SQL DB

    16/57

    16

    Getting StartedServer Configuration

  • 7/29/2019 First My SQL DB

    17/57

    17

    Getting StartedServer Configuration

  • 7/29/2019 First My SQL DB

    18/57

    18

    Getting StartedServer Configuration

  • 7/29/2019 First My SQL DB

    19/57

    19

    Getting StartedServer Configuration

  • 7/29/2019 First My SQL DB

    20/57

    20

    Getting StartedServer Configuration

  • 7/29/2019 First My SQL DB

    21/57

    21

    Getting StartedServer Configuration

  • 7/29/2019 First My SQL DB

    22/57

    22

    Getting StartedServer Configuration

  • 7/29/2019 First My SQL DB

    23/57

    23

    Getting StartedServer Configuration

  • 7/29/2019 First My SQL DB

    24/57

    24

    Getting StartedServer Configuration

  • 7/29/2019 First My SQL DB

    25/57

    25

    Getting StartedServer Configuration

  • 7/29/2019 First My SQL DB

    26/57

    26

    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/OpenSolarisshell> /usr/sbin/svcadm enable mysql

    shell> /usr/sbin/svcadm disable mysql

    shell> /usr/sbin/svcadm restart mysql

  • 7/29/2019 First My SQL DB

    27/57

    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

  • 7/29/2019 First My SQL DB

    28/57

    28

    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

  • 7/29/2019 First My SQL DB

    29/57

    29

    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 withother programs (such as a web server)

    my-large.cnf

    > This is for a large system with memory = 512M where the system runs mainlyMySQL.

    my-huge.cnf> This is for a large system with memory of 1G-2G where the system runs

    mainly MySQL.

  • 7/29/2019 First My SQL DB

    30/57

    30

    Creating Your First Database

  • 7/29/2019 First My SQL DB

    31/57

    31

    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

  • 7/29/2019 First My SQL DB

    32/57

    32

    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 serverfor execution and displays the results, then prints another

    mysql>prompt to indicate that it is ready for anothercommand

  • 7/29/2019 First My SQL DB

    33/57

    33

    Command Basics

    Keywords may be entered in any lettercase

    A command need not be given all on a single line, solengthy commands that require several lines are not aproblem

    MySQLdetermines where your statement ends bylooking for the terminating semicolon, not by lookingfor the end of the input line.

    mysql> select

    -> user()

    -> ,

    -> current_date;

  • 7/29/2019 First My SQL DB

    34/57

    34

    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 singlequote (')

    > Waiting for next line, waiting for completion of a string that began with adouble quote (")

    `> Waiting for next line, waiting for completion of an identifier that began with abacktick (`)

    /*> Waiting for next line, waiting for completion of a comment that began with /*

  • 7/29/2019 First My SQL DB

    35/57

    35

    Command Basics

    If you decide you do not want to execute a command thatyou are in the process of entering, cancel it by typing \c

    mysql> SELECT

    -> USER()

    -> \c

    mysql>

  • 7/29/2019 First My SQL DB

    36/57

    36

    Step 1. Creating the Database

  • 7/29/2019 First My SQL DB

    37/57

    37

    Creating Your First Databasemysql> 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';

  • 7/29/2019 First My SQL DB

    38/57

    38

    Step 2. Creating Tables

  • 7/29/2019 First My SQL DB

    39/57

    39

    Creating Your First Database

    mysql> show tables;Empty set (0.00 sec)

  • 7/29/2019 First My SQL DB

    40/57

    40

    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>

  • 7/29/2019 First My SQL DB

    41/57

    41

    Creating Your First Database

    mysql> show tables;

    +-------------------+

    | Tables_in_mysqlug |

    +-------------------+

    | members |+-------------------+

    1 row in set (0.00 sec)

  • 7/29/2019 First My SQL DB

    42/57

    42

    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>

  • 7/29/2019 First My SQL DB

    43/57

    43

    Step 3. Loading Data

  • 7/29/2019 First My SQL DB

    44/57

    44

    Loading Data

    You could create a text file members.txt containing onerecord per line, with values separated by tabs, and givenin the order in which the columns were listed in theCREATE TABLE statement (use \N for NULL fields):lastname firstname address city state phone email

    mysql> LOAD DATA LOCAL INFILE 'members.txt' INTO TABLEmembers;

    If you created the file on Windows with an editor that

    uses \r\n as a line terminator, you should use thisstatement instead:

    mysql> LOAD DATA LOCAL INFILE 'members.txt' INTO TABLEpet LINES TERMINATED BY '\r\n';

  • 7/29/2019 First My SQL DB

    45/57

    45

    Loading Data

    You could export a .csv file from an Excel spreadsheetcontaining one record per line, with values separated bycommas, and given in the order in which the columnswere listed in the CREATE TABLE statement

    mysql> LOAD DATA LOCAL INFILE 'members.csv' INTO TABLEmembers FIELDS TERMINATED BY ',';

    If you created the file on Windows with an editor thatuses \r\n as a line terminator, you should use this

    statement instead:

    mysql> LOAD DATA LOCAL INFILE 'members.csv' INTO TABLEmembers FIELDS TERMINATED BY ',' LINES TERMINATED BY'\r\n';

  • 7/29/2019 First My SQL DB

    46/57

    46

    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>

  • 7/29/2019 First My SQL DB

    47/57

    47

    Sample Databasesmysql> show tables;

    +-----------------+

    | Tables_in_world |

    +-----------------+

    | City |

    | Country |

    | CountryLanguage |

    +-----------------+

    3 rows in set (0.00 sec)

    mysql>

  • 7/29/2019 First My SQL DB

    48/57

    48

    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 ...

  • 7/29/2019 First My SQL DB

    49/57

    49

    MySQL Workbench 5.2

    M SQ W kb h 2

  • 7/29/2019 First My SQL DB

    50/57

    50

    MySQL Workbench 5.2

  • 7/29/2019 First My SQL DB

    51/57

    51

    Step 4. Retrieving Data

    R t i i D t

  • 7/29/2019 First My SQL DB

    52/57

    52

    Retrieving Data

    The SELECT statement is used to pull informationfrom a table. The general form of the statement is:

    SELECT what_to_select

    FROMwhich_table

    WHERE conditions_to_satisfy;

    what_to_selectindicates what you want to see> List of columns, or* to indicate all columns.

    which_table indicates the table from which you want toretrieve data

    The WHERE clause is optional. If it is present,conditions_to_satisfyspecifies one or more conditionsthat rows must satisfy to qualify for retrieval.

    R t i i D t

  • 7/29/2019 First My SQL DB

    53/57

    53

    Retrieving Data

    mysql> SELECT * FROMmembers;

    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

    R t i i D t

  • 7/29/2019 First My SQL DB

    54/57

    54

    Retrieving Data

    mysql> SELECT lastname, firstname FROM members ORDER BYfirstname;

    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'

    Retrieving Data

  • 7/29/2019 First My SQL DB

    55/57

    55

    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 | 44012 | 440-552-5470 | [email protected] |

    | Lengen | John & Lisa | 718 Sawmill Drive | Avon Lake | OH | 44012 | 440-933-5488 | |

    +----------+-------------+------------------------+-----------+-------+-------+--------------+------------------+

    2 rows in set (0.00 sec)

    mysql>

    Additional Resources

  • 7/29/2019 First My SQL DB

    56/57

    56

    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

    http://www.mysqlperformanceblog.com/http://www.mysqlperformanceblog.com/
  • 7/29/2019 First My SQL DB

    57/57

    Thank You!

    Scott Seighman

    [email protected]