Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.



Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this


  1. 1. mysql
  2. 2. Contents of mysql <ul><li>Introduction </li></ul><ul><li>Commands </li></ul><ul><li>examples </li></ul>
  3. 3. Introduction to mysql <ul><li>MySQL is a powerful Relational Database Management System (RDBMS) which we will use to learn the basic principles of database and data manipulation using Structured Query Language (SQL) statements. SQL is a database language that is used to retrieve, insert, delete and update stored data. This is achieved by constructing conditional statements that conform to a specific syntax </li></ul><ul><li>How does mysql works? </li></ul><ul><li>MySQL is a database server program and as such is installed on one machine, but can 'serve' the database to a variety of locations </li></ul>
  4. 4. Mysql syntax <ul><li>mysql syntax: </li></ul><ul><li>The great thing about everything you do in MySQL is that the &quot;code&quot; is very easy for humans to read, as opposed to harder programming languages like C or C++. Very few special characters and symbols are required to create a MySQL query, </li></ul>
  5. 5. Mysql commands <ul><li>CREATE DATABASE </li></ul><ul><li>CREATE TABLE </li></ul><ul><li>INSERT </li></ul><ul><li>REPLACE </li></ul><ul><li>UPDATE </li></ul><ul><li>SELECT </li></ul><ul><li>DELETE </li></ul><ul><li>WHERE </li></ul><ul><li>IN </li></ul><ul><li>AND </li></ul><ul><li>OR </li></ul><ul><li>RLIKE </li></ul><ul><li>DISTINCT </li></ul><ul><li>VALUES </li></ul><ul><li>SET </li></ul><ul><li>MAX </li></ul>
  6. 6. Create table <ul><li>CREATE DATABASE: </li></ul><ul><li>CREATE DATABASE database_name ; </li></ul><ul><li>Will create a MySQL database. </li></ul><ul><li>Example: </li></ul><ul><li>mysql> CREATE TABLE users ( </li></ul><ul><li>-> id INT NOT NULL AUTO_INCREMENT, </li></ul><ul><li>-> name VARCHAR (50), </li></ul><ul><li>-> email VARCHAR (50), </li></ul><ul><li>-> PRIMARY KEY (id)); </li></ul><ul><li>Table created. </li></ul>
  7. 7. Primary key <ul><li>The Primary Key is a type of index MySQL uses. This index can do such things as; </li></ul><ul><li>Quickly find the rows that match a WHERE clause. </li></ul><ul><li>Retrieve rows from other tables when performing joins. Sort or group a table if the sorting or grouping is done on a leftmost prefix of a usable key </li></ul><ul><li>This can definitely help boost the speeds of your queries as well. </li></ul>
  8. 8. To add data <ul><li>Adding Data to a Table </li></ul><ul><li>Adding your Data to a table is not that hard of a process at all. Basically you specify what table you are inserting the values into, then you go ahead and do so. The syntax is as follows; </li></ul><ul><li>mysql> INSERT INTO users VALUES (&quot;NULL&quot;,&quot;BlairIreland&quot;,&quot; [email_address] &quot;); </li></ul><ul><li>If successful, you should get something like the following for a response; </li></ul><ul><li>Query Ok, 1 row affected (0.05 sec) </li></ul>
  9. 9. Viewing data <ul><li>Viewing Data: </li></ul><ul><li>After you add data to your table, you probably want to check it out to make sure everything went as planned. To do so, you would utilize the SELECT command. </li></ul><ul><li>To view all data in the table, you would use something like this; </li></ul><ul><li>mysql> SELECT * FROM users; </li></ul><ul><li>This will give you an output like this </li></ul><ul><li>2 rows is set. </li></ul>
  10. 10. To select a particular row <ul><li>To select a particular row in this database though, you would use this sort of command; </li></ul><ul><li>mysql> SELECT * FROM users WHERE (name=&quot;Blair Ireland&quot;); </li></ul><ul><li>This would give you </li></ul><ul><li>+----+---------------+---------------------------------+ </li></ul><ul><li>| id | name | email | </li></ul><ul><li>+----+---------------+---------------------------------+ </li></ul><ul><li>| 1 | Blair Ireland | | </li></ul><ul><li>+----+---------------+---------------------------------+ </li></ul>
  11. 11. To select specific column <ul><li>select specific columns, like this; mysql> select name from users; </li></ul><ul><li>+----------------+ </li></ul><ul><li>| name | </li></ul><ul><li>+----------------+ </li></ul><ul><li>| Blair Ireland | </li></ul><ul><li>| Mark Hardy | </li></ul><ul><li>+----------------+ </li></ul>
  12. 12. Modifying database <ul><li>Modifying Database Data </li></ul><ul><li>If you have data already in the database that needs some modifying, you would change it by utilizing the UPDATE command in mysql. </li></ul><ul><li>Its use is something like this; </li></ul><ul><li>mysql> UPDATE users SET email = '' </li></ul><ul><li>-> WHERE email = &quot; [email_address] &quot;; </li></ul><ul><li>This would just change all rows with email set to and change them to In this case though, only one entry has as its email, so only one entry would be changed. </li></ul>
  13. 13. Deleting data <ul><li>Deleting Database Data </li></ul><ul><li>If you want to remove data in the database, you would use MySQL's DELETE command. Its use would be as follows </li></ul><ul><li>mysql> DELETE FROM users WHERE (name=&quot;Mark Hardy&quot;); </li></ul><ul><li>This would delete Mark Hardy's entry in the database, leaving only Blair Ireland's entry in it. </li></ul>
  14. 14. <ul><li>Advanced commands: </li></ul><ul><li>sql> CREATE TABLE users ( </li></ul><ul><li>-> id INT NOT NULL AUTO_INCREMENT, </li></ul><ul><li>-> name VARCHAR (50), </li></ul><ul><li>-> email VARCHAR (50), </li></ul><ul><li>-> PRIMARY KEY (id)); </li></ul><ul><li>Table created. </li></ul>
  15. 15. Search command <ul><li>To do a general search, you would use the following syntax; </li></ul><ul><li>mysql> SELECT * FROM test WHERE </li></ul><ul><li>-> (name LIKE &quot;%B%&quot;); </li></ul><ul><li>This will result in finding anything with the capital letter B in the column name. Notice the two %'s used. This checks for anything before or after that letter. You can use just one if you like though. </li></ul><ul><li>You can place that % sign anywhere within the query though, as the search is based upon the placement of this character. </li></ul>
  16. 16. Order by command <ul><li>To use a literal wildcard character in your searches, you Order By </li></ul><ul><li>mysql> SELECT * FROM users WHERE </li></ul><ul><li>-> (name = &quot;Joe%&quot;) ORDER BY id DESC; </li></ul><ul><li>This will return all the records containing someone with the first name of Joe, and will output it from the greatest ID Number, descend until the lowest ID number is reached. </li></ul><ul><li>The default for ORDER BY is ascending, so if you want it to go by the lowest ID number first, you would just type in ORDER BY id, or you could plug in the ASC keyword where DESC is currently. Both would give you the same result. </li></ul>
  17. 17. Logical operators <ul><li>mysql> CREATE TABLE users ( </li></ul><ul><li>-> id INT NOT NULL AUTO_INCREMENT, </li></ul><ul><li>-> name VARCHAR (50), </li></ul><ul><li>-> email VARCHAR (50), </li></ul><ul><li>-> PRIMARY KEY (id)); </li></ul>
  18. 18. Not operator <ul><li>NOT (or) ! </li></ul><ul><li>mysql> SELECT * FROM users WHERE </li></ul><ul><li>-> (name != &quot;Blair Ireland&quot;); </li></ul><ul><li>or </li></ul><ul><li>mysql> SELECT * FROM users WHERE </li></ul><ul><li>-> (name NOT = &quot;Blair Ireland&quot;); </li></ul><ul><li>This query would return all records without Blair Ireland present as the name. </li></ul>
  19. 19. And operator <ul><li>AND (or) && </li></ul><ul><li>mysql> SELECT * FROM users WHERE </li></ul><ul><li>mysql> (name = &quot;Blair Ireland&quot;) AND mysql> (email = &quot;;); </li></ul><ul><li>or </li></ul><ul><li>mysql> SELECT * FROM users WHERE </li></ul><ul><li>-> (name = &quot;Blair Ireland&quot;) && </li></ul><ul><li>-> (email = &quot;;); </li></ul><ul><li>This query would return all records with Blair Ireland present as the name, and as the email. </li></ul>
  20. 20. Or operator <ul><li>mysql> SELECT * FROM test WHERE </li></ul><ul><li>-> (name = &quot;Blair Ireland&quot;) OR </li></ul><ul><li>-> (email = &quot;;); </li></ul><ul><li>or </li></ul><ul><li>mysql> SELECT * FROM test WHERE </li></ul><ul><li>-> (name = &quot;Blair Ireland&quot;) || </li></ul><ul><li>-> (email = &quot;;); </li></ul><ul><li>This query would return all records with Blair Ireland present as the name, or records with as the email. </li></ul>
  21. 21. Renaming and deleting an entire table <ul><li>Renaming a Table: </li></ul><ul><li>mysql> ALTER TABLE users RENAME public; </li></ul><ul><li>Deleting an entire table: </li></ul><ul><li>To delete (or drop) an entire table, you would use the following syntax; </li></ul><ul><li>mysql> DROP TABLE public; </li></ul><ul><li>If you would like to drop more tables at once though, you would do this; </li></ul><ul><li>mysql> DROP TABLE public, tests; </li></ul>
  22. 22. Remove and optimize table <ul><li>Remove a Column: </li></ul><ul><li>mysql> ALTER TABLE public DROP COLUMN time; </li></ul><ul><li>After you make these changes to the table, you may want to optimize the table afterwards (especially if you are using VARCHAR's, TEXT's or BLOB's, as this will optimize its memory allocation. You will also want to do it if you have deleted a large part of a table. </li></ul><ul><li>During a table optimization, the original table is available to clients, however, modifying and adding to the table is stalled until optimization is complete. </li></ul><ul><li>The syntax is: </li></ul><ul><li>OPTIMIZE TABLE table_name_goes_here </li></ul>