1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 2002-2008 1:1 Relationships These slides are licensed.

Slides:



Advertisements
Similar presentations
Physical Database Design and Tuning R&G - Chapter 20 Although the whole of this life were said to be nothing but a dream and the physical world nothing.
Advertisements

ER to Relational Mapping. Logical DB Design: ER to Relational Entity sets to tables. CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER,
Logical DB Design: ER to Relational Entity sets to tables. Employees ssn name lot CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER, PRIMARY.
Relational Database Design Via ER Modelling
BACS 485—Database Management Advanced SQL Overview Advanced DDL, DML, and DCL Commands.
Relational Database Design UNIT II 1. 2 Advantages of Using Database Systems Centralized control of a firm’s data Redundancy can be reduced (avoid keeping.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Tallahassee, Florida, 2014 COP4710 Database Systems Relational Model Fall 2014.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Entity-Relationship Model Chapter 2.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Entity-Relationship Model Chapter 2.
Normalisation The theory of Relational Database Design.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Natural Joins These slides are licensed.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Introduction to Conceptual Database Design.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Extended SQL & The Relational Calculus.
THE RELATIONAL DATABASE MODEL & THE DATABASE DEVELOPMENT PROCESS Fact of the Week: According to a Gartner study in ‘06, Microsoft SQL server had the highest.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
Chapter 6 Developing Data Models for Business Databases.
Michael F. Price College of Business Chapter 6: Logical database design and the relational model.
Ch 6: ER to Relational Mapping
ER to Relational Mapping. Logical DB Design: ER to Relational Entity sets to tables. CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER,
CS370 Spring 2007 CS 370 Database Systems Lecture 7 The Relational model.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Introduction to Objects & Databases These.
DATA MODELING AND DATABASE DESIGN
o At the end of this lesson, you will be able to:  Describe the life-cycle development phases  Discuss the theoretical and physical aspects of a relational.
RAJIKA TANDON DATABASES CSE 781 – Database Management Systems Instructor: Dr. A. Goel.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen M:N Relationships & Bridge Classes These.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relationship Classes & N-ary Relationships.
CMPE 226 Database Systems September 16 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Dependent & Identifying Relationships.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Basic Normal Forms These slides are licensed.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Conceptual State Constraints These slides.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Basic Relational Algebra These slides.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Advanced Normalization These slides are.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Introduction to Relational Databases &
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Cursors These slides are licensed under.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Union Subclasses & Disjoint 1:M Relationships.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
Database Design Sections 11 Database relationship, Integrity, keys, mapping conceptual model to logical/physical model Previous Section 12 – DDLesson11Fa12.ppt.
1 Functional Dependencies and Normalization Chapter 15.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relational Mapping with Constraints &
Database Design Sections 11 & 12 drawing conventions, generic model, integrity, keys, mapping conceptual model to logical/physical model.
11-1 Copyright  Oracle Corporation, All rights reserved. What Are Constraints? Constraints enforce rules at the table level. Constraints prevent.
Copyright  Oracle Corporation, All rights reserved. 11 Including Constraints.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relational State Constraints These slides.
11 Including Constraints Objectives At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints At the.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen The Relational Model & Relational Mapping.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Advanced Relational Algebra These slides.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subclasses These slides are licensed under.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Object-Relational Database Programming.
Lecture4: Informal guidelines for good relational design Mapping ERD to Relation Prepared by L. Nouf Almujally Ref. Chapter3 Lecture4 1.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relational State Assertions These slides.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Collection Operators These slides are.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Grouping These slides are licensed under.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Collection Operators These slides are.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Inner Joins These slides are licensed.
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
Lecture # 14 Chapter # 5 The Relational Data Model and Relational Database Constraints Database Systems.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Functional Dependencies & Normalization.
Relational Normalization Theory
Including Constraints
Developing Data Models – Conversion Rules
These are slides from Dr. Phil Cannata’s Class
Copyright © Ellis Cohen Consistency & Initialization
Copyright © Ellis Cohen
Presentation transcript:

1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen :1 Relationships These slides are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. For more information on how you may use them, please see

2 © Ellis Cohen Overview of Lecture 1:1 Relationships & Mappings 1:1 Mappings & Performance Modeling, Transforming & Mapping Parallel Relationships 1:1 Dependent Relationships 1:1 Identifying Relationships

3 © Ellis Cohen :1 Relationships & Mappings

4 © Ellis Cohen :1 Relationships DeskEmployee assigned to Each employee may be assigned to at most one desk Each desk may be assigned to at most one employee Assignment: Desk assigned to Employee Visual Conceptual Model (Crow Magnum) Textual Conceptual Model (Brief ConText)

5 © Ellis Cohen :1 Relationships EmployeeDesk sits at UML EmployeeDesk sits at Chen 0..1 EmployeeDesk EmployeeDesk Crow Magnum Easy Crow Magnum sits at

6 © Ellis Cohen Mapping of 1:1 Relationships Suppose some employees don't have desks & some desks don't have employees Desk Employee assigned to deskno height width depth empno Desks 40694… 15129… 30276… 81107… 60019… Emps empno ename desk references the employee (if any) who sits at it

7 © Ellis Cohen Alternate 1:1 Mapping Desk Employee assigned to deskno height width depth Desks 40694… 15129… … 81107… … Emps empno ename deskno Pick mapping that has fewest NULLS, or (more often) that has best performance for your operations employee references the desk (if any) that they sit at

8 © Ellis Cohen :1 Relational Fingerprint EmployeeDesk assigned to EmployeeDesk Relational Fingerprint for 1:1 relationship. Uniqueness indicator distinguishes 1:1 from 1:M Every employee has at most one desk Desks deskno height width depth empno ename deskno Emps Each desk is associated with at most a single employee! Desks deskno height width depth empno ename deskno Emps assigned to

9 © Ellis Cohen Combining Unique & FK Constraints Desks deskno height width depth empno ename deskno EmployeeDesk Emps Desks deskno height width depth empno ename deskno Emps Combine unique symbol with FK constraint assigned to

10 © Ellis Cohen Desks deskno height width depth Emps empno ename deskno Desks deskno height width depth empno Emps empno ename Alternate Relational Models Desks( deskno, height, width, depth) Emps( empno, ename, deskno) unique( deskno ) deskno references Desks Emps( empno, ename) Desks( deskno, height, width, depth, empno) unique( empno ) empno references Emps Unique references correspond to 1:1 relationships

11 © Ellis Cohen Mandatory 1:1 Relationships DeskEmployee assigned to What's the best relational mapping?

12 © Ellis Cohen Desks deskno height width depth Emps empno ename deskno ! Desks deskno height width depth empno Emps empno ename Mandatory 1:1 Enforcement Enforced directly by a non-null constraint State assertion must be separately enforced: (SELECT count(DISTINCT empno) FROM Desks) = (SELECT count(*) FROM Emps)

13 © Ellis Cohen Desks deskno height width depth Emps empno ename deskno ! SQL for Mandatory 1:1 CREATE TABLE Emps( empnonumber(4) primary key, enamevarchar(20), desknonumber(5) not null unique references Desks );

14 © Ellis Cohen :1 Mappings & Performance

15 © Ellis Cohen Desks deskno height width depth Emps empno ename deskno Desks deskno height width depth empno Emps empno ename Deskless Employee Problem Visual CONCEPTUAL Model (Crow Magnum) Visual RELATIONAL Model (Relational Schema) For both cases, write the SQL to list the empno's of employees without desks (don’t use subqueries) DeskEmployee assigned to

16 © Ellis Cohen Desks deskno height width depth Emps empno ename deskno Desks deskno height width depth empno Emps empno ename Deskless Employees SELECT empno FROM Emps WHERE deskno IS NULL SELECT empno FROM Desks NATURAL RIGHT JOIN Emps WHERE deskno IS NULL SELECT empno FROM Emps EXCEPT SELECT empno FROM Desks Pattern of queries may drive the choice of which mapping to use Oracle uses MINUS instead of EXCEPT

17 © Ellis Cohen Two Way Mapping ? Desks deskno height width depth empno Emps empno ename deskno DeskEmployee assigned to Would it ever make sense to represent a 1:1 relationship with references in both directions?

18 © Ellis Cohen Two Way Mapping Issues Desks deskno height width depth empno Emps empno ename deskno Pro: Allows more queries to be done without joins or collection operations Con: Adds redundancy and overhead Is there any way to allow cheaper queries without foreign keys in both directions?

19 © Ellis Cohen Desks deskno height width depth Emps empno ename deskno Desks deskno height width depth empno Emps empno ename hasDesk Alternative to Two Way Mapping SELECT empno FROM Emps WHERE deskno IS NULL SELECT empno FROM Emps WHERE hasDesk = 'FALSE' Still redundant, but cheaper to enforce

20 © Ellis Cohen Combine the Tables ? Why else is this design questionable? Wasted Space, Performance, Maintainability, Abstraction 40694… … … 81107… … empno ename deskno height width depth Desk not assigned to an employee Employee not assigned to a desk Neither empno nor deskno ALONE can be candidate keys, since both of them can be null. Also, most DBs will not allow even part of a primary key to be NULL!

21 © Ellis Cohen Bridge Table Representation Desk Employee assigned to deskno height width depth Desks 40694… 15129… 30276… 81107… 60019… empno … Emps deskno empno Assignments Used INFREQUENTLY for sparse relationships Can choose either key Assignments empno deskno Desks deskno height width depth Emps empno ename p.s. Good for sparse 1:M too!

22 © Ellis Cohen Mapping 1:1 Relationships One-way Reference –Direction generally picked based on # of NULLs Mandatory participation pattern of queries Two-way Reference –Arguable example of trading off cost of managing redundancy for increased query performance (but only do at end of design process after performance analysis) Combined Table –Generally a bad approach Bridge Table –Useful for 2-way sparse relationships (for both 1:1 and 1:M relationships)

23 © Ellis Cohen Modeling, Transforming & Mapping Parallel Relationships

24 © Ellis Cohen :M/1:1 Parallel Relationships DeptEmployee works for manages What's the best relational schema for this? There are many examples of parallel relationships between entity classes, which may be related to one another. In these cases, the obvious mapping may not necessarily be the best one empno ename deptno dname

25 © Ellis Cohen Combining Parallel Relationships 7369SMITH ALLEN BLAKE JONES20 empno ename deptno Emps deptno dname dmgr Depts works for manages 10ACCOUNTING RESEARCH SALES7698 DeptEmployee works for manages Emps empno ename deptno Depts deptno dname dmgr !

26 © Ellis Cohen Alternative Design 7369SMITH ALLEN BLAKE JONES20 empno ename deptno mdno Emps deptno dname Depts works for manages 10ACCOUNTING 20RESEARCH 30SALES DeptEmployee works for manages Emps empno ename deptno mdno Depts deptno dname But this has more NULLs and requires an assertion to enforce "each dept has a single dept mgr"

27 © Ellis Cohen Boolean Transformation 7369SMITH ALLEN BLAKE JONES20 Emps 7369SMITH ALLEN BLAKE JONES201 Emps empno ename deptno mdnoempno ename deptno isDMgr DeptEmployee works for manages empno ename deptno dname DeptEmployee works for empno ename isDMgr Make this change either in the Conceptual Model or just during Relational Mapping manages

28 © Ellis Cohen Extending Boolean Fields 7369SMITH ALLEN BLAKE JONES201 Emps 7369SMITH20CLERK 7499ALLEN30SALESMAN 7698BLAKE30DEPTMGR 7566JONES20DEPTMGR Emps empno ename deptno isDMgr empno ename deptno job

29 © Ellis Cohen :1 Dependent Relationships

30 © Ellis Cohen :1 Dependent Relationships Note that, when an Employee is deleted, their Benefits Membership should be deleted as well. That is, BenefitsMembership is dependent upon Employee Employee Benefits Membership has Suppose every employee who pays a nominal fee can get a Benefits Membership. But every Benefits Membership is associated with an employee

31 © Ellis Cohen Describing 1:1 Dependent Relationships [Employee:] BenefitsMembership( membid, … ) Visual Conceptual Model (Crow Magnum) Textual Conceptual Model (Brief ConText) Identifies a 1:1 dependent relationship Crow Magnum Easy Crow Magnum Employee Benefits Membership has Employee Benefits Membership has empno membid Lifetime Dependency: When an Employee is deleted, their Benefits Membership is deleted as well

32 © Ellis Cohen :M vs 1:1 Dependent Relationships 1:M1:1 Independent Mandatory Dependent Implied by dependent

33 © Ellis Cohen UML 1:1 Dependent Relationships UML Crow Magnum Employee Benefits Membership has empno ename membid paydate level Employee Benefits Membership has 0..1 PK empno ename PK membid paydate level This is the closest UML equivalent, although it implies composition as well as lifetime dependency & mandatory participation, which is not really accurate.

34 © Ellis Cohen Mapping 1:1 Dependent Relationships Emps empno ename BenefitMemberships membid empno ! paydate level Visual CONCEPTUAL Model (Crow Magnum) Visual RELATIONAL Model (Relational Schema) Employee Benefits Membership has empno ename membid paydate level BenefitsMembership( membid, empno, paydate, level) empno unique not null references Emps on delete cascade

35 © Ellis Cohen SQL for Dependent 1:1 CREATE TABLE BenefitsMembership( membidnumber(4) primary key, empnonumber(4) unique not null references Emps on delete cascade, paydatedate, levelchar ); Emps empno ename BenefitMemberships membid empno ! paydate level

36 © Ellis Cohen :M Relational Mappings Dept Div deptno divno Dept Div deptno Dept Div deptno divno Depts deptno divno ! Divs divno … Depts deptno divno ! Divs divno … Depts deptno divno Divs divno …

37 © Ellis Cohen :1 Relational Mappings Memb Emp membid empno Memb Emp membid Memb Emp membid empno Membs membid empno ! Emps empno … Membs membid empno ! Emps empno … Membs membid empno Emps empno …

38 © Ellis Cohen :1 Identifying Relationships

39 © Ellis Cohen :1 Relationships and Weak Identity There really is no reason for a Benefits Membership to have its own primary key. It can be uniquely identified by the primary key of its associated Employee It can use weak identity, resulting in a 1-1 identifying relationship Employee Benefits Membership has empno ename membid paydate level

40 © Ellis Cohen :1 Identifying Relationships [Employee:] BenefitsMembership( paydate, level ) Visual Conceptual Model (Crow Magnum) Textual Conceptual Model (Brief ConText) Identifies a 1:1 identifying relationship Crow Magnum Easy Crow Magnum Employee Benefits Membership has Employee Benefits Membership has empno paydate level paydate level Identifying: Weak Identity & Lifetime Dependency Instance Discriminator not needed

41 © Ellis Cohen :1 Relationships Independent Mandatory Mandatory Participation Dependent Mandatory Participation Lifetime Dependency Identifying Mandatory Participation Lifetime Dependency Weak Identity

42 © Ellis Cohen :M vs 1:1 Relationships 1:M1:1 Independent Mandatory Dependent Identifying

43 © Ellis Cohen UML 1:1 Identifying Relationships UML Crow Magnum Employee Benefits Membership has 0..1 PK empno ename paydate level Employee Benefits Membership has empno ename paydate level This is the closest UML equivalent, although it implies composition as well as lifetime dependency & mandatory participation, which is not really accurate

44 © Ellis Cohen level paydate Employee Chen 1:1 Identifying Relationships Chen Crow Magnum Employee Benefits Membership has empno ename paydate level Benefits Membership has ename empno

45 © Ellis Cohen Alternate Crow's Foot 1:1 Identifying Relationships Alternate Crow's Foot Crow Magnum Employee Benefits Membership has empno ename paydate level Employee Benefits Membership has In alternate Crow's Foot representations, Ordinary independent relationships are drawn with dashed lines, while continuous lines are only used for identifying relationships

46 © Ellis Cohen Mapping 1:1 Identifying Relationships Employee Benefits Membership has a Emps empno ename BenefitMemberships empno paydate level empno ename paydate level No instance discriminator needed Visual CONCEPTUAL Model (Crow Magnum) Visual RELATIONAL Model (Relational Schema) BenefitsMembership( empno, paydate, level) empno references Emps on delete cascade

47 © Ellis Cohen :M Relational Mappings Dept Div deptno divno Dept Div deptno Dept Div deptno Dept Div deptno divno Depts divno deptno Divs divno … Depts deptno divno ! Divs divno … Depts deptno divno ! Divs divno … Depts deptno divno Divs divno …

48 © Ellis Cohen :1 Relational Mappings Memb Emp membid empno Memb Emp membid Memb Emp membid Memb Emp empno Emps empno … Membs membid empno ! Emps empno … Membs membid empno ! Emps empno … Membs membid empno Emps empno … Membs empno

49 © Ellis Cohen Adding Non-Primary Candidate Key Employee Benefits Membership has a Emps empno ename BenefitMemberships empno membid ! paydate level empno ename membid ! {unique} paydate level Visual CONCEPTUAL Model (Crow Magnum) RELATIONAL Model (Relational Schema) Also make unique, but not the PK BenefitsMembership( empno, membid, paydate, level) empno references Emps on delete cascade membid unique not null

50 © Ellis Cohen SQL for Identifying 1:1 CREATE TABLE BenefitsMembership( empnonumber(4) primary key references Emps on delete cascade, membidnumber(4) unique not null, paydatedate, levelchar ); Emps empno ename BenefitMemberships empno membid ! paydate level

51 © Ellis Cohen Optional Composite Domains as Weak Entity Classes Employee empno enamecollegeinfo Employee empno ename College Info name year major 40694… 15129… 30276… 81107… 60019… empno ename Emps 40694……… 30276……… 81107……… empno name year major CollegeInfos Not every Employee has CollegeInfo

52 © Ellis Cohen The "is a" Relationship USPerson Employee is a USPersons ssno name address phone Emps ssno job sal comm The "is a" relationship is better represented using subclasses Visual CONCEPTUAL Model (Crow Magnum) Visual RELATIONAL Model (Relational Schema) Could instead use a 1:1 dependent relationship. Then Emps would have its own primary key.

53 © Ellis Cohen "is a" as a Subclass USPerson Employee is a USPerson Employee ssno name address phone job sal comm ssno name address phone job sal comm Subclass representation, with arrow pointing towards the superclass