Beginner guide to mysql command line

73,897 views
73,468 views

Published on

This presentation is useful to learn mysql command line tool.

Published in: Technology
1 Comment
19 Likes
Statistics
Notes
No Downloads
Views
Total views
73,897
On SlideShare
0
From Embeds
0
Number of Embeds
21
Actions
Shares
0
Downloads
0
Comments
1
Likes
19
Embeds 0
No embeds

No notes for slide

Beginner guide to mysql command line

  1. 1. Beginner guide to mysql command line Tool . <br />Priti Solanki<br />
  2. 2. agenda<br /><ul><li>what is mysqltool
  3. 3. mysql tool on windows
  4. 4. mysqlcommands
  5. 5. mysqltips</li></ul>Priti Solanki<br />
  6. 6. What is “mysql” !!<br /><ul><li> “mysql” is a simple SQL shell
  7. 7. It supports interactive and non-interactive mode.</li></ul>On Windows<br />Make sure you have XAMPP/WAMP installed on your local machine.<br />Open command prompt.<br />Type on command line C:>mysql<br /> If “'mysql' is not recognized as an internal or external command“ error comes then you have to add the path to mysql bin directory in window’s environment variable.<br />http://dev.mysql.com/doc/refman/5.5/en/mysql-installation-windows-path.html<br />Priti Solanki<br />
  8. 8. Mysql Command<br /><ul><li>Connect to mysql</li></ul>shell>mysql –hlocalhost –uroot –p<br /> Enter password:<br />Welcome to the MySQL monitor. Commands end with ; or g.<br /> Your MySQL connection id is 22<br /> Server version: 5.1.41 Source distribution<br /> <br /> Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.<br />If you are logged in successfully you will be displayed Welcome message and your prompt will get modified as <br />mysql>_<br /><ul><li>Show database list</li></ul>mysql> show databases;<br /> <br />+--------------------+<br />| Database |<br />+--------------------+<br />| information_schema |<br />| cdcol |<br />| mechanics |<br />| mysql |<br />| ninja_world |<br />| phpmyadmin |<br />| test |<br />+--------------------+<br />7 rows in set (0.00 sec)<br />Priti Solanki<br />
  9. 9. Mysql Command<br /><ul><li>How to select data from tables?</li></ul>Before firing query to mysql you have to let mysql know which database you are going to use.<br />mysql> use ninja_world;<br /> Database changed<br /> listing of tables in this database before I start querying ?<br />mysql> show tables;<br />+-----------------------+<br />| Tables_in_ninja_world |<br />+-----------------------+<br />| ninja |<br />| power |<br />+-----------------------+<br />2 rows in set (0.00 sec)<br />mysql> select * from ninja;<br />+----+----------------+----------+<br />| id | name | power-id |<br />+----+----------------+----------+<br />| 1 | NarutoUzumaki | 0 |<br />| 2 | Sakura Haruno | 0 |<br />| 3 | SasukeUchiha | 0 |<br />| 4 | Rocklee | 0 |<br />| 5 | ChojiAkimichi | 0 |<br />+----+----------------+----------+<br />Priti Solanki<br />
  10. 10. <ul><li>How to insert data in a table?</li></ul>mysql> use ninja_world;<br /> Database changed<br />mysql> INSERT INTO ninja( `name` , `power-id` )<br /> -> VALUES (<br /> -> 'komizkhazi', '1'<br /> -> );<br /> Query OK, 1 row affected (0.00 sec)<br /><ul><li>How to update data in a table?</li></ul>mysql> Update ninja SET name="psolanki" where id=16;<br />Query OK, 1 row affected (0.01 sec)<br /> Rows matched: 1 Changed: 1 Warnings: 0<br /><ul><li>How to delete data from a table?</li></ul>mysql> delete from ninja where id=16;<br />Query OK, 1 row affected (0.00 sec)<br />Priti Solanki<br />
  11. 11. mysqlTips<br /><ul><li>Use ‘- -quick’ option to retrieve results from the server a row at a time rather than retrieving the entire result set and buffering it in memory before displaying it.
  12. 12. Some time query results are more readable when displayed vertically.</li></ul>mysql> select * from mechanic where if=101G<br /> Press enter. The output will be formatted as shown below<br /><ul><li>For tabular output, use “- - raw” OR “-r” mysql options.</li></ul> shell> mysql -r -uroot -e "SHOW VARIABLES LIKE 'version%'";<br />+-------------------------+---------------------+<br />| Variable_name | Value |<br />+-------------------------+---------------------+<br />| version | 5.1.41 |<br />| version_comment | Source distribution |<br />| version_compile_machine | ia32 |<br />| version_compile_os | Win32 |<br />+-------------------------+---------------------+<br />Priti Solanki<br />
  13. 13. Continue….<br /><ul><li>Get xml output of your mysql query.</li></ul> shell> mysql -X -uroot -e "USE ninja_world;select * from ninja LIMIT 1"; <br /><?xml version="1.0"?><br /><resultset statement="select * from ninja" xmlns:xsi="http://www.w3.org/2001/XML<br />Schema-instance"><br /> <row><br /> <field name="id">1</field><br /> <field name="name">NarutoUzumaki</field><br /> <field name="power-id">0</field><br /> </row><br /></resultset><br /><ul><li>Get html output from mysql query.</li></ul>shell> mysql --html -uroot -e "USE ninja_world;select * from ninja LIMIT 1";<br /><TABLE BORDER=1><TR><TH>id</TH><TH>name</TH><TH>power-id</TH></TR><TR><TD>1</TD><br /><TD>NarutoUzumaki</TD><TD>0</TD></TR></TABLE><br />Priti Solanki<br />
  14. 14. Continue….<br /><ul><li>To reconfigure the mysql prompt.</li></ul>mysql> R ninja><br /> ninja><br /><ul><li>Read the source file and executes the statements (in windows).</li></ul>mysql> source c:test.sql <br /> Database changed<br />+----+----------------+----------+<br />| id | name | power-id |<br />+----+----------------+----------+<br />| 1 | NarutoUzumaki | 0 |<br />| 2 | Sakura Haruno | 0 |<br />| 3 | SasukeUchiha | 0 |<br />| 4 | Rocklee | 0 |<br />Test.sql is a sql file which contains following sql statement<br />Use ninja_world;<br />Select * from ninja;<br />From here you are good to go for your own  !!. For any modification, correction or suggestion which can make this presentation good for beginners please care to drop a line on pritiatwork@gmail.com<br />Priti Solanki<br />

×