slides01 database systems

Upload: trasdamad

Post on 10-Apr-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 Slides01 Database Systems

    1/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 11

    Database Systems

    Lecture/Workshop: Mon 1-3 LIB 1316; Wed 11 12, LH2

    Lab: Thu 10-12, ACC, additional help: Mon or Tue?

    Judy Cushing [email protected]

    Office Hours: Thursdays 1-2pm, Lab I, 1003, 867-6652 Class web page: off the SOS web page, or

    academic.evergreen.edu/r/ricdan16/SOSdatabase/databaseHome.html

    Assignments due most Wednesdays

    Project Parts due Mondays or Thursdays

    Lab Aids: Dan Rice, Jeff Jones

  • 8/8/2019 Slides01 Database Systems

    2/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 12

    TextbooksRequired:

    Database Systems: The Complete Book, by Garcia-Molina, Ullman, andWidom (first edition), Prentice Hall, 2002.

    Recommended: (one of these)

    SQL in a Nutshell : A Desktop Quick Reference, Kevin E. Kline, DanielKline, OReilly.

    A Guide to the SQL Standard. C. J. Date and H. Darwen, Addison-

    Wesley, 1999. It is more succinct but perhaps a more useful summary,than the Melton-Simon book.

    SQL3 Complete, A Guide to the SQL Standard: A User's Guide to theStandard Database Language SQL, (fourth edition), by C.J. Date andHugh Darwen, Addison-Wesley, 2000.

    SQL: 1999 - Understanding Relational Language Components, (firstedition), by Melton and Simon, Morgan Kaufmann, 2002.

    If you areusing PostgreSQL: Books on Unix, Perl, PHP, and CGI,PostgreSQL: Introduction and Concepts, Bruce Momjian, Addison-Wesley, 2001. This might also be on the web.

  • 8/8/2019 Slides01 Database Systems

    3/43

  • 8/8/2019 Slides01 Database Systems

    4/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 14

    Project

    Build an application using a relational databasesystem (SQL Server or PostgreSQL) accessed viaJava and the web.

    The project has 8 parts (due Mondays or Thursdays),starting with design and ending with a completeapplication.

    The early programming assignments should bewritten in SQL, Java.

    Some students found it helpful to switch toJavaScript, HTML (PHP or Perl if Postgres) for theweb-accessible part.

  • 8/8/2019 Slides01 Database Systems

    5/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 15

    Project - tentative schedule

    1. Project Part 1 due Jan. 14 (Mon) Choose a project anddesign an E/R diagram.

    2. Project Part 2 due Jan. 24 (TH) Relational design.

    3. Project Part 3 due Jan. 31 (TH) Create database inSQLServer.

    4. Project Part 4 due Feb. 7 (TH) Queries, updates, andindexes.

    5. Project Part 5 due Feb. 21 (TH) Embedded SQL.

    6. Project Part 6 due Feb. 28 (TH) Views, constraints, andtriggers.

    7. Project Part 7 due Mar. 6 (Wed) Entire databaseapplication, in Java.

    8. Project Part 8 due Mar. 11 (Mon) Database application,accessed via the web.

  • 8/8/2019 Slides01 Database Systems

    6/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 16

    Typical Warning

    (for those taking this class elsewhere)

    The database class is a lot of work.

    But it is worth it. Of all courses you take, it may be the

    one that gets you a job.

    We know that SOS gets people jobs, but

    database expertise does help. Data

    is key to many applications.

  • 8/8/2019 Slides01 Database Systems

    7/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 17

    Schedule

    Today: Jan. 7 Intro, Entity-Relationship Model.

    Read Chapter 1 and Sections 2.1, 2.3.

    Jan. 9 (W)Weak Entity Sets, Entity-Relationship Design.

    Read Sections 2.2, 2.4.

    Jan. 14 (M)Relational Model, Functional Dependencies.

    Read Sections 3.1-3.5. Project 1 due.

    Jan. 16 (W)Normal Forms, Multivalued Dependencies.

    Read Sections 3.6-3.7. Assignment 1 due.

  • 8/8/2019 Slides01 Database Systems

    8/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 18

    Syllabus The background and history of database

    management systems. The fundamentals of using a database

    management systems.

    Industry standards used for database management

    systems. Theoretical background of the relational model.

    Queries and Updates.

    Logic databases? Constraints and Triggers.

    Transactions and Security. Object-oriented, object-relational, semi-structured

    and XML database systems.

    Mediation and warehousing.

  • 8/8/2019 Slides01 Database Systems

    9/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 19

    What is a Database Management System?

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

    of data.

    3. Supports concurrent access to very largeamounts of 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 fromthe same account via ATM machines at the same time new

    balance is wrong whichever writes last.

  • 8/8/2019 Slides01 Database Systems

    10/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 110

    Relational Model

    Based on tables, as:

    acct # name balance

    12345 Sally 1000.21

    34567 Sue 285.48

    Today used in mostDBMS's.

  • 8/8/2019 Slides01 Database Systems

    11/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 111

    The DBMS Marketplace

    Relational DBMS companies Oracle, Sybase are among thelargest software companies in the world.

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

    Microsoft offers SQL-Server, plus Microsoft Access for the cheapDBMS on the desktop, answered by lite systems from othercompetitors.

    Relational companies also challenged by object-oriented DBcompanies, and XML data stores.

    But countered with object-relational systems, which retain therelational core while allowing type extension as in OO systems.

  • 8/8/2019 Slides01 Database Systems

    12/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 112

    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 likeupdate. SQL = intergalactic dataspeak.

    3. DBMS implementation.

    Consider DBMS system components, p. 11.

  • 8/8/2019 Slides01 Database Systems

    13/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 113

    Query Languages

    Employee

    Name Dept

    Department

    Dept Manager

    SQL

    SELECT ManagerFROM Employee, Department

    WHERE Employee.name = "Clark Kent

    AND Employee.Dept = Department.Dept

    Query Language

    Data definition language (DDL) ~ like type defs in C or Pascal

    Data Manipulation Language (DML)Query (SELECT)

    UPDATE < relation name >

    SET = < new-value>

    WHERE

  • 8/8/2019 Slides01 Database Systems

    14/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 114

    Host LanguagesC, C++, Fortran, Lisp, COBOL

    Application prog.

    Local Vars

    DBMS

    Calls to

    DB

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

    Query languageless general "non procedural" andoptimizable

    (Memory)

    (Storage)

  • 8/8/2019 Slides01 Database Systems

    15/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 115

    Relational model is good for:

    Large amounts of data > simple operations

    Navigate among small number of relations

    Difficult Applications for relational model:

    VLSI Design (CAD in general)

    CASE

    Graphical Data

    CPUALU

    ADDER

    Adder

    A

    FA

    ALU ADDER

    Bill of Materials or transitive

    closure

  • 8/8/2019 Slides01 Database Systems

    16/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 116

    Where 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 Identity

    4. Inheritance Defining new classes like old classes

    Object model: usually find objects via explicit navigation

    Also query language in some systems

  • 8/8/2019 Slides01 Database Systems

    17/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 117

    LOGIC (Horn Clause) DATA MODEL

    Prolog, Datalogif A1 and A2 then B

    prolog B:- A1 and A2Functions 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 = ZMore power than relational

    Can Compute Transitive Closure

    edge(X,Y)

    path(X,Y) :- edge(X,Y)path(X,Z) :- path(X,Y) & edge(Y,Z)

  • 8/8/2019 Slides01 Database Systems

    18/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 118

    Hierarchical

    60s

    70's

    80's

    90s

    now

    RelationalChoice for most new

    applications

    Object Bases Knowledge Bases

    Network

    Multi-tiered (web) Architectures XML Data Transfers and Transformations

  • 8/8/2019 Slides01 Database Systems

    19/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 119

    Students

    Entity/Relationship Model

    Diagrams to represent designs. Entity like object = thing.

    Entity setlike class = set of similar

    entities/objects.

    Attribute = property of entities in an entity set,

    similar to fields of a struct.

    In diagrams, entity setp rectangle;

    attributep

    oval. ID name phone

    height

  • 8/8/2019 Slides01 Database Systems

    20/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 120

    Relationships

    Connect two or more entity sets.

    Represented by diamonds.

    Students CoursesTaking

  • 8/8/2019 Slides01 Database Systems

    21/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 121

    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 list of entities, one from each

    set, that are connected by the relationship.

    Students Courses

    Sally CS180

    Sally CS111Joe CS180

  • 8/8/2019 Slides01 Database Systems

    22/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 122

    Multiway Relationships

    Usually 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, Lab

    Aids.

    Possibly, this E/R diagram is OK:

    Student CourseTaking

    Assisting

    Lab Aid

  • 8/8/2019 Slides01 Database Systems

    23/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 123

    Works in CS180, because each Lab Aid

    works for all students. Connection student-

    LabAid is only via the course.

    But what if students were divided intosections, each headed by a LabAid?

    Then, a student in CS180 would be related to

    only one of the LabAids for CS180. Which

    one?

    Need a 3-way relationship to tell.

  • 8/8/2019 Slides01 Database Systems

    24/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 124

    Student Course LabAid

    Ann CS180 Jan

    Sue CS180 PatBob CS180 Jan

    Student

    Course

    LabAid

    Enrolls

  • 8/8/2019 Slides01 Database Systems

    25/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 125

    Beers-Bars-Drinkers Example

    Our running example for the course.name addr license

    name manf name addr

    Beer Drinker

    BarServes Frequents

    Likes

  • 8/8/2019 Slides01 Database Systems

    26/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 126

    Multiplicity of Relationships

    Representationof Many-One

    E/R: arrow pointing to one.

    Rounded arrow = exactly one.

    Many-many Many-one One-one

  • 8/8/2019 Slides01 Database Systems

    27/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 127

    Example:

    Drinkers Have Favorite Beersname addr license

    name manf name addr

    Beer Drinker

    BarServes Frequents

    Likes

    Favorite

  • 8/8/2019 Slides01 Database Systems

    28/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 128

    One-One Relationships

    Put arrows in both directions.

    Design Issue:Is the rounded arrow justified?

    Design Issue:

    Here, manufacturer is an E.S.In earlier diagrams it is an attribute.

    Which is right?

    Manufacturer Beer Best-

    seller

  • 8/8/2019 Slides01 Database Systems

    29/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 129

    Attributes on Relationships

    Shorthand for 3-way relationship:

    Bar Beer Sells

    price

    Bar Beer Sells

    price

    Price

  • 8/8/2019 Slides01 Database Systems

    30/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 130

    A true 3-way relationship.

    Price depends jointly on beer and bar.

    Notice arrow convention for multiwayrelationships: all other E.S. determine oneof these.

    Not sufficiently general to express anypossibility.

    However, if price, say, depended only on thebeer, then we could use two 2-way

    relationships: price-beer and beer-bar.Or better: just make price an attribute of beer.

  • 8/8/2019 Slides01 Database Systems

    31/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 131

    Converting Multiway to 2-Way Baroque in E/R, but 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.

    Bar Beer

    The-

    Bar

    Price

    The-

    Beer

    The-

    Price

    BBP

  • 8/8/2019 Slides01 Database Systems

    32/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 132

    Roles

    Sometimes an E.S. participates more thanonce in a relationship.

    Label edges with roles to distinguish.

    Husband Wife

    d1 d2d3 d4

    Drinker

    Married

    husband wife

  • 8/8/2019 Slides01 Database Systems

    33/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 133

    NoticeBuddies is symmetric, Married not.

    No way to say symmetric in E/R.

    DesignQuestion

    Should we replace husbandand wifeby one

    relationshipspouse?

    Buddy1 Buddy2

    d1 d2

    d1 d3d2 d1d2 d4

    Drinker

    Buddies

    1 2

  • 8/8/2019 Slides01 Database Systems

    34/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 134

    More Design Issues1. Subclasses.

    2. Keys.

    3. Weak entity sets. (Next class.)

  • 8/8/2019 Slides01 Database Systems

    35/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 135

    SubclassesSubclass = special case = fewer entities =

    more properties.

    Example: Ales are a kind of beer. In

    addition to theproperties (= attributes and

    relationships) of beers, there is a color

    attribute for ales.

  • 8/8/2019 Slides01 Database Systems

    36/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 136

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

    inheritance).

    isa triangles indicate the subclass relation.

    name manf Beer

    Alecolor

    isa

  • 8/8/2019 Slides01 Database Systems

    37/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 137

    Different Subclass Viewpoints1. E/R viewpoint: An entity has a componentin each entity set

    to which it logically belongs. Its properties are the union of the properties of these E.S.

    2. Contrasts with object-oriented viewpoint: An object (entity)

    belongs to exactly one class.

    It inheritspropertiesof its superclasses.

    name manf Beer

    Alecolor

    isaPetes Ale

  • 8/8/2019 Slides01 Database Systems

    38/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 138

    Multiple Inheritance

    Theoretically, an E.S. could be a subclass ofseveral other entity sets.name manf

    Beers

    name manf

    Wines

    GrapeBeers

    isaisa

  • 8/8/2019 Slides01 Database Systems

    39/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 139

    ProblemsHow should conflicts be resolved?

    Example: manf means vintner for wines,

    bottler for beers. What does manf mean forgrape beers?

    Need ad-hoc notation to resolve meanings.

    In practice, we shall assume a tree of entitysets connected by isa, with all isaspointing from child to parent.

  • 8/8/2019 Slides01 Database Systems

    40/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 140

    KeysA 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 allattributes of the designated key.

  • 8/8/2019 Slides01 Database Systems

    41/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 141

    Example

    Suppose name is key forBeers.

    Beer name is also key for ales. In general, key at root is key for all.

    name manf Beers

    Alescolor

    isa

  • 8/8/2019 Slides01 Database Systems

    42/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 142

    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

  • 8/8/2019 Slides01 Database Systems

    43/43

    Winter 2002 Judy Cushing, Art Keller, Jeff Ullman 143

    or

    Assignment for Wednesday

    (for those who did SOS this Fall)

    Convert the UML for EU-Bid and EU-Leaseto an ER diagram.

    Merge them into a design for one database.