+ All Categories
Home > Documents > 09-ER to Relational Mapping

09-ER to Relational Mapping

Date post: 04-Jun-2018
Category:
Upload: v-bharath-kumar-reddy
View: 230 times
Download: 0 times
Share this document with a friend

of 30

Transcript
  • 8/13/2019 09-ER to Relational Mapping

    1/30

    ER to Relational

    Mapping

    2002, National Centre for Software Technology, Bangalore.

    ER to Relational 1999-2002 NCST 1

  • 8/13/2019 09-ER to Relational Mapping

    2/30

    Translating the E-R Model

    E-R model captures the database at the conceptual

    level - translating the model into the relational

    model (or any other model) yields the actualdatabase.

    a Data-Definition Language (DDL) is used to set

    up the actual database schema.

    ER to Relational 1999-2002 NCST 2

  • 8/13/2019 09-ER to Relational Mapping

    3/30

    ER to Relational 1999-2002, NCST 3

    trainee

    sponsor

    is_sponsored_by

    Every trainee has one and only onesponsor and every sponsor sponsors

    one and only one trainee.

    One-to-One

    Mandatory

    Sp_Id ....... .... ..........

    EmpId ....... .... Sp_Id

    Not Null!

  • 8/13/2019 09-ER to Relational Mapping

    4/30

    ER to Relational 1999-2002, NCST 4

    Every dept has one and only onemanager; an employee can be the

    manager of at most one dept.

    One-to-One

    Optional

    Emp_Id ........ ..... ............

    DeptId ....... .... Emp_Id

    department

    employee

    manages

    Not Null!

  • 8/13/2019 09-ER to Relational Mapping

    5/30

    ER to Relational 1999-2002, NCST 5

    Some of the consultants areprovided PCs at their desks.

    One-to-One

    Optional

    PC

    consultant

    provided_with

    PC_Id ....... .... ..........

    EmpId ....... .... PC_Id

    Null Allowed!

  • 8/13/2019 09-ER to Relational Mapping

    6/30ER to Relational 1999-2002, NCST 6

    Every employee is alloted adepartment. A department

    however, staffs none, one or manyemployees.

    One-to-Many

    Optional

    Dept ....... .... ..........

    EmpId ....... .... Dept

    department

    employee

    staffs

    Not Null!

  • 8/13/2019 09-ER to Relational Mapping

    7/30ER to Relational 1999-2002, NCST 7

    A consultant can have at most onesecretary. A secretary may work for

    several consultants.

    One-to-Many

    Optional

    Sec_Id ....... .... ..........

    C_Id ....... .... Sec_Id

    secretary

    consultant

    works_for

    Null Allowed!

  • 8/13/2019 09-ER to Relational Mapping

    8/30ER to Relational 1999-2002, NCST 8

    A professional association may havenone, one or more consultants as

    members. Similarly, a consultant may be

    a member of none, one or manyprofessional associations.

    Many-to-Many

    Optional

    PA_Id ....... .... ..........Prof_asso

    c

    consultant

    is_member_of C_Id ....... ....PA_Id

    C_Id ....... ....

    is_member_of

  • 8/13/2019 09-ER to Relational Mapping

    9/30ER to Relational 1999-2002, NCST 9

    Recursive

    One-to-One

    Mandatory

    trainee

    is_partner_of

    EmpId ....... .... Prtnr_Id

    Every trainee has another trainee asa partner.

    references

    Not Null!

  • 8/13/2019 09-ER to Relational Mapping

    10/30ER to Relational 1999-2002, NCST 10

    An employee may have anotheremployee as a spouse.

    Recursive

    One-to-One

    Optional

    employee

    is_married_to

    EmpId ....... .... Spouse_Id

    references

    Null

    Allowed.

  • 8/13/2019 09-ER to Relational Mapping

    11/30ER to Relational 1999-2002, NCST 11

    Consultants are divided into groupswith each group having a leader.

    Recursive

    One-to-Many

    Optional

    consultant

    leads

    EmpId ....... .... Ldr_Id

    references

    Null

    Allowed.

  • 8/13/2019 09-ER to Relational Mapping

    12/30ER to Relational 1999-2002, NCST 12

    Several projects employ similar skillsets.

    i.e., projects share skills.

    Recursive

    Many-to-Many

    Optional

    project

    shares_skills_with

    Proj_Id ....... ....

    shares_skills_with

    Proj1Id ....... ....Proj2Id SkillId

  • 8/13/2019 09-ER to Relational Mapping

    13/30ER to Relational 1999-2002, NCST 13

    Consultants use casebooks forprojects. Each consultant uses adifferent casebook for different

    projects.

    Ternary

    One-One-One

    Mandatory

    consultant

    casebook project

    Proj_Id ....... ....Book_Id ....... ......

    EmpId ....... ....

    uses_casebook uses_casebookEmpId Proj_Id Book_Id

  • 8/13/2019 09-ER to Relational Mapping

    14/30ER to Relational 1999-2002, NCST 14

    Employees use a wide rangeof different skills for each

    project.

    Ternary

    Many-Many-Many

    Optional

    employee

    skills project

    Proj_Id ....... ....Skill_Id ....... ......

    EmpId ....... ....

    uses_skillEmpId Proj_Id Skill_Id

    uses_skill

  • 8/13/2019 09-ER to Relational Mapping

    15/30

    ER to Relational 1999-2002, NCST 15

    A consultant is either anaccountant or a lawyer but not

    none and not both.

    Specialisation

    Hierarchies

    consultant

    Emp_Id ....... .....

    Emp_Id ....... ......

    1

    lawyeraccountant

    (d)

    accountant

    lawyer

    does not map toa table

  • 8/13/2019 09-ER to Relational Mapping

    16/30

    ER to Relational 1999-2002, NCST 16

    A consultant is either anaccountant or a lawyer but not

    none and not both.

    Specialisation

    Hierarchies2

    Emp_Id ....... ...... acct .... ... lwyr .... ... Type

    consultant

    A/L

    consultant

    lawyeraccountant

    (d)

  • 8/13/2019 09-ER to Relational Mapping

    17/30

    ER to Relational 1999-2002, NCST 17

    A consultant is either anaccountant or a lawyer or

    none, but not both.

    Specialisation

    Hierarchies

    Emp_Id ....... .....

    Emp_Id ....... ......

    3

    accountant

    lawyer

    Emp_Id ....... .....

    consultantconsultant

    lawyeraccountant

    (d)

  • 8/13/2019 09-ER to Relational Mapping

    18/30

    ER to Relational 1999-2002, NCST 18

    A consultant is either anaccountant or a lawyer or

    none or both

    Specialisation

    Hierarchies4

    Emp_Id ....... ...... acct .... ... lwyr .... ... A L

    consultant

    Boolean

    consultant

    lawyeraccountant

    (o)

  • 8/13/2019 09-ER to Relational Mapping

    19/30

    a relationship between two entities implies a

    migration of key from one entity to another.

    ER to Relational : a recap

    generally, every entity translates into a distinct

    table.

    in a 1:many relationship, the key migrates to the

    entity at whose end cardinality is >1.

    ER to Relational 1999-2002, NCST 19

  • 8/13/2019 09-ER to Relational Mapping

    20/30

    ER to Relational : a recap

    a many:many relationship is best avoided; where it

    does exist, the relation itself maps to a distinct table.

    table capturing the many:many relationship should

    preferably take the same name as the relationship.

    ER to Relational 1999-2002, NCST 20

  • 8/13/2019 09-ER to Relational Mapping

    21/30

    ER to Relational 1999-2002, NCST 21

    Data Definition Language

    SQL : Structured Query Language.

    Components of SQL:

    Data Definition Language Data Manipulation Language

    Transaction Control Component

    and others.......

    Data Definition Language (DDL) is used to

    set up the schema.

  • 8/13/2019 09-ER to Relational Mapping

    22/30

    ER to Relational 1999-2002, NCST 22

    DDL Syntax

    SQL: the standard

    case insensitive

    SQL: implementations

    free-format

    SQL*Plus, mySQL,

    SQL Server . . . .

    SQL2

    (SQL92)

    v/s

    Although the implementations are built around the

    SQL standard, they have their own nuances.

    support for ad-hoc queries

  • 8/13/2019 09-ER to Relational Mapping

    23/30

    ER to Relational 1999-2002, NCST 23

    DDL Syntax

    create table tablename(col1_name datatype,

    col2_name datatype,

    ......coln_name datatype)

    where datatype is a valid data-type supported by the DBMS product

    in use : char, varchar2, date, long, etc.

  • 8/13/2019 09-ER to Relational Mapping

    24/30

    ER to Relational 1999-2002, NCST 24

    DDL : Example

    create table employee(emp_id varchar2(10),

    emp_fname varchar2(20),

    emp_lname varchar2(20),emp_address varchar2(80),

    emp_city varchar2(15),

    emp_email varchar2(30))

  • 8/13/2019 09-ER to Relational Mapping

    25/30

    ER to Relational 1999-2002, NCST 25

    Enforcing Integrity Constraints

    Integrity Constraints capture business semantics;

    modeled by the relationships.

    Declarative constraints enforce integrity at the

    field level or row level or table level

    e.g. column sexcan take values Mor Fonly

    columns empnoand datepdconstitute the

    primary for the table

  • 8/13/2019 09-ER to Relational Mapping

    26/30

    ER to Relational 1999-2002, NCST 26

    Enforcing Integrity Constraints

    Triggers*help enforce integrity constraints at

    the database/table/row level (although it can also

    be used for enforcing integrity at the field level)

    e.g. the total outstandings of a customer should notexceed the credit limit specified for him.

    * triggers are procedural, and fire (execute) when a specified event

    occurs - like an update on a row, or a deletion of row, etc..

  • 8/13/2019 09-ER to Relational Mapping

    27/30

    ER to Relational 1999-2002, NCST 27

    Declarative Constraints

    Enforcing entity integrity (primary key)

    create table employee(emp_id varchar2(10) primary key,

    emp_name varchar2(20),emp_address varchar2(80));

    create table salary_pmts (

    sal_emp varchar2(20),

    sal_datepd date,sal_amount number,

    primary key (sal_emp, sal_datepd));

  • 8/13/2019 09-ER to Relational Mapping

    28/30

  • 8/13/2019 09-ER to Relational Mapping

    29/30

    ER to Relational 1999-2002, NCST 29

    Enforcing other business rules

    create table employee(emp_id varchar2(10) primary key,emp_name varchar2(20),

    emp_address varchar2(80)

    emp_sex varchar2(1)

    check ( sex in (M,F)),

    emp_birthdt date,

    emp_joindt date,

    check ((emp_joindt - emp_birthdt/365)>18));

    all examples use SQL*Plus

  • 8/13/2019 09-ER to Relational Mapping

    30/30

    References

    Fundamentals of Database Systems

    R. Elmasri , S. NavatheAddison Wesley


Recommended