57
Creating Your First MySQL Database Scott Seighman Sales Consultant Oracle

Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

Embed Size (px)

Citation preview

Page 1: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

Creating Your First MySQL DatabaseScott SeighmanSales ConsultantOracle

Page 2: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

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

Page 3: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

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

Page 4: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

4

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

Page 5: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

5

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

Page 6: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

6

Getting StartedInstallation - Windows

Page 7: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

7

Getting StartedInstallation - Windows

Page 8: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

8

Getting StartedInstallation - Windows

Page 9: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

9

Getting StartedInstallation - Windows

Page 10: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

10

Getting StartedInstallation - Windows

Page 11: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

11

Getting StartedInstallation - Windows

Page 12: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

12

Getting StartedInstallation - Windows

Page 13: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

13

Getting StartedServer Configuration

Page 14: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

14

Getting StartedServer Configuration

Page 15: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

15

Getting StartedServer Configuration

Page 16: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

16

Getting StartedServer Configuration

Page 17: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

17

Getting StartedServer Configuration

Page 18: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

18

Getting StartedServer Configuration

Page 19: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

19

Getting StartedServer Configuration

Page 20: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

20

Getting StartedServer Configuration

Page 21: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

21

Getting StartedServer Configuration

Page 22: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

22

Getting StartedServer Configuration

Page 23: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

23

Getting StartedServer Configuration

Page 24: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

24

Getting StartedServer Configuration

Page 25: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

25

Getting StartedServer Configuration

Page 26: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

26

Starting & Stopping• Windows (+ Admin GUI)

shell> mysqldshell> mysqld –consoleshell> mysqladmin shutdown

• Linux (+ Admin GUI)shell> sudo /etc/init.d/mysql startshell> sudo /etc/init.d/mysql start# /sbin/service mysqld start# /sbin/service mysqld stop# /sbin/service mysqld restart

• Solaris/OpenSolarisshell> /usr/sbin/svcadm enable mysqlshell> /usr/sbin/svcadm disable mysqlshell> /usr/sbin/svcadm restart mysql

Page 27: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

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

Page 28: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

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

Page 29: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

29

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.

Page 30: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

30

Creating Your First Database

Page 31: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

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

Page 32: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

32

Command Basicsmysql> 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

Page 33: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

33

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;

Page 34: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

34

Command Basics

Prompt Meaningmysql> 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 /*

Page 35: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

35

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()-> \cmysql>

Page 36: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

36

Step 1. Creating the Database

Page 37: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

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';

Page 38: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

38

Step 2. Creating Tables

Page 39: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

39

Creating Your First Databasemysql> show tables;Empty set (0.00 sec)

Page 40: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

40

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

Page 41: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

41

Creating Your First Databasemysql> show tables;+-------------------+| Tables_in_mysqlug |+-------------------+| members |+-------------------+1 row in set (0.00 sec)

Page 42: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

42

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

Page 43: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

43

Step 3. Loading Data

Page 44: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

44

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';

Page 45: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

45

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';

Page 46: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

46

Sample Databases$ mysql -u root -pEnter password: Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 39Server 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 changedmysql> 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>

Page 47: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

47

Sample Databasesmysql> show tables;+-----------------+| Tables_in_world |+-----------------+| City | | Country | | CountryLanguage | +-----------------+3 rows in set (0.00 sec)

mysql>

Page 48: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

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

Page 49: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

49

MySQL Workbench 5.2

Page 50: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

50

MySQL Workbench 5.2

Page 51: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

51

Step 4. Retrieving Data

Page 52: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

52

Retrieving Data

• The SELECT statement is used to pull information from a table. The general form of the statement is:

SELECT what_to_selectFROM which_tableWHERE 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.

Page 53: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

53

Retrieving Datamysql> 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

Page 54: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

54

Retrieving Datamysql> 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'

Page 55: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

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>

Page 56: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

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

Page 57: Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda •Installation Review •Accessing the MySQL Server >Command Line >GUI Tools/MySQL Workbench

Thank You!Scott [email protected]