(c) 2000, the university of michigan 1 database application design handout #5 february 4, 2000

27
(C) 2000, The Un iversity of Mich igan 1 Database Application Design Handout #5 February 4, 2000

Upload: sydney-barker

Post on 13-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

1

Database Application Design

Handout #5

February 4, 2000

Page 2: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

2

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

Page 3: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

3

Foundations of relational implementation

(cont’d)

Page 4: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

4

Expressing queries in relational algebra

Type Format Example

Set operations +, -, intersection,union, product

STUDENT [Name] – JUNIOR [Name]

Selection Relation WHEREcondition

CLASS WHERE Name = ‘A’

Projection Relation [list ofattributes]

STUDENT [Name, Major, Age]

Join (Inner join) Relation1 JOIN(condition) Relation2

STUDENT JOIN (SID =StudentNumber) ENROLLMENT

Outer join Relation1 LEFTOUTER JOIN(condition) Relation2

STUDENT LEFT OUTER JOIN (SID= StudentNumber) ENROLLMENT

Page 5: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

5

Examples

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

STUDENT

Page 6: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

6

Examples (Cont’d)

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

ENROLLMENT

Page 7: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

7

Examples (Cont’d)

Name Time Room

BA200 M-F9 SC110

BD445 MWF3 SC213

BF410 MWF8 SC213

CS150 MWF3 EA304

CS250 NWF12 EB210

CLASS

Page 8: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

8

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)

Page 9: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

9

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?

Page 10: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

10

Structured Query Language (SQL)

Page 11: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

11

SQL Overview

• ANSI Standard

• Multitude of implementations

• SQL92 (ANSI)

• Not a programming language!

• Two major modes of use: embedded and interactive

Page 12: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

12

Projections in SQL

SELECT SID, Name, MajorFROM STUDENT

SELECT MajorFROM STUDENT

SELECT DISTINCT MajorFROM STUDENT

Page 13: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

13

Selections in SQL

SELECT SID, Name, Major, GradeLevel, AgeFROM STUDENTWHERE Major =‘MATH’

SELECT *FROM STUDENTWHERE Major =‘MATH’

SELECT *FROM STUDENTWHERE Major =‘MATH’ AND Age > 21

Page 14: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

14

Selections in SQL (Cont’d)

• Set values for the condition (IN, NOT IN)

• Ranges (BETWEEN)

• LIKE

• Wild cards (%, _)

• IS NULL

Page 15: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

15

Sorting in SQL

• ORDER BY

• ASC, DESC

Page 16: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

16

Built-in functions

• COUNT, SUM, AVG, MAX, MIN

• SELECT COUNT(*)FROM STUDENT

• SELECT COUNT(Major)FROM STUDENT

Page 17: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

17

Grouping

• GROUP BY

• HAVING

• Example:SELECT Major, COUNT(*)

FROM STUDENTGROUP BY MajorHAVING COUNT(*) > 2

• Ordering (WHERE is computed first)

Page 18: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

18

Subqueries

SELECT NameFROM STUDENTWHERE SID IN

(SELECT StudentNumber FROM ENROLLMENT WHERE ClassName = ‘BD445’)

Page 19: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

19

Joins in SQL

• Using more than one table in a SELECT

• Comparing subqueries and joins– not equivalent

• Outer joins (ANSI vs. Access)

Page 20: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

20

EXISTS and NOT EXISTS

SELECT DISTINCT StudentNumberFROM ENROLLMENT AWHERE EXISTS

(SELECT * FROM ENROLLMENT B WHERE A.StudentNumber =

B.StudentNumber AND A.ClassName NOT = B.ClassName)

Page 21: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

21

EXISTS and NOT EXISTS (Cont’d)

SELECT Student.NameFROM STUDENTWHERE NOT EXISTS

(SELECT * FROM ENROLLMENT WHERE NOT EXISTS

(SELECT * FROM CLASS WHERE CLASS.Name = ENROLLMENT.ClassName AND ENROLLMENT.StudentNumber =

STUDENT.SID))

Page 22: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

22

Inserting dataINSERT INTO ENROLLMENT

VALUES (400,’BD445’,44)

INSERT INTO ENROLLMENT(StudentNumber,ClassName)VALUES (400,’BD445)

INSERT INTO JUNIORVALUES(SELECT SID, Name, MajorFROM STUDENTWHERE GradeLevel = ‘JR’)

Page 23: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

23

Deleting data

DELETE STUDENTWHERE STUDENT.SID = 100

DELETE ENROLLMENTWHERE ENROLLMENT.StudentNumber IN

(SELECT STUDENT.SID FROM STUDENT WHERE STUDENT.Major = ‘Accounting’)

DELETE STUDENTWHERE Student.Major = ‘Accounting’

Ordering!

Page 24: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

24

Updating data

UPDATE ENROLLMENTSET PositionNumber = 44WHERE SID = 400

UPDATE ENROLLMENTSET PositionNumber = MAX (PositionNumber) + 1WHERE SID = 400

Page 25: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

25

Database design using E-R Models

Page 26: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

26

E-R Design

• Transforming user requirements represented using E-R models into relational database designs

Page 27: (C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000

(C) 2000, The University of Michigan

27

Readings for next time

• Kroenke– Chapter 10: Database application design

• YRK (optional)– Chapter 8: Database application architectures

– Chapter 9: CGI programming