Presentation is loading. Please wait.

Presentation is loading. Please wait.

MS3304: Week 7 Talking to Databases: SQL Basics. Overview Connecting to a database Querying a database Basic SQL syntax for mySQL Writing simple and complex.

Similar presentations


Presentation on theme: "MS3304: Week 7 Talking to Databases: SQL Basics. Overview Connecting to a database Querying a database Basic SQL syntax for mySQL Writing simple and complex."— Presentation transcript:

1 MS3304: Week 7 Talking to Databases: SQL Basics

2 Overview Connecting to a database Querying a database Basic SQL syntax for mySQL Writing simple and complex SQL statements for mySQL Advanced topic: preview of advanced SQL commands

3 Review of dynamic web pages URL Request - Sometimes including variables Database Server finds script/HTML template Associated with URL Script/HTML template is passed to interpreter that executes the script instructions This HTML page is returned to the client via the web server Language Interpreter Client Machine   Template/Scripts Web Server  Interpreter plugs data into the HTML template according to script instructions to create an HTML page on the fly Script requests data from the database which is returned to the interpreter

4 PHP to MySQL communication Client Machine  Database  Template/Scripts Language Interpreter Web Server  Make the connection to the database Compose SQL query Send SQL query

5 Composing queries: Sample database of MS3304 students What kinds of information would be useful? What data would need to be in the database? How would we ask for this information?

6 Sample database of MS3304 students: Useful information 1.All info about all students 2.All info for a specific student 3.Attendance sheet for the lecture 4.Attendance sheet for workshops 5.Marks sheet for a group project 6.Marks report sheet for the office 7.List students resitting 8.All student numbers and marks for IT students in

7 Sample database of MS3304 students: field names and data types firstNamevarchar surnamevarchar studentNumbervarchar groupint cw1int cw2int averageint IT_progtinyint MM_progtinyint teamvarchar

8 Query information needed Name(s) of field(s) that we want information returned from Criteria for the search Order to sort results in

9 Query information: your turn Queries 1.All info about all students 2.All info for a specific student 3.Lecture attendance sheet 4.Workshop attendance sheet 5.Team marks sheet 6.Final marks for office 7.List students resitting Database fields firstName surname studentNumber group cw1 cw2 average team IT_prog MM_prog State the fields returned, the fields used for searching criteria, sort order, and criteria statement for each of the above.

10 What is SQL? SQL (often pronounced “sequel”) stands for Structured Query Language A set of special reserved words organised in a specific order used exclusively for communicating with a database All major databases use SQL although there can be slight differences in the syntax

11 Writing an query string: basic SQL syntax SELECT which columns FROM which table WHERE conditions ORDER BY fields to sort by

12 My SQL operators Comparison and Logical Operators =equals !=not equal to <less than >greater than <=less than or equal to >=greater than or equal to ORwhere at least one of the conditions is true ANDwhere all conditions are true NOTwhere the condition is not true

13 LIKE and wildcards for comparing strings LIKE is used mainly to compare strings –Used in conjunction with two wildcard characters: Percentage sign (%) matches zero or more characters Underscore (_) matches a single character –LIKE and is case insensitive The star character (*) is used as a wildcard to denote all

14 SQL example statements It is possible to write queries with set values SELECT * FROM students WHERE firstName = 'Bob' SELECT * FROM students WHERE average > 70 ORDER BY studentNumber It is possible to use PHP variables in SQL queries SELECT * FROM students WHERE surname = '$surname' AND firstName = '$firstName'

15 SQL expression strings: your turn SQL operators/wildcards =equals !=not equal to <less than >greater than <=less than or equal to >=greater than or equal to ORlogical OR ANDlogical AND NOTlogical NOT LIKEstring comparison *Wildcard for all SQL Statement Syntax SELECT which columns FROM which table WHERE conditions ORDER BY fields to sort by Write the SQL query string for the seven queries previously given. The name of the table: students

16 Advanced Topic: Other SQL terms IS NULLhas a value IS NOT NULLhas no value BETWEENwithin a range NOT BETWEENnot within a range

17 Advanced Topic: Other uses of SQL Add a new record to a database Update an existing record in a database Carry out complex multi-table queries (called joins)

18 Overview PHP passes information to the database in order to make a connection Once a connection exists SQL statements are sent from PHP to allow interaction with the database SQL has a specific syntax that may vary slightly depending on the DB


Download ppt "MS3304: Week 7 Talking to Databases: SQL Basics. Overview Connecting to a database Querying a database Basic SQL syntax for mySQL Writing simple and complex."

Similar presentations


Ads by Google