Database Application Design
Download
Report
Transcript Database Application Design
February 4, 2000
Database Application Design
Handout #5
(C) 2000, The
University of Michigan
1
Course information
•
•
•
•
•
•
Instructor: Dragomir R. Radev ([email protected])
Office: 305A, West Hall
Phone: (734) 615-5225
Office hours: Thursdays 3-4 and Fridays 1-2
Course page: http://www.si.umich.edu/~radev/654w00
Class meets on Fridays, 2:30 - 5:30 PM, 311 WH
(C) 2000, The
University of Michigan
2
Foundations of relational
implementation
(cont’d)
(C) 2000, The
University of Michigan
3
Expressing queries in relational
algebra
Type
Format
Set operations
+, -, intersection,
union, product
Selection
Relation WHERE
condition
Projection
Relation [list of
attributes]
Join (Inner join) Relation1 JOIN
(condition) Relation2
Outer join
Relation1 LEFT
OUTER JOIN
(condition) Relation2
(C) 2000, The
University of Michigan
Example
STUDENT [Name] – JUNIOR [Name]
CLASS WHERE Name = ‘A’
STUDENT [Name, Major, Age]
STUDENT JOIN (SID =
StudentNumber) ENROLLMENT
STUDENT LEFT OUTER JOIN (SID
= StudentNumber) ENROLLMENT
4
Examples
STUDENT
SID
Name
Major
GradeLevel
Age
100
JONES
HISTORY
GR
21
150
PARKS
ACCOUNTING SO
19
200
BAKER
MATH
GR
50
250
GLASS
HISTORY
SN
50
300
BAKER
ACCOUNTING SN
41
350
RUSSELL MATH
JR
20
400
RYE
ACCOUNTING FR
18
450
JONES
HISTORY
SN
24
(C) 2000, The
University of Michigan
5
Examples (Cont’d)
ENROLLMENT
StudentNumber
ClassName PositionNumber
100
BD445
1
150
BA200
1
200
BD445
2
200
CS250
1
300
CS150
1
400
BA200
2
400
BF410
1
400
CS250
2
450
BA200
3
(C) 2000, The
University of Michigan
6
Examples (Cont’d)
CLASS
Name
Time
Room
BA200
M-F9
SC110
BD445
MWF3
SC213
BF410
MWF8
SC213
CS150
MWF3
EA304
CS250
NWF12
EB210
(C) 2000, The
University of Michigan
7
Examples (Cont’d)
Full set of relations:
JUNIOR (Snum, Name, Major)
HONOR-STUDENT (Number, Name, Interest)
STUDENT (SID, Name, Major, GradeLevel, Age)
CLASS (Name, Time, Room)
ENROLLMENT (StudentNumber, ClassName,
PositionNumber)
FACULTY (FID, Name, Department)
(C) 2000, The
University of Michigan
8
Examples (Cont’d)
• What are the names of all students?
• What are the student numbers of all students enrolled in a class?
• What are the student numbers of all students not enrolled in a
class?
• What are the numbers of students enrolled in the class
‘BD445’?
• What are the names of the students enrolled in class ‘BD445’?
• What are the names and meeting times of ‘PARKS’ classes?
• Wwhat are the grade levels and meeting rooms of all students,
including students not enrolled in a class?
(C) 2000, The
University of Michigan
9
Structured Query Language
(SQL)
(C) 2000, The
University of Michigan
10
SQL Overview
•
•
•
•
•
ANSI Standard
Multitude of implementations
SQL92 (ANSI)
Not a programming language!
Two major modes of use: embedded and
interactive
(C) 2000, The
University of Michigan
11
Projections in SQL
SELECT SID, Name, Major
FROM STUDENT
SELECT Major
FROM STUDENT
SELECT DISTINCT Major
FROM STUDENT
(C) 2000, The
University of Michigan
12
Selections in SQL
SELECT SID, Name, Major, GradeLevel, Age
FROM STUDENT
WHERE Major =‘MATH’
SELECT *
FROM STUDENT
WHERE Major =‘MATH’
SELECT *
FROM STUDENT
WHERE Major =‘MATH’ AND Age > 21
(C) 2000, The
University of Michigan
13
Selections in SQL (Cont’d)
•
•
•
•
•
Set values for the condition (IN, NOT IN)
Ranges (BETWEEN)
LIKE
Wild cards (%, _)
IS NULL
(C) 2000, The
University of Michigan
14
Sorting in SQL
• ORDER BY
• ASC, DESC
(C) 2000, The
University of Michigan
15
Built-in functions
• COUNT, SUM, AVG, MAX, MIN
• SELECT COUNT(*)
FROM STUDENT
• SELECT COUNT(Major)
FROM STUDENT
(C) 2000, The
University of Michigan
16
Grouping
• GROUP BY
• HAVING
• Example:
SELECT Major, COUNT(*)
FROM STUDENT
GROUP BY Major
HAVING COUNT(*) > 2
• Ordering (WHERE is computed first)
(C) 2000, The
University of Michigan
17
Subqueries
SELECT Name
FROM STUDENT
WHERE SID IN
(SELECT StudentNumber
FROM ENROLLMENT
WHERE ClassName = ‘BD445’)
(C) 2000, The
University of Michigan
18
Joins in SQL
• Using more than one table in a SELECT
• Comparing subqueries and joins
– not equivalent
• Outer joins (ANSI vs. Access)
(C) 2000, The
University of Michigan
19
EXISTS and NOT EXISTS
SELECT DISTINCT StudentNumber
FROM ENROLLMENT A
WHERE EXISTS
(SELECT *
FROM ENROLLMENT B
WHERE A.StudentNumber = B.StudentNumber
AND A.ClassName NOT = B.ClassName)
(C) 2000, The
University of Michigan
20
EXISTS and NOT EXISTS
(Cont’d)
SELECT Student.Name
FROM STUDENT
WHERE NOT EXISTS
(SELECT *
FROM ENROLLMENT
WHERE NOT EXISTS
(SELECT *
FROM CLASS
WHERE CLASS.Name = ENROLLMENT.ClassName
AND ENROLLMENT.StudentNumber = STUDENT.SID))
(C) 2000, The
University of Michigan
21
Inserting data
INSERT INTO ENROLLMENT
VALUES (400,’BD445’,44)
INSERT INTO ENROLLMENT
(StudentNumber,ClassName)
VALUES (400,’BD445)
INSERT INTO JUNIOR
VALUES
(SELECT SID, Name, Major
FROM STUDENT
WHERE GradeLevel = ‘JR’)
(C) 2000, The
University of Michigan
22
Deleting data
DELETE STUDENT
WHERE STUDENT.SID = 100
DELETE ENROLLMENT
WHERE ENROLLMENT.StudentNumber IN
(SELECT STUDENT.SID
FROM STUDENT
WHERE STUDENT.Major = ‘Accounting’)
DELETE STUDENT
WHERE Student.Major = ‘Accounting’
Ordering!
(C) 2000, The
University of Michigan
23
Updating data
UPDATE ENROLLMENT
SET PositionNumber = 44
WHERE SID = 400
UPDATE ENROLLMENT
SET PositionNumber = MAX (PositionNumber) + 1
WHERE SID = 400
(C) 2000, The
University of Michigan
24
Database design using E-R
Models
(C) 2000, The
University of Michigan
25
E-R Design
• Transforming user requirements represented
using E-R models into relational database
designs
(C) 2000, The
University of Michigan
26
Readings for next time
• Kroenke
– Chapter 10: Database application design
• YRK (optional)
– Chapter 8: Database application architectures
– Chapter 9: CGI programming
(C) 2000, The
University of Michigan
27