My sql102


Published on

MySQL 102

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

My sql102

  1. 1. MySQL 102 MySQL 102 is the second part of apresentation to be preceded by MySQL 101 and covers material that will build on the previous presentation. 1Http://
  2. 2. Agenda➔ Workbench➔ User Accounts & Privileges➔ Tables and Databases➔ Views➔ Stored Routines/Procedures➔ Questions and Answers➔ Replication (after a break) @stoker 2
  3. 3. Workbench FREE Three tools in one  Query Tool  Admin Tool  Entity Relationship Mapper Scripts available for setting up replication, fail over and more 3
  4. 4. Did I mention free? 4
  5. 5. Admin 5
  6. 6. User Accounts 6
  7. 7. Users and Hosts 7
  8. 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 8
  9. 9. Joes Infojoe can connect to the mysqld server from any host.Note he does not have a password which can be aserious security concern. 9htallation.html
  10. 10. Joes Priviledges 10
  11. 11. Joes LimitsAccount Limits can be used to throttle user access. 11
  12. 12. Schema Privileges 12
  13. 13. Easier than ...GRANT INSERT, UPDATE,SELECTON world.*To joe@%;FLUSH PRIVILEGES; 13
  14. 14. Tables & DatabasesDatabases contain tables, whichare used for storing data.Databases also contain objectssuch as stored routines ortriggers. – MySQl 5.0 CertificationGuidehttphtml 14
  15. 15. 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
  16. 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. 17. CREATE a DATABASE, aka SCHEMA Use Workbench to create a schema 17
  18. 18. Create demoHere we create a schema named demo with UTF8 character setand the general case insensitive collation. 18
  19. 19. Review – Workbench will show the statement used to create the schema. Or you could type in the SQL by hand. 19
  20. 20. We have demo Double click on demo to expand the list of contents 20
  21. 21. CREATE a table Now we can create a table in the demo schema 21
  22. 22. Creating a TableCreate a table named stuff with a column namedmember_id that is NOT NULL, AUTOINCREMENT,UNSIGNED and a PRIMARY KEY 22
  23. 23. Review the SQLWe can see theSQL that is aboutto be executed tocreate the table. 23
  24. 24. Double Check Any problems will be show here as well as success 24
  25. 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. 26. After adding two more columnsFor the sake of brevity,two more columns havebeen added for name andemail, both are stup tohold CHAR data 26
  27. 27. Add some data Note we do not have member_id mentioned. 27
  28. 28. Select data from stuff Here we select data from the table. Note that member_id was automatically filled in for this record. 28
  29. 29. Add a few more recordsHere a few more recordshave been added and themember_id has again hasbeen incremented andinserted. 29
  30. 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. 31. Boss: We need to trackprevious email addresses Lets track previous email address by placing them in a new column creatively called oldemail. 31
  32. 32. Trigger in Action 32
  33. 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? 33
  34. 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. 35. Create a VIEW, use as a table SELECT * from get_stuffg SELECT Customer from get_stuffg 35
  36. 36. Stored Routines Stored Procedures Stored procedures and Stored Routines take zero or more arguments. A Stored Procedure can pass back values throughoutput 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. 37. Questions/ @stoker 37