students - Personal Home Pages (at UEL)
Download
Report
Transcript students - Personal Home Pages (at UEL)
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 pages
URL Request Sometimes
including variables
:
Client
Machine
Web Server
This HTML
page is
returned to
the client
via the web
server
Server finds script/HTML
template
Associated with URL
Template/Scripts
Interpreter plugs data into the
HTML template according to
script instructions to create an
HTML page on the fly
Language
Interpreter
Database
Script/HTML template
is passed to interpreter
that executes the script
instructions
Script requests data from the
database which is returned to
the interpreter
PHP to MySQL communication
:
Client
Machine
Web Server
Template/Scripts
Language
Interpreter
Database
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 information
1.
2.
3.
4.
5.
6.
7.
8.
All info about all students
All info for a specific student
Attendance sheet for the lecture
Attendance sheet for workshops
Marks sheet for a group project
Marks report sheet for the office
List students resitting
All student numbers and marks for IT
students in
Sample database of MS3304 students:
field names and data types
firstName
surname
studentNumber
group
cw1
cw2
average
IT_prog
MM_prog
team
varchar
varchar
varchar
int
int
int
int
tinyint
tinyint
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
Queries
1.
2.
3.
4.
5.
6.
7.
All info about all students
All info for a specific
student
Lecture attendance sheet
Workshop attendance sheet
Team marks sheet
Final marks for office
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.
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 table
WHERE conditions
ORDER BY fields to sort by
My SQL operators
Comparison and Logical Operators
=
!=
<
>
<=
>=
OR
AND
NOT
equals
not equal to
less than
greater than
less than or equal to
greater than or equal to
where at least one of the conditions is true
where all conditions are true
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 turn
SQL operators/wildcards
=
equals
!=
not equal to
<
less than
>
greater than
<=less than or equal to
>=greater than or equal to
OR
logical OR
AND logical AND
NOT logical NOT
LIKE
string 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
Advanced Topic: Other SQL terms
IS NULL
IS NOT NULL
BETWEEN
NOT BETWEEN
has a value
has no value
within a range
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