18
MS3304: Week 7 Talking to Databases: SQL Basics

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

Embed Size (px)

Citation preview

Page 1: 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

MS3304: Week 7

Talking to Databases: SQL Basics

Page 2: 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

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

Page 3: 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

Review of dynamic web pagesURL Request - Sometimes including variables

Database

Server finds script/HTML templateAssociated 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

LanguageInterpreter

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

Page 4: 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

PHP to MySQL communication

Client Machine

Database

Template/Scripts

LanguageInterpreter

Web Server

Make the connection to the database

Compose SQL query

Send SQL query

Page 5: 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

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?

Page 6: 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

Sample database of MS3304 students:Useful information1. All info about all students2. All info for a specific student3. Attendance sheet for the lecture4. Attendance sheet for workshops 5. Marks sheet for a group project6. Marks report sheet for the office7. List students resitting8. All student numbers and marks for IT

students in

Page 7: 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

Sample database of MS3304 students:field names and data types

firstName varcharsurname varcharstudentNumber varchargroup intcw1 intcw2 intaverage intIT_prog tinyintMM_prog tinyintteam varchar

Page 8: 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

Query information needed

• Name(s) of field(s) that we want information returned from

• Criteria for the search• Order to sort results in

Page 9: 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

Query information: your turn

Queries1. All info about all students2. All info for a specific

student3. Lecture attendance sheet4. Workshop attendance

sheet 5. Team marks sheet6. Final marks for office7. List students resitting

Database fieldsfirstNamesurnamestudentNumbergroupcw1cw2averageteamIT_progMM_prog

State the fields returned, the fields used for searching criteria, sort order, and criteria statement for each of the above.

Page 10: 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

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

Page 11: 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

Writing an query string: basic SQL syntax

SELECT which columns FROM which tableWHERE conditionsORDER BY fields to sort by

Page 12: 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

My SQL operators

Comparison and Logical Operators= equals!= not equal to< less than> greater than<= less than or equal to>= greater than or equal toOR where at least one of the conditions is

trueAND where all conditions are trueNOT where the condition is not true

Page 13: 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

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

Page 14: 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

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'

Page 15: 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

SQL expression strings: your turnSQL operators/wildcards= equals

!= not equal to< less than> greater than<= less than or equal to>= greater than or equal

toOR logical ORAND logical ANDNOT logical NOTLIKE string comparison* Wildcard for all

SQL Statement Syntax

SELECT which columns FROM which tableWHERE conditionsORDER BY fields to sort by

Write the SQL query string for the seven queries previously given.

The name of the table: students

Page 16: 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

Advanced Topic: Other SQL terms

IS NULL has a valueIS NOT NULL has no valueBETWEEN within a rangeNOT BETWEEN not within a range

Page 17: 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

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)

Page 18: 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

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