Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda...

Post on 11-Mar-2018

225 views 6 download

Transcript of Creating Your First MySQL Database - Meetupfiles.meetup.com/1179656/FirstMySQLDB.pdf2 Agenda...

Creating Your First MySQL DatabaseScott SeighmanSales ConsultantOracle

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

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

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

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

6

Getting StartedInstallation - Windows

7

Getting StartedInstallation - Windows

8

Getting StartedInstallation - Windows

9

Getting StartedInstallation - Windows

10

Getting StartedInstallation - Windows

11

Getting StartedInstallation - Windows

12

Getting StartedInstallation - Windows

13

Getting StartedServer Configuration

14

Getting StartedServer Configuration

15

Getting StartedServer Configuration

16

Getting StartedServer Configuration

17

Getting StartedServer Configuration

18

Getting StartedServer Configuration

19

Getting StartedServer Configuration

20

Getting StartedServer Configuration

21

Getting StartedServer Configuration

22

Getting StartedServer Configuration

23

Getting StartedServer Configuration

24

Getting StartedServer Configuration

25

Getting StartedServer Configuration

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

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

• 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

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.

30

Creating Your First Database

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

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

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;

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

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>

36

Step 1. Creating the Database

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

38

Step 2. Creating Tables

39

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

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>

41

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

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>

43

Step 3. Loading Data

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

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

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>

47

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

mysql>

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

49

MySQL Workbench 5.2

50

MySQL Workbench 5.2

51

Step 4. Retrieving Data

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.

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

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'

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 | agibbs@oh.rr.com |

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

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

2 rows in set (0.00 sec)

mysql>

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

Thank You!Scott Seighmanscott.seighman@oracle.com