Upload
ophelia-payne
View
216
Download
3
Embed Size (px)
Citation preview
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
statements for mySQL• Advanced topic: preview of
advanced SQL commands
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
PHP to MySQL communication
Client Machine
Database
Template/Scripts
LanguageInterpreter
Web Server
Make the connection to the database
Compose SQL query
Send SQL query
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?
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
Sample database of MS3304 students:field names and data types
firstName varcharsurname varcharstudentNumber varchargroup intcw1 intcw2 intaverage intIT_prog tinyintMM_prog tinyintteam varchar
Query information needed
• Name(s) of field(s) that we want information returned from
• Criteria for the search• Order to sort results in
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.
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
Writing an query string: basic SQL syntax
SELECT which columns FROM which tableWHERE conditionsORDER BY fields to sort by
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
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
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'
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
Advanced Topic: Other SQL terms
IS NULL has a valueIS NOT NULL has no valueBETWEEN within a rangeNOT BETWEEN not within a range
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)
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