Top Banner
1 MySQL 102 MySQL 102 MySQL 102 is the second part of a presentation to be preceded by MySQL 101 and covers material that will build on the previous presentation. Http://slideshare.net/davestokes/presentations
37
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: My sql102

1

MySQL 102MySQL 102

MySQL 102 is the second part of a presentation to be preceded by MySQL 101

and covers material that will build on the previous presentation.

Http://slideshare.net/davestokes/presentations

Page 2: My sql102

2

AgendaAgenda➔ Workbench➔ User Accounts & Privileges➔ Tables and Databases➔ Views➔ Stored Routines/Procedures➔ Questions and Answers➔ Replication (after a break)

[email protected] @stoker

Page 3: My sql102

3

WorkbenchWorkbench FREE http://dev.mysql.com/downloads/ Three tools in one

Query Tool Admin Tool Entity Relationship Mapper

Scripts available for setting up replication, fail over and more

http://dev.mysql.com/doc/refman/5.5/en/getting-mysql.html

Page 4: My sql102

4

Did I mention free?

http://dev.mysql.com/downloads/workbench/

Page 5: My sql102

5

Admin

Page 6: My sql102

6

User Accounts

Page 7: My sql102

7

Users and Hosts

Page 8: My sql102

8

Know whomcan do what

We have four accounts Anonymous (%) ODBC joe root

ODBC and joe can login from any host while root and anonymous are local host only

Page 9: My sql102

9

Joe's InfoJoe's Info

htallation.html

'joe' can connect to the mysqld server from any host. Note he does not have a password which can be a serious security concern.

Page 10: My sql102

10

Joe's Priviledges

Page 11: My sql102

11

Joe's Limits

Account Limits can be used to throttle user access.

Page 12: My sql102

12

Schema Privileges

Page 13: My sql102

13

Easier than ...GRANT INSERT, UPDATE, SELECTON world.*To 'joe'@'%';FLUSH PRIVILEGES;

Page 14: My sql102

14

Tables & DatabasesTables & Databases

Databases contain tables, which are used for storing data. Databases also contain objects such as stored routines or triggers. – MySQl 5.0 Certification Guide

httphtml

Page 15: My sql102

15

1. CREATE DATABASE foo2. (magic)

3. mkdir foo

CREATE DATBASE will create a directory to house .frm files

InnoDB tables will have table spaces for data & indexes

MyISQM can also have .MYD and .MYI files for data and indexes

Page 16: My sql102

16

Databases db.opt will hold info on

character sets, collations.

Databases can hold zero or more tables plus objects like view, triggers

Databases can not be nested

Page 17: My sql102

17

CREATE a DATABASE, aka SCHEMA

Use Workbench to create a schema

Page 18: My sql102

18

Create 'demo'

Here we create a schema named 'demo' with UTF8 character set and the 'general' 'case insensitive' collation.

Page 19: My sql102

19

Review – Workbench will show

the statement used to create the schema.

Or you could type in the SQL by hand.

Page 20: My sql102

20

We have 'demo' Double click on demo

to expand the list of contents

Page 21: My sql102

21

CREATE a table

Now we can create a table in the 'demo' schema

Page 22: My sql102

22

Creating a Table

Create a table named stuff with a column named member_id that is NOT NULL, AUTOINCREMENT, UNSIGNED and a PRIMARY KEY

Page 23: My sql102

23

Review the SQLWe can see the SQL that is about to be executed to create the table.

Page 24: My sql102

24

Double Check Any problems will be

show here as well as success

Page 25: My sql102

25

SHOW CREATE TABLE

Note that INT(10) means 10 characters will be shown for output. INT(2) will show two places of data by default but still holds upto 2147483647 signed or 4294967295 unsigned.

Page 26: My sql102

26

After adding two more columns

For the sake of brevity, two more columns have been added for name and email, both are stup to hold CHAR data

Page 27: My sql102

27

Add some data

Note we do not have member_id mentioned.

Page 28: My sql102

28

Select data from stuff

Here we select data from the table. Note that member_id was automatically filled in for this record.

Page 29: My sql102

29

Add a few more records

Here a few more records have been added and the member_id has again has been incremented and inserted.

Page 30: My sql102

30

Triggers Triggers are 'fired' in response to a an SQL

statement. They can happen before OR after an INSERT,

UPDATE, or DELETE event. They can be used to check values, sum data, etc. You may not want 'logic' in your database and

prefer to have it in the application level.

Page 31: My sql102

31

Boss: We need to track previous email addresses

Lets track previous email address by placing them in a new column creatively called oldemail.

Page 32: My sql102

32

Trigger in Action

Page 33: My sql102

33

Did you catch the mistake?

The previous trigger had a serious logical flaw. Take a look at it and see if you can

spot it.

Hint: Is the email the only thing that will ever get updated?

Page 34: My sql102

34

VIEWS Views are stored queries that when invoked

produce a result set. A view acts as a virtual table. Can be used to hide table and column names from

programmers. Can be used to gather information from many

tables into one view for simpler access (PHB protector)

Not materialized

Page 35: My sql102

35

Create a VIEW, use as a table

SELECT * from get_stuff\g SELECT Customer from get_stuff\g

Page 36: My sql102

36

Stored RoutinesStored Procedures

Stored procedures and Stored Routines take zero or more arguments. A Stored Procedure can pass back values through

output variables or result sets. A function must output exactly ONE scalar variable.

MySQL Follows the ISO:2003 SQL standard syntax for stored routines.

And theses two subjects are not for newbies.

Page 37: My sql102

37

Questions/AnswersQuestions/Answers

[email protected] @stoker