31
CHAPTER 2 RELATIONAL MODEL Intro to DB

Intro to DB CHAPTER 2 RELATIONAL MODELocw.snu.ac.kr/sites/default/files/NOTE/Week2_Chapter 02.pdf · 2019. 9. 6. · END OF CHAPTER 2. Title: Chap 2: Rel Model Author: SGLEE Created

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

  • CHAPTER 2

    RELATIONAL MODEL

    Intro to DB

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 2

    Chapter 2: Relational Model

    ▪ Structure of Relational Databases

    ▪ Database Schema

    ▪ Keys

    ▪ Schema Diagrams

    ▪ Relational Query Languages

    ▪ Relational Operations

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 3

    Data Model

    ▪ The framework/formalism for representing data and their relationships

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 4

    Data Model

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 5

    Data Model

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 6

    Data Model

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 7

    Data Model - Relational

    attributes

    (or columns)

    tuples

    (or rows)

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 8

    Relation (in Set Theory)

    ▪ Binary relation R on a set A

    is a collection of ordered pairs of elements of A

    ▪ R A A

    ▪ (Bipartite) Relation R2 between elements of set B and set C

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 9

    Basic Structure of a Relation

    ▪ Formally,

    given sets D1, D2, …. Dn a relation r is a subset of D1 x D2 x … x Dn

    r D1, X ….. X Dn (n: degree of r)

    or

    r = { | d1 ∈ D1, ..., dn ∈ Dn } (set of tuples)

    ▪ Example: customer-name = {Jones, Smith, Curry, Lindsay}

    customer-street = {Main, North, Park}

    customer-city = {Harrison, Rye, Pittsfield}

    Then r = { (Jones, Main, Harrison), (Smith, North, Rye),

    (Curry, North, Rye), (Lindsay, Park, Pittsfield)}

    is a relation over customer-name x customer-street x customer-city

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 10

    Attribute Types

    ▪ Each attribute of a relation has a name

    ▪ The set of allowed values for each attribute is called the domain of the

    attribute

    ▪ Attribute values are (normally) required to be atomic,

    that is, indivisible

    E.g. multivalued attribute values are not atomic

    E.g. composite attribute values are not atomic

    ▪ The special value null is a member of every domain

    ▪ The null value causes complications in the definition of many operations

    we shall ignore the effect of null values in our main presentation and consider their effect

    later

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 11

    Relation Schema

    ▪ A1, A2, …, An are attributes

    ▪ R = (A1, A2, …, An ) is a relation schema

    e.g.

    Customer-schema = (customer-name, customer-street, customer-city)

    ▪ r(R) is a relation (variable) on the relation schema R

    e.g.

    customer(Customer-schema)

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 12

    Relation Instance

    Jones

    Smith

    Curry

    Lindsay

    customer-name

    Main

    North

    North

    Park

    customer-street

    Harrison

    Rye

    Rye

    Pittsfield

    customer-city

    customer

    attributes

    tuples

    ▪ The current values (relation instance) of a relation are specified by a table

    ▪ An element t of r is a tuple,

    represented by a row in a table

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 13

    Relations are Unordered

    ▪ Order of tuples is irrelevant

    (tuples may be stored in an arbitrary order)

    ▪ E.g. instructor relation with unordered tuples

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 14

    Relational Database

    ▪ A database consists of multiple relations

    ▪ Information about an enterprise is broken up into parts, with each relation storing one part of the information

    E.g.: instructor: information about teachers in the university

    student: information about students

    advisor: information about which instructor advises which students

    ▪ Storing all information as a single relation is not a good idea

    univ (instructor -ID, name, dept_name, salary, student_Id, ..)

    ▪ Database design (Chapter 7 & 8) deals with how to decide on the relational schemas

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 15

    A relational database for a university

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 16

    Keys

    ▪ Let K R

    ▪ K is a superkey of R

    if values for K are sufficient to identify a unique tuple of each possible relation

    r(R).

    ▪ By “possible r” we mean a relation r that could exist in the enterprise we are

    modeling.

    Example: {customer-name, customer-street} and

    {customer-name}

    are both superkeys of Customer, if no two customers can possibly have the same name.

    ▪ K is a candidate key if K is minimal

    Example: {customer-name} is a candidate key for Customer,

    since it is a superkey, and no subset of it is a superkey.

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 17

    Keys

    ▪ Primary key: one of the candidate keys is selected to be the primary key of

    the table

    which one?

    ▪ Foreign key: Value in one relation must appear in another

    Referencing relation

    Referenced relation

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 18

    Schema Diagram for a Banking Enterprise

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 19

    Schema Diagram for the University

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 20

    Query Languages

    ▪ Language in which user requests information from the database. procedural

    specify what data are needed and how to get those data

    nonprocedural

    declarative

    specify only what data are needed

    ▪ “Pure” languages:

    Relational Algebra

    Tuple Relational Calculus

    Domain Relational Calculus

    ▪ Pure languages form underlying basis of query languages that people use.

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 21

    Relational Algebra

    ▪ Algebra : operators and operands Relational algebra

    operands : relations

    operators : basic operators (+ additional operations)

    take two or more relations as inputs and give a new relation as a result.

    ▪ Procedural language

    ▪ Operators select join

    project division

    union assignment

    set difference …

    Cartesian product

    rename

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 22

    Examples of Relational Operators

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 23

    Select Operation – selection of tuples

    A B C D

    1

    5

    12

    23

    7

    7

    3

    10

    A B C D

    1

    23

    7

    10

    Relation r :

    A=B ^ D>5 (r) :

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 24

    Project Operation – selection of columns

    ▪ Relation r: A B C

    10

    20

    30

    40

    1

    1

    1

    2

    A C

    1

    1

    1

    2

    =

    A C

    1

    1

    2

    A,C (r)

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 25

    Union Operation –merging two relations

    ▪ Relations r, s: A B

    1

    2

    1

    A B

    2

    3

    r

    s

    A B

    1

    2

    1

    3

    ▪ r s:

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 26

    Set Difference Operation

    ▪ Relations r, s: A B

    1

    2

    1

    A B

    2

    3

    r

    s

    A B

    1

    1

    ▪ r – s:

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 27

    Set-Intersection Operation

    ▪ Relation r, s:

    ▪ r s

    A B

    1

    2

    1

    A B

    2

    3

    r s

    A B

    2

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 28

    A B

    1

    2

    A B

    1

    1

    1

    1

    2

    2

    2

    2

    C D

    10

    19

    20

    10

    10

    19

    20

    10

    E

    a

    a

    b

    b

    a

    a

    b

    b

    C D

    10

    19

    20

    10

    E

    a

    a

    b

    b

    r s

    Joining two relations: Cartesian-Product Op.

    ▪ Relations r, s:

    ▪ r x s:

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 29

    Joining two relations: Natural-Join Operation

    ▪ Relations r, s:

    A B

    1

    2

    4

    1

    2

    C D

    a

    a

    b

    a

    b

    B

    1

    3

    1

    2

    3

    D

    a

    a

    a

    b

    b

    E

    r

    A B

    1

    1

    1

    1

    2

    C D

    a

    a

    a

    a

    b

    E

    s

    ▪ r⋈ s

  • Original Slides:

    © Silberschatz, Korth and SudarshanIntro to DB

    Copyright © by S.-g. Lee Chap 2 - 30

    Composition of Operations

    ▪ Can build expressions using multiple operations

    ▪ Example: A=C(r x s)

    ▪ r x s

    ▪ A=C(r x s)

    A B

    1

    1

    1

    1

    2

    2

    2

    2

    C D

    10

    19

    20

    10

    10

    19

    20

    10

    E

    a

    a

    b

    b

    a

    a

    b

    b

    A B C D E

    1

    2

    2

    10

    19

    20

    a

    a

    b

    A B

    1

    2

    C D

    10

    19

    20

    10

    E

    a

    a

    b

    b

    r s

  • END OF CHAPTER 2