Published on

Learn MYSQL installation with help of screenshots and also learn various commands in MYSQL

Published in: Technology
  • Be the first to comment

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

No notes for slide


  1. 1. MYSQL
  2. 2. <ul><li>a multithreaded, multi-user SQL database management system. </li></ul><ul><li>A Database Management System which is available for both Linux and Windows. </li></ul><ul><li>Popular Open Source Database. </li></ul><ul><li>a relational database management system (RDBMS) that has more than 6 million installations. </li></ul>What is MYSQL?
  5. 5. download the latest MySQL install package from the MySQL site. I recommend you use the Windows Essentials package. <ul><li>Double click on the installation package to initilize the installation wizard </li></ul>
  6. 6. Step 1: Choose the setup type <ul><li>use the Custom option so that you can define your installation path. </li></ul><ul><li>Press the Next button </li></ul>
  7. 7. Step 2 <ul><li>Use the Change botton to specify your installation path,use a path without spaces(recommended). </li></ul><ul><li>Click the Next button </li></ul>
  8. 8. Step 3 <ul><li>select the Skip Sign-Up option(Account not needed) </li></ul><ul><li>click the Next button </li></ul>
  9. 9. Step 4 <ul><li>Check the “Configure the MySQL Server now” </li></ul><ul><li>click the Finish button to complete the installtion wizard and start the configuration wizard. </li></ul>
  10. 10. Step 5: Configuring MySQL <ul><li>You should now be presented with the MySQL Server Instance Configuration Wizard. </li></ul><ul><li>Click the Next button to continue. </li></ul>
  11. 11. Step 6 <ul><li>Select the Detailed Configuration option </li></ul><ul><li>Click the Next button </li></ul>
  12. 12. Step 7 <ul><li>choose the server type </li></ul><ul><li>click the Next button to continue </li></ul>
  13. 13. Step 8 <ul><li>Select your database usage option </li></ul><ul><li>Click the Next button </li></ul>
  14. 14. Step 9 <ul><li>If you selected to enables the InnoDB database engine, then you will be prompted to set a path for use by the InnoDB datafile. </li></ul><ul><li>Leave this as the defaultu </li></ul><ul><li>Click the Next button </li></ul>
  15. 15. Step 10 <ul><li>set the estimated conccurrent connection usage. </li></ul><ul><li>using the Decision Support (DSS)/OLAP option which sets the concurrent connection limit to 20 which is pretty safe. </li></ul><ul><li>Click next button </li></ul>
  16. 16. Step 11 <ul><li>Check the Enable TCP/IP Networking option </li></ul><ul><li>uncheck the Enable Strict Mode option even though it is on by default and recommended. </li></ul><ul><li>Click next </li></ul>
  17. 17. Step 12 <ul><li>Select Standard Character Set as default </li></ul><ul><li>Click next button </li></ul>
  18. 18. Step 13 <ul><li>Check the Install As Windows Service and Launch MySQL Server Automatically & Include Bin Directory in Windows PATH options so that you can execute the MySQL tools from anywhere when using the command line. This can be handy when creating automated scripts for backups, etc. </li></ul><ul><li>click the Next button </li></ul>
  19. 19. Step 14 <ul><li>Set a strong password (atleast 6 characters) </li></ul><ul><li>DON'T CHECK the Enable root access for remote machines option & Create An Anonymous Account option. </li></ul><ul><li>Click next button </li></ul>
  20. 20. Step 15 <ul><li>Confrim your settings. </li></ul><ul><li>Click the Execute button </li></ul><ul><li>Press Finish to complete the configuration wizard and exit. </li></ul>
  21. 21. COMMANDS
  22. 22. Login to MySQL monitor ..mysqlinmysql -u[username] -p[password] Example: ..mysqlinmysql -uroot -pmysecret
  23. 23. Create a database on the sql server. SYNTAX: CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [create_specification] ... create_specification: [DEFAULT] CHARACTER SET [=] charset_name | [DEFAULT] COLLATE [=] collation_name u-1@srv-1 mysqlart $ mysql -u root Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 5 to server version: 4.0.14-log Type 'help;' or 'h' for help. Type 'c' to clear the buffer. mysql> create database sysops; Query OK, 1 row affected (0.00 sec) mysql> quit Bye u-1@srv-1 mysqlart $ Example:
  24. 24. List all databases on the sql server. SYNTAX: mysql> show databases; mysql> SHOW DATABASES; +----------+ | Database | +----------+ | info     | | java2s   | | mysql    | | t        | | test     | | ttt      | +----------+ 6 rows in set (0.00 sec)
  25. 25. Switch to a database. mysql> use [db name];
  26. 26. To see all the tables in the db. mysql> show tables;
  27. 27. CREATE TABLE SYNTAX: CREATE TABLE [table_name] ( [column_name1] INT AUTO_INCREMENT, [column_name2] VARCHAR(30) NOT NULL, [column_name3] ENUM('guest', 'customer', 'admin')NULL, [column_name4] DATE NULL, [column_name5] VARCHAR(30) NOT NULL, [column_name6] DATETIME NOT NULL, [column_name7] CHAR(1) NULL, [column_name8] BLOB NULL, [column_name9] TEXT NOT NULL, UNIQUE(username), PRIMARY KEY (column_name1) ); Example: CREATE TABLE user ( userid INT AUTO_INCREMENT, username VARCHAR(30) NOT NULL, group_type ENUM('guest', 'customer', 'admin') NULL, date_of_birth DATE NULL, password VARCHAR(30) NOT NULL, registration_date DATETIME NOT NULL, account_disable CHAR(1) NULL, image BLOB NULL, comment TEXT NOT NULL, UNIQUE(username), PRIMARY KEY (userid) );
  28. 28. INSERT STATEMENTS <ul><li>Syntax: </li></ul><ul><li>INSERT INTO table_name ( `col_A`, `col_B`, `col_C`) VALUES ( `col_A_data`, `col_B_data`, `col_C_data`) ; </li></ul><ul><li>Example: </li></ul><ul><li>INSERT INTO music ( 'id', `artist`, `album`) VALUES ( '1', `the beatles`, `Abbey Road`); </li></ul>
  29. 29. REPLACE STATEMENTS <ul><li>Syntax: </li></ul><ul><li>REPLACE INTO table_name ( `col_A`, `col_B`) VALUES ( `col A data`, `col B data`) ; </li></ul><ul><li>Example: </li></ul><ul><li>REPLACE INTO music ( 'id', `artist`, `album`) VALUES ( '1', `the beatles`, `abbey road`); </li></ul>
  30. 30. UPDATE STATEMENTS <ul><li>Syntax: </li></ul><ul><li>UPDATE table_name SET col_B='new_data' WHERE col_A='reference_data' ; </li></ul><ul><li>Example: </li></ul><ul><li>UPDATE music SET title='Come Together' WHERE id=1; </li></ul>
  31. 31. Add a new column &quot;male&quot; in table user. Syntax: ALTER TABLE [table_name] ADD COLUMN [column_name] CHAR(1) NOT NULL; <ul><li>Example: </li></ul><ul><li>ALTER TABLE user </li></ul><ul><li>ADD COLUMN male </li></ul><ul><li>CHAR(1) NOT NULL; </li></ul>
  32. 32. Change column name &quot;male&quot; into &quot;gender&quot; in table user and change the type to VARCHAR(3) and allow NULL values. Syntax: ALTER TABLE [table_name] CHANGE [old_column] [new_column] VARCHAR(3) NULL; Example: ALTER TABLE user CHANGE male gender VARCHAR (3) NULL;
  33. 33. Change the size of column &quot;gender&quot; from 3 to 6 in table user. Syntax: ALTER TABLE [table_name] MODIFY [column_name] VARCHAR(6); Example: ALTER TABLE user MODIFY gender VARCHAR(6);
  34. 34. SELECT STATEMENTS <ul><li>Syntax: </li></ul><ul><li>SELECT * FROM table_name WHERE 1 ; </li></ul><ul><li>Example: </li></ul><ul><li>SELECT * FROM music WHERE 1; </li></ul>
  35. 35. DELETE STATEMENTS <ul><li>Syntax: </li></ul><ul><li>DELETE FROM table_name WHERE column_name='search_data'; </li></ul><ul><li>Example: </li></ul><ul><li>DELETE FROM music WHERE artist='the beatles'; </li></ul>
  36. 36. Show field formats of the selected table. Syntax: DESCRIBE [table_name]; Example: DESCRIBE mos_menu;
  37. 37. To see database's field formats. mysql> describe [table name];
  38. 38. To delete a db. mysql> drop database [database name]; Example: DROP DATABASE demodb;
  39. 39. To delete a table. mysql> drop table [table name]; Example: DROP TABLE user;
  40. 40. Show all data in a table. mysql> SELECT * FROM [table name]; Example: SELECT * FROM mos_menu;
  41. 41. Show all records from mos_menu table containing name &quot;Home&quot;. SELECT * FROM [table_name] WHERE [field_name]=[value]; Example: SELECT * FROM mos_menu WHERE name = &quot;Home&quot;;
  42. 42. Returns the columns and column information pertaining to the designated table. mysql> show columns from [table name];
  43. 43. Show certain selected rows with the value &quot;whatever&quot;. mysql> SELECT * FROM [table name] WHERE [field name] = &quot;whatever&quot;;
  44. 44. Show all records containing the name &quot;Bob&quot; AND the phone number '3444444'. mysql> SELECT * FROM [table name] WHERE name = &quot;Bob&quot; AND phone_number = '3444444';
  45. 45. Show all records not containing the name &quot;Bob&quot; AND the phone number '3444444' order by the phone_number field. mysql> SELECT * FROM [table name] WHERE name != &quot;Bob&quot; AND phone_number = '3444444' order by phone_number;
  46. 46. Show all records starting with the letters 'bob' AND the phone number '3444444'. mysql> SELECT * FROM [table name] WHERE name like &quot;Bob%&quot; AND phone_number = '3444444';
  47. 47. Show all records starting with the letters 'bob' AND the phone number '3444444' limit to records 1 through 5. mysql> SELECT * FROM [table name] WHERE name like &quot;Bob%&quot; AND phone_number = '3444444' limit 1,5;
  48. 48. Use a regular expression to find records. Use &quot;REGEXP BINARY&quot; to force case-sensitivity. This finds any record beginning with a. mysql> SELECT * FROM [table name] WHERE rec RLIKE &quot;^a&quot;;
  49. 49. Show unique records. mysql> SELECT DISTINCT [column name] FROM [table name];
  50. 50. Show selected records sorted in an ascending (asc) or descending (desc). mysql> SELECT [col1],[col2] FROM [table name] ORDER BY [col2] DESC;
  51. 51. Return number of rows. mysql> SELECT COUNT(*) FROM [table name];
  52. 52. Sum column. mysql> SELECT SUM(*) FROM [table name];
  53. 53. Join tables on common columns. mysql> select lookup.illustrationid, lookup.personid,person.birthday from lookup left join person on lookup.personid=person.personid=statement to join birthday in person table with primary illustration id;
  54. 54. Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs. # mysql -u root -p mysql> use mysql; mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password')); mysql> flush privileges;
  55. 55. Change a users password from unix shell. # [mysql dir]/bin/mysqladmin -u username -h -p password 'new-password'
  56. 56. Change a users password from MySQL prompt. Login as root. Set the password. Update privs. # mysql -u root -p mysql> SET PASSWORD FOR 'user'@'hostname' = PASSWORD('passwordhere'); mysql> flush privileges;
  57. 57. Recover a MySQL root password. Stop the MySQL server process. Start again with no grant tables. Login to MySQL as root. Set new password. Exit MySQL and restart MySQL server. # /etc/init.d/mysql stop # mysqld_safe --skip-grant-tables & # mysql -u root mysql> use mysql; mysql> update user set password=PASSWORD(&quot;newrootpassword&quot;) where User='root'; mysql> flush privileges; mysql> quit # /etc/init.d/mysql stop # /etc/init.d/mysql start
  58. 58. Set a root password if there is on root password. # mysqladmin -u root password newpassword
  59. 59. Update a root password. # mysqladmin -u root -p oldpassword newpassword
  60. 60. Allow the user &quot;bob&quot; to connect to the server from localhost using the password &quot;passwd&quot;. Login as root. Switch to the MySQL db. Give privs. Update privs. # mysql -u root -p mysql> use mysql; mysql> grant usage on *.* to bob@localhost identified by 'passwd'; mysql> flush privileges;
  61. 61. Give user privilages for a db. Login as root. Switch to the MySQL db. Grant privs. Update privs. # mysql -u root -p mysql> use mysql; mysql> INSERT INTO db (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) VALUES ('%','databasename','username','Y','Y','Y','Y','Y','N'); mysql> flush privileges; or mysql> grant all privileges on databasename.* to username@localhost; mysql> flush privileges;
  62. 62. To update info already in a table. mysql> UPDATE [table name] SET Select_priv = 'Y',Insert_priv = 'Y',Update_priv = 'Y' where [field name] = 'user';
  63. 63. Delete a row(s) from a table. mysql> DELETE from [table name] where [field name] = 'whatever';
  64. 64. Update database permissions/privilages. mysql> flush privileges;
  65. 65. Delete a column. mysql> alter table [table name] drop column [column name];
  66. 66. Add a new column to db. mysql> alter table [table name] add column [new column name] varchar (20);
  67. 67. Change column name. mysql> alter table [table name] change [old column name] [new column name] varchar (50);
  68. 68. Make a unique column so you get no dupes. mysql> alter table [table name] add unique ([column name]);
  69. 69. Make a column bigger. mysql> alter table [table name] modify [column name] VARCHAR(3);
  70. 70. Delete unique from table. mysql> alter table [table name] drop index [colmn name];
  71. 71. Load a CSV file into a table. mysql> LOAD DATA INFILE '/tmp/filename.csv' replace INTO TABLE [table name] FIELDS TERMINATED BY ',' LINES TERMINATED BY ' ' (field1,field2,field3);
  72. 72. Dump all databases for backup. Backup file is sql commands to recreate all db's. # [mysql dir]/bin/mysqldump -u root -ppassword --opt >/tmp/alldatabases.sql
  73. 73. Dump one database for backup. # [mysql dir]/bin/mysqldump -u username -ppassword --databases databasename >/tmp/databasename.sql
  74. 74. Dump a table from a database. # [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql
  75. 75. Restore database (or database table) from backup. # [mysql dir]/bin/mysql -u username -ppassword databasename < /tmp/databasename.sql
  76. 76. Thank You