Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email:...

41
Santa Clara Univ ersity COEN 178 Holliday 1–1 COEN 178 Database Systems JoAnne Holliday Email: [email protected] (best way to reach me) Office: Engineering 247, (408) 551-1941 Office Hours: Mon,Tues 3:30-4:45 and by appointment Class web page: http://www.cse.scu.edu/~jholliday/ Two midterms, a project and a final exam T.A. – James Dong

Transcript of Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email:...

Page 1: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–1

COEN 178Database Systems

• JoAnne Holliday• Email: [email protected] (best way to reach me)• Office: Engineering 247, (408) 551-1941• Office Hours: Mon,Tues 3:30-4:45 and by

appointment• Class web page: http://www.cse.scu.edu/~jholliday/• Two midterms, a project and a final exam• T.A. – James Dong

Page 2: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–2

Textbooks and MeetingsRequired:• A First Course in Database Systems, by Ullman

and Widom (second edition), Prentice Hall, 2002.

Recommended: • Oracle 8 Programming, A Primer, (fourth

edition), by R. Sunderraman, Addison-Wesley Longman, 2000.

Class Meetings:• Lecture MWF 1:00-2:05pm, Engr 602• Lab: W or F 2:15-5:00 608B

Page 3: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–3

Grading

• Lab work and instructor discretion: 10% of grade.

• Project: 15% of grade.

• 2 Midterms for a total of 40% of grade.

• Last midterm: 35% of grade.

Page 4: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–4

Project

• You will build an application using a relational database system (Oracle8i).

• The project has 2 parts, starting with design and ending up with a complete application.

• The project should be written in Java and use the JDBC or written in Perl.

• Java is the preferred language.

Page 5: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–5

Why take this class?

• This class may seem easy, but there is a theoretical part which many students find difficult – don’t be caught sleeping!!

• The benefits of accepting data constraints.

• Of all courses you take at SCU, this may be the one that gets you a job.

Page 6: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–6

Schedule

• Today: Sept. 19 (M) Intro, Entity-Relationship Model. Read Chapter 1 and Sections 2.1-2.2.

• Sept 21 (W) Weak Entity Sets, Entity-Relationship Design. Read Sections 2.3-2.4.

• Sept 23 (F) Relational Model, Functional Dependencies. Read Sections 3.1-3.5.

Page 7: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–7

Syllabus• Chapter 1. Overview of database management

systems. (1)• Chapter 2. ERD’s, principles of modeling data and

constraints. (2)• Chapter 3. The relational data model, functional

dependencies and normal forms. (4)• Chapter 5 (5.1-5.2 only). Theoretical background

of the relational model. (1)• Chapter 6. Queries and Updates. (3)• Chapter 7. Constraints and Triggers (2)• Chapter 8 (sec 6,7) Transactions and Security. (2)• Indexing and the DBA (2)

Page 8: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–8

Definitions

• A database is a collection of (related) data.

• Database Management System (DBMS) is the hardware/software package that facilitates the creation and maintenance of a computerized database.

• In a relational database, the tables are relations, the rows are tuples and there are mathematically well defined operations on relations (relational algebra).

Page 9: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–9

Benefits of a DBMS

1. Manages very large amounts of data.2. Supports efficient access to data through indexing and

a rich query language.3. Supports concurrent access to data.

Example: bank and its ATM machines.

4. Supports secure, atomic access to very large amounts of data. Contrast two people editing the same UNIX file – last to write “wins” –

with the problem if two people deduct money from the same account via ATM machines at the same time – new balance is wrong whichever writes last.

Page 10: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–10

More Benefits of a DMBS

• Controls redundancy in data storage

• Protects against unauthorized access of data

• Enforces integrity constraints

• Provides backup and recovery services

• Enforces user-defined standards

• Provides data abstraction through schemas.

Page 11: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–11

Relational Model

• Based on tables, like the Account table:

acct # name balance

12345 Sally 1000.21

34567 Sue 285.48

… … …

• Today used in most DBMS's.

• DB schema Account=(acct#,name,balance) and DB instance

Page 12: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–12

The DBMS Marketplace• Relational DBMS companies – Oracle, Sybase – are among the largest

software companies in the world.

• IBM offers its relational DB2 system. With IMS, a nonrelational system, IBM is by some accounts the largest DBMS vendor in the world.

• Microsoft offers SQL-Server, plus Microsoft Access for the cheap DBMS on the desktop, answered by “lite” systems from other competitors.

• Relational companies also challenged by “object-oriented DB” companies.

• But countered with “object-relational” systems, which retain the relational core while allowing type extension as in OO systems.

Page 13: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–13

Three Aspects to Studying DBMS's1. Modeling and design of databases.

Allows exploration of issues before committing to an implementation.

2. Programming: queries and DB operations like update.

SQL = “intergalactic dataspeak.”

3. DBMS implementation. (concurrency control- 2PL, backup and recovery- logging, query optimization)

COEN178 = (1) + (2)

Page 14: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–14

Forming Queries

Department

Dept Manager

Data definition language (DDL) creates the schema and metadata

CREATE TABLE Account (…..);

Data Manipulation Language (DML)Query (SELECT), UPDATE, DELETE

In the SQL query language:

SELECT ManagerFROM DepartmentWHERE Department.Dept = ‘accounting’

Schema is

Department = (Dept,Manager)

Page 15: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–15

Host Languages C, C++, Java, Lisp

Application prog.

Local Vars

DBMS

Calls toDB

Host language is completely general (Turing complete)but gives you no support

Query language—less general "non procedural" andoptimizable

(Memory)

(Storage)

Page 16: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–16

Relational model is good for:

Large amounts of data —> simple operations

Navigate among small number of relations

Difficult Applications for relational model (complex objects and relationships:

• VLSI Design (CAD in general)

• CASE

• Graphical Data

CPUALU

ADDER

Adder

A

FA

ALU ADDER

Bill of Materials or transitive closure

Page 17: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–17

Other ModelsWhere number of "relations" is large, relationships are complex

•Object Data Model•Logic Data Model

OBJECT DATA MODEL

1. Complex Objects – Nested Structure (pointers or references)

2. Encapsulation, set of Methods/Access functions3. Object Identity4. Inheritance – Defining new classes like old classes

Object model: usually find objects via explicit navigationAlso query language in some systems

Page 18: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–18

LOGIC (Horn Clause) DATA MODEL• Prolog, Datalogif A1 and A2 then Bprolog B:- A1 and A2

Functions s(5) = 6 (successor)Predicates with Arguments sum(X,Y,Z) X + Y = Z

sum(X,0,X) means X + 0 = X (always true for all X)sum(X,s(Y),s(Z)):-sum(X,Y,Z)means X+(Y+1) = (Z+1) if X + Y = Z

More power than relational model

Can Compute Transitive Closureedge(X,Y)path(X,Y) :- edge(X,Y)path(X,Z) :- path(X,Y) & edge(Y,Z)

Page 19: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–19

Students

Entity/Relationship DiagramsA model where diagrams represent designs.• Entity like object, = “thing.”• Entity set like class = set of “similar”

entities/objects.• Attribute = property of entities in an entity set,

similar to fields of a struct.• In diagrams, entity set rectangle;

attribute oval.ID name phone

major

Page 20: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–20

Relationships

• Connect two or more entity sets.

• Represented by diamonds.

Students CoursesTaking

Page 21: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–21

Relationship Set

Think of the “value” of a relationship set as a table.

• One column for each of the connected entity sets.

• One row for each of the entities, one from each set, that are connected by the relationship.

Students Courses

Sally COEN178

Sally COEN111

Joe COEN178

… …

Page 22: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–22

Multiway RelationshipsUsually binary relationships (connecting two E.S.) suffice.• However, there are some cases where three or more E.S.

must be connected by one relationship.• Example: relationship among students, courses, TA's

(and graders).

Possibly, this E/R diagram is OK:

Students CoursesTaking

Assisting

TA/Graders

Page 23: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–23

Multiway Relationships

• Works in COEN178, because each TA (or grader) is a TA of all students. Connection student-TA is only via the course.

• But what if students were divided into sections, with different TAs? Then, a student in COEN178 would be related

to only one of the TA's. Which one?

• Need a 3-way relationship to tell.

Page 24: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–24

Students Courses TAsAnn CS180 JanSue CS180 PatBob CS180 Jan… … …

Students

Courses

TAs

Enrolls

Page 25: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–25

Beers-Bars-Drinkers Example

• Example of complex relationships.name addr license

name manf name addr

Beers Drinkers

BarsServes Frequents

Likes

Page 26: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–26

Multiplicity of Relationships

Representation of Many-One

• Arrow pointing to “at most one.”

• Rounded arrow = “exactly one.”

Many-many Many-one One-one

Page 27: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–27

Example:Drinkers Have Favorite Beers

name addr license

name manf name addr

Beers Drinkers

BarsServes Frequents

Likes

Favorite

Page 28: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–28

Many-One Relationships

A bar has one best seller – many bars may have the same one.

Is the rounded arrow justified?

Alternate notation: total participation

Bars BeersBest-seller

Bars BeersBest-seller

Page 29: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–29

Attributes on Relationships

• Shorthand for 3-way relationship:

Bars BeersSells

price

Bars BeersSells

price

Prices

Page 30: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–30

• A true 3-way relationship. Price depends jointly on beer and bar.

• Notice arrow convention for multiway relationships: “all other E.S. determine one of these.” Not sufficiently general to express any

possibility. However, if price, say, depended only on the

beer, then we could use two 2-way relationships: price-beer and beer-bar.

Or better: just make price an attribute of beer.

Page 31: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–31

Converting Multiway to 2-Way• Necessary in certain “object-oriented” models.

• Create a new connecting E.S. to represent rows of a relationship set. E.g., (Joe's Bar, Bud, $2.50) for the Sells relationship.

• Many-one relationships from the connecting E.S. to the others.

Bars Beers

The-Bar

Price

The-Beer

The-Price

BBP

Page 32: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–32

Roles

Sometimes an E.S. participates more than once in a relationship.

• Label edges with roles to distinguish.

Husband Wifed1 d2

d3 d4

… …Drinkers

Married

husband wife

Page 33: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–33

• Notice Buddies is symmetric, Married not. No way to say “symmetric” in E/R.

Design QuestionShould we replace husband and wife by one

relationship spouse to make it symmetric?

Buddy1 Buddy2d1 d2

d1 d3

d2 d1

d2 d4

… …Drinkers

Buddies

1 2

Page 34: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–34

More Design Issues

1. Subclasses.

2. Keys.

3. Weak entity sets.

Page 35: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–35

Subclasses

Subclass = special case = fewer entities = more properties.

• Example: A bank stores records of bank accounts. Savings accounts are a kind of bank account. In addition to the properties (= attributes and relationships) of bank accounts, there is a “interest rate” attribute for savings accounts.

Page 36: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–36

E/R Subclasses• Assume subclasses form a tree (no multiple

inheritance).• isa triangles indicate the subclass relation.

nameAcc#

Accounts

Savings a/crate

isa

Checking a/c

Page 37: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–37

Multiple InheritanceTheoretically, an E.S. could be a subclass of

several other entity sets.name Acc#

Bank account

owner

Interest instrument

SavingsA/C

isaisa

Certificate of deposit

Page 38: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–38

ProblemsHow to show?

• A customer can have several loans (or no loans) but each loan is associated with only one (exactly one) customer?

• A customer can have at most one loan and a loan may be ‘owned’ by several customers?

BorrowsCustomer Loan

Page 39: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–39

Keys

A key is a set of attributes whose values can belong to at most one entity.

• In E/R model, every E.S. must have a key. It could have more than one key, but one set of

attributes is the “designated” key.

• In E/R diagrams, you should underline all attributes of the designated key.

Page 40: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–40

Example• Suppose acc# is key for Account.

• acc# is also key for S/A. In general, key at root is key for all.

acc# nameAccount

S/Arate

isa

Page 41: Santa Clara UniversityCOEN 178 Holliday1–1 COEN 178 Database Systems JoAnne Holliday Email: jholliday@scu.edu (best way to reach me) Office: Engineering.

Santa Clara University COEN 178 Holliday 1–41

Example: A Multiattribute Key

• Possibly, the combination of hours + room also forms a key, but we have not designated it as such.

dept roomCourses

number hours