My sql regis_handsonlab

  • 213 views
Uploaded on

MySQL Hands on Lab @ RMOUG labs 2011

MySQL Hands on Lab @ RMOUG labs 2011

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
213
On Slideshare
0
From Embeds
0
Number of Embeds
5

Actions

Shares
Downloads
7
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. <Insert Picture Here>MySQL: Regis Hands On LabKeith Larsonkeith.larson@oracle.comMySQL Community Manager
  • 2. The following is intended to outline our general productdirection. It is intended for information purposes only, andmay not be incorporated into any contract. It is not acommitment to deliver any material, code, orfunctionality, and should not be relied upon in makingpurchasing decisions.The development, release, and timing of any features orfunctionality described for Oracle’s products remains atthe sole discretion of Oracle.Safe Harbor Statement
  • 3. tar -xvf MySQL-5.5.16-1.rhel5.x86_64.tarrpm -i MySQL-server-5.5.16-1.rhel5.x86_64.rpmIt will prompt you with setting passwords but I will show thatsoon.MySQLInstallation
  • 4. http://dev.mysql.com/doc/index-other.htmlwget http://downloads.mysql.com/docs/world_innodb.sql.gzwget launchpad.net/test-db/employees-db-1/1.0.6/+download/employees_db-dump-files-1.0.5.tar.bz2MySQLTest Data
  • 5. -bash-3.2$ mysqlmysql> create database world;ERROR 1044 (42000): Access denied for user @localhost todatabase worldmysql> exitmysql -u rootmysql> CREATE DATABASE world;mysql>exit;mysql -u root world < world_innodb.sqlMySQLDenied
  • 6. -bash-3.2$ mysqladmin -u root password brad-bash-3.2$ mysql -u root worldERROR 1045 (28000): Access denied for userroot@localhost (using password: NO)-bash-3.2$ mysql -u root world -pEnter password:MySQLSet Root Password
  • 7. http://dev.mysql.com/doc/refman/5.5/en/grant.htmlmysql> CREATE USER monty@localhost IDENTIFIED BY some_pass;mysql> GRANT ALTER, CREATE VIEW, CREATE, DELETE, DROP, GRANTOPTION, INDEX, INSERT, SELECT, SHOW VIEW, TRIGGER, UPDATE ON*.* TO monty@localhostWITH GRANT OPTION;mysql> CREATE USER monty@% IDENTIFIED BY some_pass;mysql> GRANT ALTER, CREATE VIEW, CREATE, DELETE, DROP, GRANTOPTION, INDEX, INSERT, SELECT, SHOW VIEW, TRIGGER, UPDATE ON*.* TO monty@%WITH GRANT OPTION;mysql> flush privileges ;MySQLNew Users
  • 8. http://dev.mysql.com/doc/refman/5.5/en/grant.htmlmysql> CREATE USER admin@localhost IDENTIFIED BY admin_pass;GRANT ALL ON *.* TO admin@localhost;mysql> flush privileges ;mysql -u monty -pEnter password:mysql -u admin -pEnter password:MySQLNew Super Users
  • 9. mysql -u admin -pmysql> show databases;+--------------------+| Database |+--------------------+| information_schema || bobmason || mysql || performance_schema || test || world |+--------------------+mysql> create database <yourname>_example;Query OK, 1 row affected (0.00 sec)MySQLCreate Database/Schema
  • 10. mysql> use world;mysql> show tables;+-----------------+| Tables_in_world |+-----------------+| City || Country || CountryLanguage |+-----------------+3 rows in set (0.00 sec)mysql> show create table City;MySQLTable
  • 11. CREATE TABLE `City` (`ID` int(11) NOT NULL AUTO_INCREMENT,`Name` char(35) NOT NULL DEFAULT ,`CountryCode` char(3) NOT NULL DEFAULT ,`District` char(20) NOT NULL DEFAULT ,`Population` int(11) NOT NULL DEFAULT 0,PRIMARY KEY (`ID`),KEY `CountryCode` (`CountryCode`),CONSTRAINT `city_ibfk_1` FOREIGN KEY (`CountryCode`) REFERENCES`Country` (`Code`)) ENGINE=InnoDB AUTO_INCREMENT=4080 DEFAULT CHARSET=latin1MySQLTable City
  • 12. mysql> use <yourname>_example;mysql> CREATE TABLE `City` (`ID` int(11) NOT NULL AUTO_INCREMENT,`Name` char(35) NOT NULL DEFAULT ,`CountryCode` char(3) NOT NULL DEFAULT ,`District` char(20) NOT NULL DEFAULT ,`Population` int(11) NOT NULL DEFAULT 0,PRIMARY KEY (`ID`),KEY `CountryCode` (`CountryCode`),CONSTRAINT `city_ibfk_1` FOREIGN KEY (`CountryCode`) REFERENCES`Country` (`Code`)) ENGINE=InnoDBERROR 1005 (HY000): Cant create table <yourname>_example.City (errno: 150)http://forums.mysql.com/read.php?22,19755,19755MySQLCreate Table in Your Database
  • 13. mysql> show create table world.Country;mysql> CREATE TABLE `Country` (-> `Code` char(3) NOT NULL DEFAULT ,-> `Name` char(52) NOT NULL DEFAULT ,-> `Continent` enum(Asia,Europe,North America,Africa,Oceania,Antarctica,South America) NOT NULLDEFAULT Asia,-> `Region` char(26) NOT NULL DEFAULT ,-> `SurfaceArea` float(10,2) NOT NULL DEFAULT 0.00,-> `IndepYear` smallint(6) DEFAULT NULL,-> `Population` int(11) NOT NULL DEFAULT 0,-> `LifeExpectancy` float(3,1) DEFAULT NULL,-> `GNP` float(10,2) DEFAULT NULL,-> `GNPOld` float(10,2) DEFAULT NULL,-> `LocalName` char(45) NOT NULL DEFAULT ,-> `GovernmentForm` char(45) NOT NULL DEFAULT ,-> `HeadOfState` char(60) DEFAULT NULL,-> `Capital` int(11) DEFAULT NULL,-> `Code2` char(2) NOT NULL DEFAULT ,-> PRIMARY KEY (`Code`)-> ) ENGINE=InnoDB;Query OK, 0 rows affected (0.02 sec)MySQLTry again ….
  • 14. Mysql> CREATE TABLE `City` (-> `ID` int(11) NOT NULL AUTO_INCREMENT,-> `Name` char(35) NOT NULL DEFAULT ,-> `CountryCode` char(3) NOT NULL DEFAULT ,-> `District` char(20) NOT NULL DEFAULT ,-> `Population` int(11) NOT NULL DEFAULT 0,-> PRIMARY KEY (`ID`),-> KEY `CountryCode` (`CountryCode`),-> CONSTRAINT `city_ibfk_1` FOREIGN KEY (`CountryCode`)REFERENCES `Country` (`Code`)-> ) ENGINE=InnoDB;Query OK, 0 rows affected (0.04 sec)MySQLCity works now...
  • 15. Mysql> CREATE TABLE `CountryLanguage` (`CountryCode` char(3) NOT NULL DEFAULT ,`Language` char(30) NOT NULL DEFAULT ,`IsOfficial` enum(T,F) NOT NULL DEFAULT F,`Percentage` float(4,1) NOT NULL DEFAULT 0.0,PRIMARY KEY (`CountryCode`,`Language`),KEY `CountryCode` (`CountryCode`),CONSTRAINT `countryLanguage_ibfk_1` FOREIGN KEY (`CountryCode`)REFERENCES `Country` (`Code`)) ENGINE=InnoDBMySQLCountryLanguage as well..
  • 16. mysql> use <yourname>_example;mysql> insert into Country select * from world.Country;Query OK, 239 rows affected (0.03 sec)Records: 239 Duplicates: 0 Warnings: 0mysql> insert into CountryLanguage select * from world.CountryLanguage;Query OK, 984 rows affected (0.03 sec)Records: 984 Duplicates: 0 Warnings: 0mysql> insert into City select * from world.City;Query OK, 4079 rows affected (0.29 sec)Records: 4079 Duplicates: 0 Warnings: 0Faster -- mysql -u admin -p <yourname>_example < world_innodb.sqlMySQLAdd Data
  • 17. SELECT ID , Name , CountryCode , PopulationFROM CityWHERE CountryCode = USAORDER BY Population DESC limit 20;SELECT C.ID , C.Name , C.CountryCode , C.PopulationFROM City CINNER JOIN Country Y ON Y.Code = C.CountryCodeINNER JOIN CountryLanguage L ON L.CountryCode = C.CountryCodeWHERE Y.Code = USAORDER BY C.Population DESC limit 20;MySQLLook at some data
  • 18. mysql> SELECT C.ID , C.Name , C.CountryCode , C.Population-> FROM City C-> INNER JOIN Country Y ON Y.Code = C.CountryCode-> INNER JOIN CountryLanguage L ON L.CountryCode = C.CountryCode-> WHERE Y.Code = USA-> ORDER BY Population DESC-> GROUP BY C.Name limit 20;MySQLGroup by…
  • 19. mysql> SELECT C.ID , C.Name , C.CountryCode , C.Population-> FROM City C-> INNER JOIN Country Y ON Y.Code = C.CountryCode-> INNER JOIN CountryLanguage L ON L.CountryCode = C.CountryCode-> WHERE Y.Code = USA-> ORDER BY Population DESC-> GROUP BY C.Name limit 20;ERROR 1064 (42000): You have an error in your SQL syntax; check the manualthat corresponds to your MySQL server version for the right syntax to usenear GROUP BY C.Name limit 20 at line 7mysql>MySQLGroup by…Error
  • 20. SELECT C.ID , C.Name , C.CountryCode , C.PopulationFROM City CINNER JOIN Country Y ON Y.Code = C.CountryCodeINNER JOIN CountryLanguage L ON L.CountryCode = C.CountryCodeWHERE Y.Code = USAGROUP BY C.NameORDER BY Population DESClimit 20;MySQLError fixed
  • 21. SET @RANK=0; # VariableSELECT @RANK:=@RANK+1 AS RANK, C.ID , C.Name , C.CountryCode ,C.PopulationFROM City CINNER JOIN Country Y ON Y.Code = C.CountryCodeINNER JOIN CountryLanguage L ON L.CountryCode = C.CountryCodeWHERE Y.Code = USAGROUP BY C.NameORDER BY Population DESClimit 20;MySQLRank Results
  • 22. SET @RANK=0;SELECT @RANK:=@RANK+1 AS RANK, C.ID , C.Name , C.CountryCode , C.PopulationFROM City CINNER JOIN Country Y ON Y.Code = C.CountryCodeINNER JOIN CountryLanguage L ON L.CountryCode = C.CountryCodeWHERE Y.Code = USAUNIONSELECT @RANK:=@RANK+1 AS RANK, C.ID , C.Name , C.CountryCode , C.PopulationFROM City CINNER JOIN Country Y ON Y.Code = C.CountryCodeINNER JOIN CountryLanguage L ON L.CountryCode = C.CountryCodeWHERE Y.Code = USA AND C.name = DenverGROUP BY C.NameORDER BY Population DESClimit 25;MySQLUnion Example
  • 23. SET @RANK=0;SELECT @RANK:=@RANK+1 AS RANK, C.ID , C.Name , C.CountryCode , C.PopulationFROM City CINNER JOIN Country Y ON Y.Code = C.CountryCodeINNER JOIN CountryLanguage L ON L.CountryCode = C.CountryCodeWHERE Y.Code = USAUNIONSELECT @RANK:=@RANK+1 AS RANK, C.ID , C.Name , C.CountryCode , C.PopulationFROM City CINNER JOIN Country Y ON Y.Code = C.CountryCodeINNER JOIN CountryLanguage L ON L.CountryCode = C.CountryCodeWHERE Y.Code = USA AND C.name = DenverGROUP BY C.NameORDER BY Population DESClimit 25;## Not what you wanted is it....MySQLUnion Example
  • 24. SET @RANK=0;SELECT @RANK:=@RANK+1 AS RANK, C.ID , DISTINCT(C.Name) as Name , C.CountryCode ,C.PopulationFROM City CINNER JOIN Country Y ON Y.Code = C.CountryCodeINNER JOIN CountryLanguage L ON L.CountryCode = C.CountryCodeWHERE Y.Code = USA AND C.Population >= 3694820UNIONSELECT @RANK:=@RANK+1 AS RANK, C.ID , C.Name , C.CountryCode , C.PopulationFROM City CINNER JOIN Country Y ON Y.Code = C.CountryCodeINNER JOIN CountryLanguage L ON L.CountryCode = C.CountryCodeWHERE Y.Code = USA AND C.name = DenverGROUP BY C.NameORDER BY Population DESClimit 25;MySQLUnion Example
  • 25. SET @RANK=0;SELECT @RANK:=@RANK+1 AS RANK, C.ID , C.Name , C.CountryCode , C.PopulationFROM City CINNER JOIN Country Y ON Y.Code = C.CountryCodeINNER JOIN CountryLanguage L ON L.CountryCode = C.CountryCodeWHERE Y.Code = USAGROUP BY C.NameORDER BY Population DESClimit 100 INTO OUTFILE /tmp/mysql_export.csvFIELDS TERMINATED BY ,LINES TERMINATED BY nMySQLExport Example
  • 26. http://dev.mysql.com/doc/refman/5.5/en/create-trigger.htmlCREATE TABLE test1(a1 INT);CREATE TABLE test2(a2 INT);CREATE TABLE test3(a3 INT NOT NULL AUTO_INCREMENT PRIMARY KEY);CREATE TABLE test4(a4 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,b4 INT DEFAULT 0);delimiter |CREATE TRIGGER testref BEFORE INSERT ON test1FOR EACH ROW BEGININSERT INTO test2 SET a2 = NEW.a1;DELETE FROM test3 WHERE a3 = NEW.a1;UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;END; |delimiter ;MySQLTrigger Example
  • 27. http://dev.mysql.com/doc/refman/5.5/en/create-trigger.htmlINSERT INTO test3 (a3) VALUES(NULL), (NULL), (NULL), (NULL), (NULL),(NULL), (NULL), (NULL), (NULL), (NULL);INSERT INTO test4 (a4) VALUES(0), (0), (0), (0), (0), (0), (0), (0), (0), (0);SELECT * FROM test1; 8 rows in set (0.00 sec)SELECT * FROM test2; 8 rows in set (0.00 sec)SELECT * FROM test3; 8 rows in set (0.00 sec)SELECT * FROM test4; 8 rows in set (0.00 sec)MySQLTrigger Example
  • 28. http://dev.mysql.com/doc/refman/5.5/en/stored-routines.htmlThe CREATE ROUTINE , ALTER ROUTINE , EXECUTE privilege is needed for storedroutines.mysql> delimiter //mysql> CREATE PROCEDURE simpleproc (OUT param1 INT)-> BEGIN-> SELECT COUNT(*) INTO param1 FROM t;-> END//Query OK, 0 rows affected (0.00 sec)mysql> delimiter ;mysql> CALL simpleproc(@a);Query OK, 0 rows affected (0.00 sec)mysql> SELECT @a;+------+| @a |+------+| 3 |+------+1 row in set (0.00 sec)MySQLStored Routines (Procedures and Functions)
  • 29. http://dev.mysql.com/doc/refman/5.5/en/commit.htmlTo disable autocommit mode, use the following statement: SET autocommit=0;To disable autocommit mode for a single series of statements, use the STARTTRANSACTION statement:START TRANSACTION;SELECT @A:=SUM(salary) FROM table1 WHERE type=1;UPDATE table2 SET summary=@A WHERE type=1;COMMIT;MySQLTransactions
  • 30. MySQL Workbench SEDatabase Design•  Visual Design, modeling•  Forward/Reverse Engineer•  Schema validation, Schema docSQL Development• SQL Editor - Color SyntaxHighlighting• Objects - Import/Export, Browse/Edit• Connections - Wizard, SSH TunnelDatabase Administration• Status, Configuration, Start/Stop• Users, Security, Sessions• Import/Export Dump FilesScripting & Plug-in SupportUI Designed to match VS 2010Saves you time developing andmanaging your MySQL apps.GA
  • 31. MySQL Workbench - Plugins•  Community driven Plugins & Add-ons site–  Code in Python, share with the community
  • 32. MySQL Workbench – Hands On Lab•  http://sqlhjalp.com/iso/OTN_Developer_Day_MySQL.iso
  • 33. Additional Resourcesmysql.com•  TCO calculator•  White Papers•  Customer use cases and success storiesdev.mysql.com•  Downloads•  Documentation•  Forums•  PlanetMySQLeDelivery.com•  Download and evaluate all MySQL products
  • 34. Additional Resourcesmysql.com•  Download MySQL 5.5, MySQL Cluster 7.1 GA, GPL Products•  MySQL Products, Editions, Licensing Options•  TCO calculator•  Upcoming Events•  Customer use cases and success storiesdev.mysql.com•  Download MySQL 5.6 DMR and Labs “early access” features•  Developer Zone Articles, How to’seDelivery.com•  Download and evaluate all MySQL products
  • 35. Additional ResourcesPlanet.mysql.com•  Blog feeds from the experts and the communityBooks:•  MySQL by Paul DuBois•  MySQL Administrators Bible•  High Performance MySQL: Optimization, Backups, Replication,and Moreforums.mysql.com•  Community interaction
  • 36. <Insert Picture Here>Thanks for attending!keith.larson@oracle.com
  • 37. Extra Slides