Dbms Ch3 Doc

Embed Size (px)

Citation preview

  • 8/3/2019 Dbms Ch3 Doc

    1/18

    THE RELATIONAL DATABASE

    MODEL

    The relational database model was developed

    by E.F.Codd in 1970.

    The relational model is based on three welldefined components:

    1. A logical data structure represented by a

    relational table where a data is stored.

    2. A set of integrity rules to enforce that the

    data remains consistent over time.

    3. A set of operations that define how the

    data are manipulated.

    The relational database model allows us to

    focus on the logical representation of the

    data and relationships rather than the

    physical storage details.

  • 8/3/2019 Dbms Ch3 Doc

    2/18

    Table

    Table plays an important role in relationalmodel of the database.

    A table is two-dimensional structure

    composed of rows and columns.

    It is a logical construct and not a physical

    construct.

    A table is also called as relation in relational

    model terminology used by Codd.

    A table is persistent relation, that is, a relation

    whose contents can be permanently stored for

    future use.

    The rows are also called records and columns

    are called fields

    Characteristics of a Relational Table

  • 8/3/2019 Dbms Ch3 Doc

    3/18

    1. A table is perceived as a two- dimensional

    structure consisting of rows and columns.

    2. Each table row (tuple) within the table

    represents a single entity occurrence

    within the entity set.

    3. Each table column represents an attribute,and each column has distinct name.

    4. Each row/column intersection represents a

    single data value

    5. All values in a column should be of the

    same data format. E.g. All integers or all

    float

    6. Each column has a specific range of

    values called as attribute domain.

    7. The order of rows and columns is not imp

    8. Each table must have an attribute or set of

    attributes which uniquely identifies a row

  • 8/3/2019 Dbms Ch3 Doc

    4/18

    Keys

    A key consists of one or more attributes that

    determine other attributes.

    Determination in the context of database

    If you know the value of an attribute A then

    you can get (determine) the value of

    attribute B. This means A Determines B.

    A B

    Functional Dependence

    The attribute B is said to be functionally

    dependent on A if each value in column A

    determines one and only one value in column

    B. Stud_Num Stud_Phone

    SuperKey

  • 8/3/2019 Dbms Ch3 Doc

    5/18

    An attribute or combination of attributes that

    uniquely identify each row in a table is called

    super key.

    Candidate Key

    A superkey without redundancies is called a

    candidate key. A minimal superkey is acandidate key

    Stud_Num

    Stud_Num, Stud_Lname

    Stud_Num, Stud_Lname, Stud_Fname-

    All the above three are super keys but

    Stud_Num is the candidate key

    Primary Key

    A candidate key which is selected to uniquely

    identify each row in the table

  • 8/3/2019 Dbms Ch3 Doc

    6/18

    It cannot contain null

    Null

    An unknown attribute value A known but missing attribute vale

    A not applicable value

    Secondary Key

    An attribute or combination of attributes usedfor data retrieval purposes.

    E.g. Although a Cust_Id is the primary key

    users use Cust_Name and Cust_Lname to

    retrieve data such keys are called secondary

    keys.

    Foreign Key

    An attribute or combination of attributes in

    one table whose values must match the

    primary key in another table or be null

    E.g Vend_Code in Product Table

    Integrity Rules

    Entity Integrity

  • 8/3/2019 Dbms Ch3 Doc

    7/18

    All primary key entries are unique and no part

    of a primary key may be null

    Use of Entity Integrity:

    Each row will have a unique identity

    Foreign key values can properly reference

    primary key values

    Referential Integrity

    A foreign key may have either a null entry-as

    long as it is not a tables primary key- or an

    entry that matches the primary key value in a

    table to which it is related.

    Every non-null foreign key value must

    reference an existing primary key value.

    Other Integrity Rule

    Other integrity rules that can be enforced in

    the relational model are NOT NULL and

    UNIQUE constraints.

  • 8/3/2019 Dbms Ch3 Doc

    8/18

    NOT NULL constraint can be placed on a

    column to ensure that every row in the table

    has a value for that column.

    UNIQUE constraint is a restriction placed on a

    column to ensure that no duplicate values exist

    for that column.

    Relational Set Operators

    There are eight relational operators which

    explain how the data is manipulated

    theoretically.

    SELECT, PROJECT, JOIN, INTERSECT,

    UNION, DIFFERENCE, PRODUCT and

    DIVIDE

    1. UNION

    Union combines all rows from two tables,

    excluding duplicate rows.

    The tables should be union-compatible

    meaning:

  • 8/3/2019 Dbms Ch3 Doc

    9/18

    The two tables should have the same

    number of columns; columns should have

    same names and should have the samedomains.

    Only then we can carry out union on the

    two tables.

    2. INTERSECT

    It gives only the rows that appear in both

    the tables. The tables have to be

    compatible to use the intersect operation

    on them.

    Data types of the columns should also

    match.

    3. DIFFERENCE

    It gives all the rows in one table that are

    not found in the other table.

    It subtracts one table from the other.

  • 8/3/2019 Dbms Ch3 Doc

    10/18

    Tables have to be union-compatible to

    give valid results.

    4. PRODUCT

    It gives all possible pair of rows and

    columns from the two tables. It is also

    known as Cartesian product.

    5. SELECT

    It is also known as RESTRICT gives

    values for all rows found in a table.

    Or one can also select rows based on some

    condition.(horizontal subset)

    6. PROJECT

    It gives all values for selected attributes.

    It gives a vertical subset of a table.

    7. DIVIDE

  • 8/3/2019 Dbms Ch3 Doc

    11/18

    It requires the use of one single column

    table and another two column table.

    Table with two columns is divided by table

    with single column the resulting table will

    contain those values where a value in the

    unshared column is associated with every

    value present in the table two.

    Relationships in Relational Database

    There are three types of relationships between

    tables.

    1: M -One to Many

    1: 1- One to One

    M: N- Many to Many

  • 8/3/2019 Dbms Ch3 Doc

    12/18

    The 1: M Relationship

    Relationship between Painter and Painting is a1: M relationship.

    One painter can paint many paintings and

    Each painting can be painted by a one and

    only one painter.

    The One to Many relationships are

    implemented by putting the primary key of the

    1 side table in the table on the many side

    as a foreign key as shown below:

    Table Name: Painter

    Primary key: Painter_Num

    Painter_NumPainter_LnamePainter_Fname

  • 8/3/2019 Dbms Ch3 Doc

    13/18

    Table Name: PaintingPrimary key: Painting_Num

    Foreign Key: Painter_Num

    Painting_NumPainting_TitlePainter_Num

    The 1: 1 Relationship

    In this relationship one instance of an entity is

    related to only one other entity instance.

    E.g. one professor is a chairman of only one

    Department and one department can only have

    one chairman.

    The M: N relationship

    Consider the following example:

  • 8/3/2019 Dbms Ch3 Doc

    14/18

    A student can take many classes and a class

    can have many students.

    This is a many to many relationship between

    STUDENT and CLASS table.

    Logically the M: N relationship between the

    STUDENT and CLASS table is as follows:

    Table: STUDENT

    Stud_NumStud_FnameStud_LnameClass_Code

    Table:CLASS

    Class_CodeStud_NumClass_TimeClass_Room

  • 8/3/2019 Dbms Ch3 Doc

    15/18

    The above table structures create a lot of

    redundancies.

    In the student table student data such as fname

    lname is repeatedly stored for every class that

    the student takes.

    Similarly in the class table every student

    taking a class generates a class record.

    The above problems in the M: N relationship

    can be avoided by creating a third entity called

    as a composite entity orbridge entity.

    This entity is implemented by creating a third

    table called linking table.

    Table: STUDENT

    Primary key: Stud_Num

    Stud_Num Stud_Fname Stud_Lname

    Table: CLASS

    Primary key: Class_Code

  • 8/3/2019 Dbms Ch3 Doc

    16/18

    Class_Code Class_Time Class_Room

    Linking Table: Enroll

    Primary key: Class_Code+Stud_Num

    Foreign key: Class_Code, Stud_Num

    Class_Code Stud_Num Grade

    Enroll table must contain the primary key of

    both tables which are linked.

    Here in the new table they are the foreign

    keys.

    Also the combination of these primary keys is

    the primary key of the new table.

    The foreign key column may have repeated

    values but those are controlled redundancies.

    Controlled redundancy is required in a

    database for increasing the performance

    (processing speed) the database.

  • 8/3/2019 Dbms Ch3 Doc

    17/18

    Data DictionaryIt provides detailed information about all the

    tables in the database.

    It contains metadata-list of attributes and

    characteristics of the attributes.

    System Catalog contains data about all the

    objects in the database.

    Homonyms

    Homonyms are the same names used to label

    different attributes.

    It should be generally avoided.

  • 8/3/2019 Dbms Ch3 Doc

    18/18

    Some relational software checks for

    homonyms and alerts the user or makes the

    corrections automatically.

    Synonyms

    The use of different names to identify the

    same object is called synonym.

    Synonyms