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
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
2
AgendaAgenda➔ Workbench➔ User Accounts & Privileges➔ Tables and Databases➔ Views➔ Stored Routines/Procedures➔ Questions and Answers➔ Replication (after a break)
[email protected] @stoker
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
4
Did I mention free?
http://dev.mysql.com/downloads/workbench/
5
Admin
6
User Accounts
7
Users and Hosts
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
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.
10
Joe's Priviledges
11
Joe's Limits
Account Limits can be used to throttle user access.
12
Schema Privileges
13
Easier than ...GRANT INSERT, UPDATE, SELECTON world.*To 'joe'@'%';FLUSH PRIVILEGES;
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
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
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
17
CREATE a DATABASE, aka SCHEMA
Use Workbench to create a schema
18
Create 'demo'
Here we create a schema named 'demo' with UTF8 character set and the 'general' 'case insensitive' collation.
19
Review – Workbench will show
the statement used to create the schema.
Or you could type in the SQL by hand.
20
We have 'demo' Double click on demo
to expand the list of contents
21
CREATE a table
Now we can create a table in the 'demo' schema
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
23
Review the SQLWe can see the SQL that is about to be executed to create the table.
24
Double Check Any problems will be
show here as well as success
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.
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
27
Add some data
Note we do not have member_id mentioned.
28
Select data from stuff
Here we select data from the table. Note that member_id was automatically filled in for this record.
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.
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.
31
Boss: We need to track previous email addresses
Lets track previous email address by placing them in a new column creatively called oldemail.
32
Trigger in Action
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?
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
35
Create a VIEW, use as a table
SELECT * from get_stuff\g SELECT Customer from get_stuff\g
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.