• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
MySQL basics
 

MySQL basics

on

  • 968 views

 

Statistics

Views

Total Views
968
Views on SlideShare
968
Embed Views
0

Actions

Likes
4
Downloads
86
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    MySQL basics MySQL basics Presentation Transcript

    • MySQL BasicsJamshid HashimiTrainer, Cresco Solutionhttp://www.jamshidhashimi.comjamshid@netlinks.af@jamshidhashimiajamshidhashimiAfghanistan WorkforceDevelopment Program
    • 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
    • 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
    • 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.
    • 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.
    • Simple SELECT• A query is a question or a requestSELECT last_name FROM employees
    • MySQL Connect• Use the PHP mysqli_connect() function toopen a new connection to the MySQL server.mysqli_connect(host,username,password,dbname);
    • 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);
    • Comments# this is a comment-- This is also a comment/*Thisis acomment*/SELECT * FROM cms_news;
    • Whitespace and Semi-colonsDEMO
    • 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;
    • 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;
    • 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;
    • 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;
    • 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;
    • 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;
    • 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;
    • 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;
    • 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.
    • The WHERE Clause and OperatorSymbols
    • The WHERE Clause and OperatorSymbolsSELECT * FROM cms_news WHERE `id` != 1;SELECT * FROM cms_news WHERE `id` >= 1;
    • Checking for EqualityDEMO
    • Checking for InequalityDEMO
    • Checking for Greater or Less ThanDEMO
    • 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;
    • The WHERE Clause and OperatorWordsSELECT * FROM cms_news WHERE `id` = 1 OR `id` = 2;SELECT * FROM cms_news WHERE `id` = 1 AND`status` = "a”;
    • The BETWEEN Operator• The BETWEEN operator allows you to specify arange to test.SELECT productCode,productName,buyPriceFROM productsWHERE buyPrice BETWEEN 90 AND 100
    • 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)
    • 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.
    • 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
    • DEMO
    • QUESTIONS?