Database Systems-Lec2

Embed Size (px)

Citation preview

  • 8/14/2019 Database Systems-Lec2

    1/47

    Chapter 2The Relational DatabaseModel

  • 8/14/2019 Database Systems-Lec2

    2/47

    Entities and Attributes An entity is a person, place, event, or thing

    for which we intend to collect data.

    University -- Students, Faculty Members,Courses

    Airlines -- Pilots, Aircraft, Routes, Suppliers

    Each entity has certain characteristics knownas attributes.

    Student -- Student Number, Name, GPA,Date of Enrollment, Data of Birth, HomeAddress, Phone Number, Major

    Aircraft -- Aircraft Number, Date of LastMaintenance, Total Hours Flown, Hours

    Flown since Last Maintenance

    Entities & Attributes

  • 8/14/2019 Database Systems-Lec2

    3/47

    Entities and Attributes

    A grouping of related entities becomes anentity set.

    The STUDENT entity set contains allstudent entities.

    The FACULTYentity set contains all

    faculty entities.The AIRCRAFT entity set contains all

    aircraft entities.

    Entities & Attributes

  • 8/14/2019 Database Systems-Lec2

    4/47

    Tables and Their Characteristics

    A table contains a group of relatedentities -- i.e. an entity set.

    The terms entity set and table are oftenused interchangeably.

    A table is also called a relation.

    Entities & Attributes

  • 8/14/2019 Database Systems-Lec2

    5/47

  • 8/14/2019 Database Systems-Lec2

    6/47

    Fiel

    d

    Record

    File

    Common DataElementsAttribute

    Entity Occurrenc

    Entity Class Conceptual data modeling notationFile processing Notation

    Student File

    Id No. Name Faculty Major 1100 Ali FIT SE

    1200 Bobby FIT MIS

    1300 Clement FCM MM

    1400 David FCM MM

    1500 Evelyn FOE CE

  • 8/14/2019 Database Systems-Lec2

    7/47

    Fiel

    d

    Record

    File

    Attribute

    Entity Occurrenc

    Entity Class

    Column

    Row

    Table

    Conceptual data modeling notation

    File processing Notation

    Relational Table Notation

    Student File

    Id No. Name Faculty Major 1100 Ali FIT SE

    1200 Bobby FIT MIS

    1300 Clement FCM MM

    1400 David FCM MM

    1500 Evelyn FOE CE

    Common DataElements

  • 8/14/2019 Database Systems-Lec2

    8/47

    Student Table

  • 8/14/2019 Database Systems-Lec2

    9/47

    Keys Controlled redundancy (shared common

    attributes) makes the relational databasework.

    The primary key of one table appears

    again as the link (foreign key) in anothertable.

    If the foreign key contains either matching

    values or nulls, the table(s) that make useof such a foreign key are said to exhibit

  • 8/14/2019 Database Systems-Lec2

    10/47

    A Simple Relational Database

  • 8/14/2019 Database Systems-Lec2

    11/47

    The Relational Schema

  • 8/14/2019 Database Systems-Lec2

    12/47

    A key helps define entity relationships.

    The keys role is based on a concept known asdetermination, which is used in the definition offunctional dependence.

    The attribute B is functionally dependent on Aif A determines B.

    An attribute that is part of a key is known as akey attribute.

    A multi-attribute key is known as a compositekey.

    If the attribute (B) is functionally dependent ona composite key (A) but not on any subset of

    that composite key, the attribute (B) is fullyfunctionall de endent on (A).

    Keys

  • 8/14/2019 Database Systems-Lec2

    13/47

    Relational Database Keys

    Superkey

    Uniquely identifies each entity STU_NUM

    STU_NUM, STU_LNAME

    STU_NUM, STU_LNAME, STU_INIT

    Candidate key Minimal superkey STU_NUM

    STU_LNAME, STU_FNAME, STU_INIT,

    STU_PHONE

  • 8/14/2019 Database Systems-Lec2

    14/47

    Relational Database Keys

    Secondary key

    Used only for data retrieval Foreign key

    Values must match primary key inanother table

  • 8/14/2019 Database Systems-Lec2

    15/47

    Integrity Rules

    Entity integrity

    Ensures all entities are unique Each entity has unique key

    Referential integrity

    Foreign key must have null value ormatch primary key values

    Makes it impossible to delete rowwhose primary key has mandatory

    matching foreign key values in

  • 8/14/2019 Database Systems-Lec2

    16/47

    An Illustration of IntegrityRules

  • 8/14/2019 Database Systems-Lec2

    17/47

    Relational Database Operators

    The degree of relational

    completeness can be defined by theextent to which relational algebra issupported.

    Relational algebra defines thetheoretical way of manipulatingtable contents using the eightrelational functions: SELECT,

    PROJECT,JOIN, INTERSECT, UNION,DIFFERENCE PRODUCT and DIVIDE.

  • 8/14/2019 Database Systems-Lec2

    18/47

    UNION

    UNION combines all rows from two tables.The two tables must be union compatible(same attribute characteristics)

  • 8/14/2019 Database Systems-Lec2

    19/47

    INTERSECT

    INTERSECT produces a listing that containsonly the rows that appear in both tables.The two tables must be union compatible.

  • 8/14/2019 Database Systems-Lec2

    20/47

    DIFFERENCE

    DIFFERENCE yields all rows in one tablethat are not found in the other table; i.e.,it subtracts one table from the other. Thetables must be union compatible.

  • 8/14/2019 Database Systems-Lec2

    21/47

  • 8/14/2019 Database Systems-Lec2

    22/47

    SELECT

    SELECT yields values for all attributesfound in a table. It yields a horizontalsubset of a table.

  • 8/14/2019 Database Systems-Lec2

    23/47

    PROJECT

    PROJECT produces a list of all values forselected attributes. It yields a verticalsubset of a table.

  • 8/14/2019 Database Systems-Lec2

    24/47

    JOIN

    JOIN allows us to combine information fromtwo or more tables.JOIN is the real powerbehind the relational database, allowingthe use of independent tables linked by

    common attributes.

  • 8/14/2019 Database Systems-Lec2

    25/47

    Natural JOIN links tables by selecting

    only the rows with common values intheir common attribute(s). It is theresult of a three-stage process:

    A PRODUCT of the tables is created.(Figure 3.12)

    A SELECT is performed on the outputof the first step to yield only the rows

    for which the common attributevalues match. (Figure 3.13)

    A PROJECT is performed to yield asingle copy of each attribute, thereby

    eliminating the duplicate column.

    Natural JOIN

  • 8/14/2019 Database Systems-Lec2

    26/47

    Natural Join, Step 1: PRODUCT

  • 8/14/2019 Database Systems-Lec2

    27/47

    Step 2: SELECT

  • 8/14/2019 Database Systems-Lec2

    28/47

    Step 3: PROJECT

  • 8/14/2019 Database Systems-Lec2

    29/47

    EquiJOIN links tables based on an equalitycondition that compares specifiedcolumns of each table. The outcome ofthe EquiJOIN does not eliminate duplicatecolumns and the condition or criteria to

    join the tables must be explicitly defined.

    Theta JOIN is an equiJOIN that comparesspecified columns of each table using acomparison operator other than theequality comparison operator.

    In an Outer JOIN, the unmatched pairswould be retained and the values for the

    unmatched other tables would be left

    EquiJOIN & Theta JOIN

  • 8/14/2019 Database Systems-Lec2

    30/47

    OUTER JOIN In outer join for tables CUSTOMER and

    AGENT, two scenarios are possible.

    - Left outer join

    Yields all rows in CUSTOMER table,including those

    that do not have a matching values inthe AGENT table

  • 8/14/2019 Database Systems-Lec2

    31/47

    OUTER JOIN- Right outer join

    Yields all rows in AGENT table, includingthose

    that do not have a matching values inthe CUSTOMER table

  • 8/14/2019 Database Systems-Lec2

    32/47

    DIVIDE

    DIVIDE requires the use of one single-column table and one two-column table.

  • 8/14/2019 Database Systems-Lec2

    33/47

    The Data Dictionary and theSystem Catalog

    Data dictionary contains metadata toprovide detailed accounting of all tableswithin the database.

    System catalog is a very detailed systemdata dictionary that describes all objectswithin the database.

    System catalog is a system-createddatabase whose tables store thedatabase characteristics and contents.

    System catalog tables can be queried justlike any other tables.

  • 8/14/2019 Database Systems-Lec2

    34/47

    A Sample Data Dictionary

  • 8/14/2019 Database Systems-Lec2

    35/47

    Relationships within theRelational Database

    E-R Diagram (ERD)

    Rectangles are used to represent

    entities. Entity names are nouns and capitalized.

    Diamonds are used to represent therelationship(s) between the entities.

    The number 1 is used to represent the1 side of the relationship.

    The letter M is used to represent the

    many sides of the relationship.

    Relationships within the

  • 8/14/2019 Database Systems-Lec2

    36/47

    1:M relationship

    - Relational modeling ideal

    - Should be the norm in any relationaldatabase design

    M:N relationship

    - Must be avoided because they lead to data

    redundancies

    1:1 relationship

    - Should be rare in any relational database

    design

    Relationships within theRelational Database

  • 8/14/2019 Database Systems-Lec2

    37/47

    The 1:M Relationship Between

    Painter and Painting

  • 8/14/2019 Database Systems-Lec2

    38/47

  • 8/14/2019 Database Systems-Lec2

    39/47

    The M:N Relationship Between

    Student and Class

  • 8/14/2019 Database Systems-Lec2

    40/47

    Sample Student Enrollment Data

  • 8/14/2019 Database Systems-Lec2

    41/47

    A Many-to-Many Relationship

    Between Student and Class

  • 8/14/2019 Database Systems-Lec2

    42/47

  • 8/14/2019 Database Systems-Lec2

    43/47

    The Expanded

    EntityRelationshipModel

  • 8/14/2019 Database Systems-Lec2

    44/47

    The Relational Schema for the

    Entity Relationship Diagram

  • 8/14/2019 Database Systems-Lec2

    45/47

    Data Redundancy Revisited

    Data redundancy leads to data

    anomalies, such anomalies candestroy database effectiveness.

    Foreign keys control redundancies

    by using common attributes sharedby tables

    Sometimes, data redundancy is

    necessary

  • 8/14/2019 Database Systems-Lec2

    46/47

    The redundancy is crucial to the systems success.Copying the product price from the PRODUCT table tothe LINE table means that it is possible to maintain the

    historical accuracy of the transactions.

    The Relational Schema for

    the Invoicing System

  • 8/14/2019 Database Systems-Lec2

    47/47

    References

    ROB, P. AND CORONEL, C., 2004, Database

    Systems. 6th Ed., Thomson Course Technology