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.


                                                 1
Http://slideshare.net/davestokes/presentations
Agenda
āž”   Workbench
āž”   User Accounts & Privileges
āž”   Tables and Databases
āž”   Views
āž”   Stored Routines/Procedures
āž”   Questions and Answers
āž”   Replication (after a break)
David.Stokes@Oracle.com   @stoker   2
Workbench
ī€Œ   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   3
Did I mention free?




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




        5
User Accounts




           6
Users and Hosts




             7
Know whom
can 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

                              8
Joe's Info


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



                                               9
htallation.html
Joe's Priviledges




               10
Joe's Limits


Account Limits can be used to throttle user access.

                                               11
Schema Privileges




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

                   13
Tables & 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                       14
1. CREATE DATABASE foo
               2. (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

                                15
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



                               16
CREATE a DATABASE, aka SCHEMA



ī€Œ   Use Workbench to
    create a schema




                                       17
Create 'demo'


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


                                                      18
Review –
ī€Œ   Workbench will show
    the statement used to
    create the schema.
ī€Œ   Or you could type in
    the SQL by hand.




                            19
We have 'demo'
ī€Œ   Double click on demo
    to expand the list of
    contents




                            20
CREATE a table



ī€Œ   Now we can create a
    table in the 'demo'
    schema




                                 21
Creating a Table



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

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




                    23
Double Check
ī€Œ   Any problems will be
    show here as well as
    success




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




                           26
Add some data

ī€Œ   Note we do not have member_id mentioned.




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




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




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




                                              30
Boss: We need to track
previous email addresses




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




                                               31
Trigger in Action




               32
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?
                                        33
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




                                                  34
Create a VIEW,
        use as a table



ī€Œ   SELECT * from get_stuffg
ī€Œ   SELECT Customer from get_stuffg




                                       35
Stored Routines
  Stored 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.


                                                        36
Questions/Answers




David.Stokes@Oracle.com   @stoker   37

My sql102

  • 1.
    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. 1 Http://slideshare.net/davestokes/presentations
  • 2.
    Agenda āž” Workbench āž” User Accounts & Privileges āž” Tables and Databases āž” Views āž” Stored Routines/Procedures āž” Questions and Answers āž” Replication (after a break) David.Stokes@Oracle.com @stoker 2
  • 3.
    Workbench ī€Œ 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 3
  • 4.
    Did I mentionfree? http://dev.mysql.com/downloads/workbench/ 4
  • 5.
  • 6.
  • 7.
  • 8.
    Know whom can dowhat ī€Œ We have four accounts ī€Œ Anonymous (%) ī€Œ ODBC ī€Œ joe ī€Œ root ī€Œ ODBC and joe can login from any host while root and anonymous are local host only 8
  • 9.
    Joe's Info 'joe' canconnect to the mysqld server from any host. Note he does not have a password which can be a serious security concern. 9 htallation.html
  • 10.
  • 11.
    Joe's Limits Account Limitscan be used to throttle user access. 11
  • 12.
  • 13.
    Easier than ... GRANTINSERT, UPDATE, SELECT ON world.* To 'joe'@'%'; FLUSH PRIVILEGES; 13
  • 14.
    Tables & Databases Databasescontain tables, which are used for storing data. Databases also contain objects such as stored routines or triggers. – MySQl 5.0 Certification Guide httphtml 14
  • 15.
    1. CREATE DATABASEfoo 2. (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 15
  • 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 16
  • 17.
    CREATE a DATABASE,aka SCHEMA ī€Œ Use Workbench to create a schema 17
  • 18.
    Create 'demo' Here wecreate a schema named 'demo' with UTF8 character set and the 'general' 'case insensitive' collation. 18
  • 19.
    Review – ī€Œ Workbench will show the statement used to create the schema. ī€Œ Or you could type in the SQL by hand. 19
  • 20.
    We have 'demo' ī€Œ Double click on demo to expand the list of contents 20
  • 21.
    CREATE a table ī€Œ Now we can create a table in the 'demo' schema 21
  • 22.
    Creating a Table Createa table named stuff with a column named member_id that is NOT NULL, AUTOINCREMENT, UNSIGNED and a PRIMARY KEY 22
  • 23.
    Review the SQL Wecan see the SQL that is about to be executed to create the table. 23
  • 24.
    Double Check ī€Œ Any problems will be show here as well as success 24
  • 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. 25
  • 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 26
  • 27.
    Add some data ī€Œ Note we do not have member_id mentioned. 27
  • 28.
    Select data fromstuff Here we select data from the table. Note that member_id was automatically filled in for this record. 28
  • 29.
    Add a fewmore records Here a few more records have been added and the member_id has again has been incremented and inserted. 29
  • 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. 30
  • 31.
    Boss: We needto track previous email addresses ī€Œ Lets track previous email address by placing them in a new column creatively called oldemail. 31
  • 32.
  • 33.
    Did you catchthe 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? 33
  • 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 34
  • 35.
    Create a VIEW, use as a table ī€Œ SELECT * from get_stuffg ī€Œ SELECT Customer from get_stuffg 35
  • 36.
    Stored Routines Stored 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. 36
  • 37.