MySQL basics


Published on

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

MySQL basics

  1. 1. MySQL BasicsJamshid HashimiTrainer, Cresco Solution WorkforceDevelopment Program
  2. 2. Agenda• Introduction MySQL• Simple Select• MySQL Connect• Comments• Whitespace and Semi-colons• Case Sensitivity• SELECTing All Columns in All Rows• SELECTing Specific Columns• Sorting Records• Sorting By a Single Column• Sorting By Multiple Columns• Sorting By Column Position
  3. 3. Agenda• Ascending and Descending Sorts• The WHERE Clause and Operator Symbols• Checking for Equality• Checking for Inequality• Checking for Greater or Less Than• Checking for NULL• The WHERE Clause and Operator Words• The BETWEEN Operator• The IN Operator• The LIKE Operator• The NOT Operator• Checking Multiple Conditions• AND, OR, Order of Evaluation
  4. 4. Introduction MySQL• MySQL is a fast, easy-to-use RDBMS used being used for many smalland big businesses. MySQL was owned and sponsored by a singlefor-profit firm, the Swedish company MySQL AB, now owned byOracle Corporation. MySQL is becoming so popular because ofmany good reasons.• MySQL is released under an open-source license. So you havenothing to pay to use it.• MySQL is a very powerful program in its own right. It handles alarge subset of the functionality of the most expensive andpowerful database packages.• MySQL uses a standard form of the well-known SQL data language.• MySQL works on many operating systems and with many languagesincluding PHP, PERL, C, C++, JAVA etc.
  5. 5. Introduction to MySQL• MySQL works very quickly and works well even withlarge data sets.• MySQL is very friendly to PHP, the most appreciatedlanguage for web development.• MySQL supports large databases, up to 50 million rowsor more in a table. The default file size limit for a tableis 4GB, but you can increase this (if your operatingsystem can handle it) to a theoretical limit of 8 millionterabytes (TB).• MySQL is customizable. The open source GPL licenseallows programmers to modify the MySQL software tofit their own specific environments.
  6. 6. Simple SELECT• A query is a question or a requestSELECT last_name FROM employees
  7. 7. MySQL Connect• Use the PHP mysqli_connect() function toopen a new connection to the MySQL server.mysqli_connect(host,username,password,dbname);
  8. 8. MySQL Connect<?php$connection = mysqli_connect("localhost","root","","awd_db");// Check connectionif (mysqli_connect_errno($connection)){echo "Failed to connect to MySQL: " . mysqli_connect_error();}mysqli_close($connection);
  9. 9. Comments# this is a comment-- This is also a comment/*Thisis acomment*/SELECT * FROM cms_news;
  10. 10. Whitespace and Semi-colonsDEMO
  11. 11. Case Sensitivity• Although database, table, and trigger namesare not case sensitive on some platforms, youshould not refer to one of these usingdifferent cases within the same statement.SELECT * FROM my_table WHERE MY_TABLE.col=1;
  12. 12. Case Sensitivity• Column, index, stored routine, and eventnames are not case sensitive on any platform,nor are column aliases.• By default, table aliases are case sensitive onUnix, but not so on Windows or Mac OS X. Thefollowing statement would not work on Unix,because it refers to the alias both as a and asA:SELECT col_name FROM tbl_name AS a WHEREa.col_name = 1 OR A.col_name = 2;
  13. 13. SELECTing All Columns in All Rows• SELECT SyntaxSELECT expressions_and_columns FROM table_name[WHERE some_condition_is_true][ORDER BY some_column [ASC | DESC]][LIMIT offset, rows]SELECT * FROM tbl_name;
  14. 14. SELECTing Specific Columns• If you do not want to see entire rows fromyour table, just name the columns in whichyou are interested, separated by commas.SELECT name_id, firstname, lastname FROMtbl_name;
  15. 15. Sorting Records• By default, results of SELECT queries areordered as they appear in the table. If youwant to order results a specific way, such as bydate, ID, name, and so on, specify yourrequirements using the ORDER BY clause.SELECT name_id, firstname, lastname FROMtbl_name ORDER BY lastname;
  16. 16. Sorting By a Single Column• The default sorting of ORDER BY results isascending (ASC); strings sort from A to Z,integers start at 0, dates sort from oldest tonewest. You can also specify a descendingsort, using DESC.SELECT name_id, firstname, lastname FROMtbl_name ORDER BY lastname DESC;
  17. 17. Sorting By Multiple Columns• Youre not limited to sorting by just onefield—you can specify as many fields as youwant, separated by commas. The sortingpriority is by list order, so if you use ORDER BYlastname, firstname, the results will be sortedby lastname, then by firstname.SELECT name_id, firstname, lastname FROMtbl_name ORDER BY lastname, firstname;
  18. 18. Sorting by Column Position• Columns selected for output can be referredto in ORDER BY and GROUP BY clauses usingcolumn names, column aliases, or columnpositions. Column positions are integers andbegin with 1SELECT name_id, firstname, lastname FROMtbl_name ORDER BY 2;
  19. 19. The WHERE Clause and OperatorSymbols• We can use a conditional clause called WHERE clauseto filter out results. Using WHERE clause we canspecify a selection criteria to select required recordsfrom a table.• You can use one or more tables separated by commato include various condition using a WHERE clause. ButWHERE clause is an optional part of SELECT command.• You can specify any condition using WHERE clause.• You can specify more than one conditions using AND orOR operators.• A WHERE clause can be used along with DELETE orUPDATE SQL command also to specify a condition.
  20. 20. The WHERE Clause and OperatorSymbols
  21. 21. The WHERE Clause and OperatorSymbolsSELECT * FROM cms_news WHERE `id` != 1;SELECT * FROM cms_news WHERE `id` >= 1;
  22. 22. Checking for EqualityDEMO
  23. 23. Checking for InequalityDEMO
  24. 24. Checking for Greater or Less ThanDEMO
  25. 25. Checking for NULL• IS NULL: operator returns true of column valueis NULL.• IS NOT NULL: operator returns true of columnvalue is not NULL.SELECT * FROM cms_news WHERE title IS NULL;SELECT * FROM cms_news WHERE title IS NOTNULL;
  26. 26. The WHERE Clause and OperatorWordsSELECT * FROM cms_news WHERE `id` = 1 OR `id` = 2;SELECT * FROM cms_news WHERE `id` = 1 AND`status` = "a”;
  27. 27. The BETWEEN Operator• The BETWEEN operator allows you to specify arange to test.SELECT productCode,productName,buyPriceFROM productsWHERE buyPrice BETWEEN 90 AND 100
  28. 28. The IN Operator• The IN operator allows you to determine if aspecified value matches any one of a list or asubquery.SELECT column_listFROM table_nameWHERE (expr|column) IN(value1,value2,...)SELECT officeCode, city, phoneFROM officesWHERE country IN (USA,France)
  29. 29. The LIKE Operator• The MySQL LIKE operator is commonly used toselect data based on patterns matching. Usingthe LIKE operator in appropriate way isessential to increase the query performance.– The percentage ( %) wildcard allows you to matchany string of zero or more characters.– The underscore (_) wildcard allows you to matchany single character.
  30. 30. The LIKE OperatorSELECT employeeNumber, lastName, firstNameFROM employeesWHERE firstName LIKE a%SELECT employeeNumber, lastName, firstNameFROM employeesWHERE lastname LIKE %on%SELECT employeeNumber, lastName, firstNameFROM employeesWHERE firstname LIKE T_m
  31. 31. DEMO
  32. 32. QUESTIONS?