D A Week 5 T A B A S E SQL (Structures Querydonna-warren.com/SU-Classes/Relational Databases/Week 5...
Transcript of D A Week 5 T A B A S E SQL (Structures Querydonna-warren.com/SU-Classes/Relational Databases/Week 5...
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
SQL (Structures Query
Language) Part 1
Week 5
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
Topics for this Unit
• SQL statements
• Creating tables
• Inserting data
• Modifying data
• Deleting data
• Querying data
• Filter queries
• Sort queries
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
History
• SQL is the programming language used for
accessing and manipulating data and objects in
relational databases
• The first versions were developed by IBM in the
1970s
• SQL became an ANSI standard in 1986 and an
ISO standard in 1987
• A major revision to the standard occurred in 1992
• Additional modifications were made in 1999, 2003,
and 2006
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
SQL Overview
• SQL is a declarative language
• Procedural languages like C# or Java
describe how to accomplish a task step
by step
• In a declarative language you say what
you want to do, not how
• SQL is the language of relational
databases
• It is used for every aspect of database
development and management
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
SQL Functionality
• SQL is not normally case sensitive
• In some environments, SQL statements
must end with a semicolon
• SQL is usually divided into two broad
areas of functionality:
– DDL (Data Definition Language)
– DML (Data Manipulation Language)
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
SQL Statements
• DML - Data definition language (DDL)
– Create and modify database objects
– SELECT - UPDATE
– INSERT - DELETE
• Data manipulation language (DML)
– Query (retrieve) and modify data
– FROM - AS - GROUP BY
– WHERE - ORDER BY - LIKE
– HAVING - BETWEEN - NULL
– Logical Calculations - Arithmetic Calculations
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
SELECT
• SELECT identifies what you want
– * = All Columns
– Specific Column Names separated by
commas
• FROM clause identifies the table
• WHERE Limits the Rows Returned
SELECT ProjectID, Name, Department
FROM Ops.Project
WHERE Department = 'Finance‘
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
Where Clause
• The WHERE clause allows you to limit
the rows you return in a query
• You use the WHERE clause to specify
the criteria by which the rows will be
filtered
SELECT LastName, FirstName,
Phone, City
FROM Customer
WHERE City = 'Seattle'
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
Distinct Key Word • Sometimes a query will return multiple
duplicate values
• For instance, the statement
SELECT TutorKey
FROM Session
could return numerous instances of each
customer
• The DISTINCT keyword will make it so it only
returns one instance of each TutorKey
• The DISTINCT keyword always operates on
the whole row, not on individual columns
• It only returns distinct rows
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
INSERT
• Insert – allows you to add rows to a
table
INSERT INTO table
VALUES(value_list)
INSERT INTO table
(column_list)
VALUES(value_list)
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
UPDATE
• Update allows changes to be made but
is destructive and cannot be recovered
from
UPDATE EMPLOYEE
SET Phone = '360-287-8810'
WHERE EmployeeNumber = 11
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
DELETE
• Delete lets you remove records
DELETE FROM EMPLOYEES
WHERE EmployeeID='11'
Note: It is not possible to
delete a record with
dependencies because it
violates referential integrity
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
Sorting Results
• ORDER BY Specifies the sort order for the
results
• SELECT FirstName, LastName,
Phone, Department FROM EMPLOYEE
ORDER BY Department
• ORDER BY does an ascending A-Z, 1-10
etc. sort by default
• You can change the direction by using the
DESC keyword after the field to be sorted
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
Functions
• Functions always have the same basic syntax:
<function name>(function arguments)
• There are hundreds of built-in functions.
• We will be concerned with two broad types of functions:
– Scalar functions
– Aggregate functions
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
Scalar Functions
• Scalar functions operate on a single
row at a time
• Here is a list of common scalar
functions used Function Name Description
GETDATE() Returns current date and time
MONTH Returns the month as in integer (1 to 12)
from a Date value
YEAR Returns the Year as a four-digit integer
from a date value
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
Aggregate Functions
• Aggregate functions operate on multiple rows
at a time
• Here is a list of common aggregate functions
Aggregate
Function
Description
COUNT Counts the number of values : COUNT(*) counts all the rows.
COUNT(columnName) counts all the values in the column but
ignores nulls
SUM Sums or totals numeric values: SUM (InStock)
AVG Returns the mean average of a set of numeric values:
AVG(Price). By default nulls are ignored.
MAX Returns the highest value in a set of numeric or datetime
values: MAX(price)
MIN Returns the smallest value in a set of numeric or datetime
value: MIN(Price)
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
DISTINCT in Aggregate Functions
• You can use the DISTINCT keyword
with aggregate functions
• Doing so means the function will ignore
duplicate values in its calculation
SELECT COUNT(DISTINCT studentKey)
AS [Unduplicated]
FROM Session
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
Group By
• When a SELECT clause includes an aggregate
function and columns that are not a part of that
function, you must use the GROUP BY keywords
to group by each of the non-included columns
• This is necessary because you are mixing
functions that operate on multiple rows with
columns that refer to values in individual rows only
SELECT TutorKey, COUNT(SessionTimeKey)
AS [Total Sessions]
FROM Session
GROUP BY TutorKey
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
Having
• Used when there is an aggregate
function in the criteria of a query
SELECT TutorKey,
COUNT(SessionTimeKey) AS [Total
Sessions]
FROM Session
GROUP BY TutorKey
HAVING COUNT(SessionTimeKey)<4
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
Aliasing
• Used to make a more readable results
SELECT StudentLastName AS
[Last Name], StudentFirstName
AS [First Name]
FROM Student
• Double quotes “ “ can be used instead
of square brackets.
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
Other Criteria
• As well as equal, you can use other
operators for the criteria:
> GREATER THAN
< LESS THAN
>= GREATER THAN OR EQUAL
=< LESS THAN OR EQUAL
• Character and date values in the criteria
are quoted with single quotes
• Numerical values are not quoted
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
Like
• The LIKE keyword used in a WHERE
operator with a wildcard (% or _) allows
you to search for patterns in character
based fields.
• The following returns all items whose
name starts with "T".
SELECT ItemName, ItemPrice
FROM Inventory
WHERE ItemName LIKE 'T%'
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
Between
• The BETWEEN keyword can be used in
criteria to return values between two
other values
• BETWEEN is inclusive of its ends
SELECT TutorKey, SessionDate,
StudentKey
FROM Session
WHERE SessionDate BETWEEN
'11/1/2008' AND '11/30/2008'
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
Calculations • You can use keywords AND, OR, and NOT to combine
criteria in a query
• AND is exclusive. The following query returns no
results
WHERE City='Seattle' AND City='Portland'
• Or is inclusive. The following query returns all records
that have either Seattle or Portland for their city
WHERE City='Seattle' OR City='Portland'
• NOT excludes. The following query returns every city
except Portland
WHERE NOT City='Portland'
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
Null
• Nulls are special cases. They are not a
value and so cannot be compared to a
value using = or < or >.
• To locate nulls, you can use the IS
keyword in a criteria:
WHERE StudentKey IS NULL
WHERE StudentKey IS NOT NULL
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
Arithmetic Operators
Operator Description
* Multiplication
/ Division
+ Addition
- Subtraction
% Modulus (returns
the remainder in
integer division)
• The order of
operation is the
same as in algebra
using the pneumonic
Please Excuse My
Dear Aunt Sally
– Parenthesis
– Exponents
– Multiplication
– Division
– Addition
– Subtraction
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
Summary
• We have discussed the most common
SQL queries used to create business
reports such as
SELECT HAVING
INSERT BETWEEN
UPDATE NULL
ORDER BY WHERE
GROUP BY AS
LIKE CALCULATIONS
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
Lab 3: EasyPHP and MySQL Setup
• Install EasyPHP and MySQL and take a
screen shot that shows the MySQL prompt on
your screen
• An installation guide is in blackboard
• Research the capabilities of MySQL.
• Write a one to two (1-2) page paper in which
you:
• Describe your experiences related to your
setup of MySQL. Include any difficulties or
issues that you had encountered during the
installation
DPW © 2005-2010
DPW © Donna Warren
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
D
A
T
A
B
A
S
E
D
E
V
E
L
O
P
M
E
N
T
• Based on your post-installation research,
describe the main capabilities of MySQL
• Describe the approach that you would take to
go from a conceptual or logical model that
you created in Visio to the implementation of
that database structure in MySQL
• Determine the additional information that you
will need to implement the database design
in a database management system
• Include the screen shot which shows that
MySQL is installed on your computer
Lab 3: EasyPHP and MySQL Setup