Reln Database

download Reln Database

of 18

Transcript of Reln Database

  • 8/8/2019 Reln Database

    1/18

    Chapter 3 The Relational Database

    Model

    l Tthe relational database model takes a logical view of datal The relational model's basic components are entities, attributes, and relationships among

    entitiesl How entities and their attributes are organized into tablesl About relational database operators, the data dictionary, and the system catalogl How data redundancy is handled in the relational database modell Why indexing is important

    A logical View of Data

    l Relational model Enables us to view data logically rather than physically Reminds us of simpler file concept of data storage or spreadsheet concept of table

    l Table Two-dimensional structure composed of rows and columns Has advantages of structural and data independence Resembles a file from conceptual point of view Easier to understand than its hierarchical and network database predecessors

    Tables

    l Table also called a relation because the relational model's creator, Codd, used this term.l A relation (and a relational DBMS's table) differs from a table in that there is no default order

    of rows or columns Note SQL does use a column order but this is a limitation of SQL, not the model.

    l Each row (tuple) represents a single entity in an entity setl Each column represents an attribute and each must have a distinct namel Each row/column intersection (cell) represents a single atomic valuel All values in a column must have same data formatl Each column has a specific range of values known as a domain l Each row must have some combination of attributes that uniquely identifies it (key)

    Student Table Example

    Page 1 of 18Chapter 3 Notes

    14-Apr-06http://cs1.mcm.edu/~rob/class/dbms/notes/Chapt03/Chapter03.html

  • 8/8/2019 Reln Database

    2/18

    Keys

    l A key consists of one or more attributes that determine other attributesl Primary key (PK) is an attribute (or a combination of attributes) that uniquely identifies any

    given entity (row)l Key's role is based on determination

    If you know the value of attribute A, you can look up (determine) the value of attribute B

    Types of Keys

    Composite keyKey consisting of multiple attributes

    Key attribute

    Page 2 of 18Chapter 3 Notes

    14-Apr-06http://cs1.mcm.edu/~rob/class/dbms/notes/Chapt03/Chapter03.html

  • 8/8/2019 Reln Database

    3/18

    Any attribute that is part of a key

    SuperkeyAny key that uniquely identifies each entityThe entire row (all attributes) is a superkey

    Candidate keyA superkey without redundancies

    Null Values

    l Can represent An unknown attribute value A known, but missing, attribute value A 'not applicable' condition

    l Not permitted in Primary key attributesl Not the same as 0 or ""l Can create problems in logic and using formulas

    Controlled Redundancy

    l Makes the relational database workl Tables within the database share common attributes that enable us to link tables togetherl Multiple occurrences of values in a table are not redundant when they are required to make the

    relationship workl Uncontrolled Redundancy is unnecessary duplication of datal In general any values duplicated between tables should be part of a Foreign Key -- Primary key

    relation (see below)

    Foreign Keys

    Foreign key (FK)An attribute whose values match primary key values in the related table

    Referential integrityFK contains a value that refers to an existing valid tuple (row) in another relation

    Secondary keyKey used strictly for data retrieval purposes

    Keys (Summary)

    Key Type Definition

    Superkey Any attribute or combination of attributes that uniquely identifies a row in the table

    CandiateKey

    aka Minimal Superkey. A superkey that does not contain a subset of attributes that isitself a superkey

    Primary Key The candidate key selected to uniquely identify all rowsCannot contain null values

    Foreign KeyAn attribute (or combination of attributes) in one table that must either match the

    Page 3 of 18Chapter 3 Notes

    14-Apr-06http://cs1.mcm.edu/~rob/class/dbms/notes/Chapt03/Chapter03.html

  • 8/8/2019 Reln Database

    4/18

    In tegrity Rules

    Entity Integrity

    l All entities are uniquel Each entity has unique primary key

    Referential Integrity

    l Foreign key must match primary key of referenced table or be nulll Impossible to delete row whose primary key has matching foreign key in another table

    Relational Algebra

    Key operators:

    l SELECTl PROJECTl JOIN

    Other operators

    l INTERSECTl UNIONl DIFFERENCEl PRODUCTl DIVIDE

    SELECT

    Subset of rows based on condition

    primary key of another table or be null

    SecondaryKey

    An attribute or combination of attributes used to make data retrieval more efficient.

    Page 4 of 18Chapter 3 Notes

    14-Apr-06http://cs1.mcm.edu/~rob/class/dbms/notes/Chapt03/Chapter03.html

  • 8/8/2019 Reln Database

    5/18

    PROJECT

    Subset of columns

    Page 5 of 18Chapter 3 Notes

    14-Apr-06http://cs1.mcm.edu/~rob/class/dbms/notes/Chapt03/Chapter03.html

  • 8/8/2019 Reln Database

    6/18

    PRODUCT

    All possible pairings of two tables

    Page 6 of 18Chapter 3 Notes

    14-Apr-06http://cs1.mcm.edu/~rob/class/dbms/notes/Chapt03/Chapter03.html

  • 8/8/2019 Reln Database

    7/18

    JOIN

    l Combines information from two or more tablesl Real power behind the relational database, allowing the use of independent tables linked by

    common attributesl Join is actually a combination of Product, Select, and Project

    Page 7 of 18Chapter 3 Notes

    14-Apr-06http://cs1.mcm.edu/~rob/class/dbms/notes/Chapt03/Chapter03.html

  • 8/8/2019 Reln Database

    8/18

    Natural JOIN

    Links tables by selecting rows with common values in common attribute(s) (typically equality onforeign keys).

    Three stage process:

    l Product creates one tablel Select yields appropriate rowsl Project yields single copy of each attribute to eliminate duplicate columns

    Natural J oin: Step 1 P roduct

    Page 8 of 18Chapter 3 Notes

    14-Apr-06http://cs1.mcm.edu/~rob/class/dbms/notes/Chapt03/Chapter03.html

  • 8/8/2019 Reln Database

    9/18

    Natural Join Step 2: Select

    Natural Join Step 3: P roject

    Page 9 of 18Chapter 3 Notes

    14-Apr-06http://cs1.mcm.edu/~rob/class/dbms/notes/Chapt03/Chapter03.html

  • 8/8/2019 Reln Database

    10/18

    Natural J oin: Outcome

    l Final outcome yields table that Does not include unmatched pairs Provides only copies of matches

    l If no match is made between the table rows,

    the new table does not include the unmatched row

    Other Joins

    EquiJOIN

    l Links tables based on equality condition that compares specified columns of tablesl Does not eliminate duplicate columnsl Join criteria must be explicitly defined

    Theta JOIN

    l EquiJOIN that compares specified columns of each table

    Outer JOIN

    l Matched pairs are retainedl Unmatched values in other tables left nulll Right and left

    Intersect

    Page 10 of 18Chapter 3 Notes

    14-Apr-06http://cs1.mcm.edu/~rob/class/dbms/notes/Chapt03/Chapter03.html

  • 8/8/2019 Reln Database

    11/18

    Difference

    Yields all rows in one table not found in the other tablethat is, it subtracts one table from the other

    Union

    Page 11 of 18Chapter 3 Notes

    14-Apr-06http://cs1.mcm.edu/~rob/class/dbms/notes/Chapt03/Chapter03.html

  • 8/8/2019 Reln Database

    12/18

    Divide

    Requires user of single-column table and two-column table (I have never seen a DIVIDE)

    Data Dictionary and System Catalog

    Data Dictionary

    l Metadatal Detailed account of all tables in database

    System Catalog

    l System-created databasel

    Stores database characteristics and contentsl The database stores its own descriptionl Tables can be queried just like any other tablesl Automatically produces database documentation

    Relationships w ithin Relational Database

    Classified by cardinalities of entities participating

    l 1:M Relational modeling ideal

    Should be the norm in any relational database designl M:N

    Must be avoided because they lead to data redundancies Split into a pari of 1:M Relationships

    l 1:1 Should be rare

    The 1:1 Relationship

    Page 12 of 18Chapter 3 Notes

    14-Apr-06http://cs1.mcm.edu/~rob/class/dbms/notes/Chapt03/Chapter03.html

  • 8/8/2019 Reln Database

    13/18

    l One entity can be related to only one other entity, and vice versal Often means that entity components were not defined properlyl Could indicate that two entities actually belong in the same tablel Sometimes 1:1 relationships are appropriate to help avoid nulls

    E-R Diagram

    l Graphical representation of modell Chenl Crows Feet

    ERD Symbols

    l Rectangles represent entitiesl Chen:

    diamonds represent relations numbers or m and n are cardinalities

    l

    Crows Foot Lines represent relationships bar crossing line is "1" side 3 pronged crows foot is many side

    Example 1:M Relationship

    Implementation of 1:M Relationship

    Page 13 of 18Chapter 3 Notes

    14-Apr-06http://cs1.mcm.edu/~rob/class/dbms/notes/Chapt03/Chapter03.html

  • 8/8/2019 Reln Database

    14/18

    M:N Relationships

    M:N Relationship

    Page 14 of 18Chapter 3 Notes

    14-Apr-06http://cs1.mcm.edu/~rob/class/dbms/notes/Chapt03/Chapter03.html

  • 8/8/2019 Reln Database

    15/18

    l Cannot be directly implemented in a Relational DBMS

    Linking Table

    l Implementation of a composite entityl Yields required M:N to 1:M conversionl Composite entity table must contain at least the primary keys of original tablesl Linking table contains multiple occurrences of the foreign key valuesl Additional attributes may be assigned as needed

    Composite ERD

    Page 15 of 18Chapter 3 Notes

    14-Apr-06http://cs1.mcm.edu/~rob/class/dbms/notes/Chapt03/Chapter03.html

  • 8/8/2019 Reln Database

    16/18

    Linking Tables: Implementation

    Page 16 of 18Chapter 3 Notes

    14-Apr-06http://cs1.mcm.edu/~rob/class/dbms/notes/Chapt03/Chapter03.html

  • 8/8/2019 Reln Database

    17/18

    Data Redundancy Revisited

    Foreign keys control and reduce redundancy.

    It is not that there is no redundancy in a properly designed database: the redundancy there is is

    controlled.

    Indexes

    Note: I do not consider indexes part of the relational model. They are an implementationartifact to improve performance

    l Arrangement used to logically access rows in a tablel Index key

    Page 17 of 18Chapter 3 Notes

    14-Apr-06http://cs1.mcm.edu/~rob/class/dbms/notes/Chapt03/Chapter03.html

  • 8/8/2019 Reln Database

    18/18

    Indexs reference point Points to data location identified by the key

    l Unique index Index in which the index key can only have one pointer value (row) associated with it

    l Each index is associated with only one table

    Notes

    Page 18 of 18Chapter 3 Notes