Introduction to Data Management. Lecture #6 E-Rà Relational Mapping (Cont.)

Similar documents
Introduction to Data Management. Lecture #5 (E-R Relational, Cont.)

Introduction to Data Management. Lecture #4 (E-R à Relational Design)

The Relational Model (ii)

Database Management Systems. Chapter 3 Part 2

The Relational Model. Chapter 3. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

The Relational Model 2. Week 3

The Relational Model. Chapter 3

Introduction to Data Management. Lecture #7 (Relational Design Theory)

The Entity-Relationship Model. Overview of Database Design

CIS 330: Applied Database Systems. ER to Relational Relational Algebra

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

The Entity-Relationship Model

High-Level Database Models (ii)

Why Study the Relational Model? The Relational Model. Relational Database: Definitions. The SQL Query Language. Relational Query Languages

Relational Model. Topics. Relational Model. Why Study the Relational Model? Linda Wu (CMPT )

High Level Database Models

Introduction to Data Management. Lecture #3 (Conceptual DB Design)

The Relational Model. Outline. Why Study the Relational Model? Faloutsos SCS object-relational model

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

The Entity-Relationship Model

Introduction to Data Management. Lecture #3 (E-R Design, Cont d.)

Introduction to Data Management. Lecture #3 (Conceptual DB Design) Instructor: Chen Li

Introduction to Data Management. Lecture #3 (Conceptual DB Design)

The Entity-Relationship Model

The Relational Model. Roadmap. Relational Database: Definitions. Why Study the Relational Model? Relational database: a set of relations

CIS 330: Applied Database Systems

ER to Relational Mapping. ER Model: Overview

Overview of db design Requirement analysis Data to be stored Applications to be built Operations (most frequent) subject to performance requirement

From ER to Relational Model. Book Chapter 3 (part 2 )

CONCEPTUAL DESIGN: ER TO RELATIONAL TO SQL

The Entity-Relationship Model. Overview of Database Design. ER Model Basics. (Ramakrishnan&Gehrke, Chapter 2)

Relational Databases BORROWED WITH MINOR ADAPTATION FROM PROF. CHRISTOS FALOUTSOS, CMU /615

Database Management Systems. Chapter 2 Part 2

OVERVIEW OF DATABASE DEVELOPMENT

Data Modeling. Yanlei Diao UMass Amherst. Slides Courtesy of R. Ramakrishnan and J. Gehrke

The Entity-Relationship (ER) Model

Database Systems ( 資料庫系統 )

Database Applications (15-415)

Introduction to Data Management. Lecture #4 (E-R Relational Translation)

Database Systems. Course Administration

Introduction to Database Design

Introduction to Data Management. Lecture #5 Relational Model (Cont.) & E-Rà Relational Mapping

Introduction to Data Management. Lecture #4 E-R Model, Still Going

Database Applications (15-415)

Introduction to Database Design. Dr. Kanda Runapongsa Dept of Computer Engineering Khon Kaen University

CIS 330: Web-driven Web Applications. Lecture 2: Introduction to ER Modeling

CS/INFO 330 Entity-Relationship Modeling. Announcements. Goals of This Lecture. Mirek Riedewald

Contents. Database. Information Policy. C03. Entity Relationship Model WKU-IP-C03 Database / Entity Relationship Model

Database Applications (15-415)

COMP Instructor: Dimitris Papadias WWW page:

The Entity-Relationship Model. Steps in Database Design

Databases Model the Real World. The Entity- Relationship Model. Conceptual Design. Steps in Database Design. ER Model Basics. ER Model Basics (Contd.

Database Design & Deployment

Conceptual Design. The Entity-Relationship (ER) Model

The Entity-Relationship (ER) Model 2

MIS Database Systems Entity-Relationship Model.

LAB 3 Notes. Codd proposed the relational model in 70 Main advantage of Relational Model : Simple representation (relationstables(row,

Database Design. ER Model. Overview. Introduction to Database Design. UVic C SC 370. Database design can be divided in six major steps:

Introduction to Database Design

Overview. Introduction to Database Design. ER Model. Database Design

SYLLABUS ADMIN DATABASE SYSTEMS I WEEK 2 THE ENTITY-RELATIONSHIP MODEL. Assignment #2 changed. A2Q1 moved to A3Q1

Database Management Systems. Session 2. Instructor: Vinnie Costa

Introduction to Database Design

Modeling Your Data. Chapter 2. cs542 1

Entity-Relationship Diagrams

Translating an ER Diagram to a Relational Schema

ER Model Overview. The Entity-Relationship Model. Database Design Process. ER Model Basics

Database Management Systems. Chapter 3 Part 1

Database Design CENG 351

Conceptual Design. The Entity-Relationship (ER) Model

CSC 261/461 Database Systems Lecture 10

Introduction to Data Management. Lecture 16 (SQL: There s STILL More...) It s time for another installment of...

The Relational Model

Translation of ER-diagram into Relational Schema. Dr. Sunnie S. Chung CIS430/530

Database Systems. Lecture2:E-R model. Juan Huo( 霍娟 )

Databases Model the Real World. The Entity- Relationship Model. Conceptual Design. Steps in Database Design. ER Model Basics. ER Model Basics (Contd.

Integrity constraints, relationships. CS634 Lecture 2

The Relational Data Model. Data Model

Comp 5311 Database Management Systems. 4b. Structured Query Language 3

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

Database Management Systems. Syllabus. Instructor: Vinnie Costa

Database Applications (15-415)

Introduction to Databases

CSIT5300: Advanced Database Systems

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

The Relational Model

Translation of ER-diagram into Relational Schema. Dr. Sunnie S. Chung CIS430/530

Introduction to Data Management. Lecture #17 (SQL, the Never Ending Story )

Introduction to Data Management. Lecture #1 (The Course Trailer )

The Relational Model

ER Model. CSC 343 Winter 2018 MICHAEL LIUT

CSE 530A. ER Model to Relational Schema. Washington University Fall 2013

CSC 261/461 Database Systems Lecture 8. Spring 2018

The Relational Model. Week 2

Introduction to Data Management. Lecture #2 (Big Picture, Cont.)

Relational model continued. Understanding how to use the relational model. Summary of board example: with Copies as weak entity

CSE 344 AUGUST 1 ST ENTITIES

VARDHAMAN COLLEGE OF ENGINEERING Shamshabad , Hyderabad B.Tech. CSE IV Semester (VCE - R11) T P C 3+1* -- 4 (A1511) DATABASE MANAGEMENT SYSTEMS

CSC 261/461 Database Systems Lecture 8. Fall 2017

Transcription:

Introduction to Data Management Lecture #6 E-Rà Relational Mapping (Cont.) Instructor: Mike Carey mjcarey@ics.uci.edu Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 It s time again for... Brought to you by Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2

Today s Reminders v Continue to follow the course wiki page http://www.ics.uci.edu/~cs122a/ v Continue to live by the Piazza page https://piazza.com/uci/spring2018/cs122a/home v First HW assignment is due today Up to 24 hours to finish with a 20% late penalty v Next HW assignment is available now Translate E-R PEEEza schema into relational form Use our solution schema (out tomorrow at 5pm) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3 Review: Participation Constraints v Does every department have a manager? If so, this is a participation constraint: the participation of Departments in Manages is said to be total (vs. partial). Every did value in Departments table must appear in a row of the Manages table (with a non-null value!!) lot since did d budget Manages Departments M Works_In N since Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4

Participation Constraints in SQL v We can capture participation constraints involving one entity set in a binary relationship, but little else (without resorting to the use of triggers). CREATE TABLE Department2 ( did INTEGER, d CHAR(20), budget REAL, mgr_ CHAR(11) NOT NULL, mgr_since DATE, PRIMARY KEY (did), FOREIGN KEY (mgr_) REFERENCES, ON DELETE NO ACTION*) (*or: RESTRICT) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5 Review: Weak Entities v A weak entity can be identified (uniquely) only by considering the primary key of another (owner) entity. Owner entity set and weak entity set must participate in a one-to-many relationship set (1 owner, many weak entities). Weak entity set must have total participation in this identifying relationship set. lot cost p age Policy Dependents Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6

Translating Weak Entity Sets v Weak entity set and identifying relationship set are translated into a single table. When the owner entity is deleted, all of its owned weak entities must also be deleted. CREATE TABLE Dependents2 ( p CHAR(20), age INTEGER, cost REAL, CHAR(11) NOT NULL, PRIMARY KEY (p, ), FOREIGN KEY () REFERENCES, ON DELETE CASCADE) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 7 Review: ISA Hierarchies lot v As in C++, or other PLs, attributes are inherited. hourly_wages v If we declare A ISA B, then every A entity is also considered to be a B entity. hours_worked ISA contractid Hourly_Emps Contract_Emps v Overlap constraints: Can employee Joe be an Hourly_Emps as well as a Contract_Emps entity? (Allowed/disallowed) v Covering constraints: Must each entity be either an Hourly_Emps or a Contract_Emps entity? (Yes/no) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 8

From ISA Hierarchies to Relations v Most general and clean approach (recommended): 3 relations:, Hourly_Emps, and Contract_Emps. Hourly_Emps: Every employee recorded in. For hourly emps, extra info recorded in Hourly_Emps (hourly_wages, hours_worked, ); delete Hourly_Emps tuple if referenced tuple is deleted. Queries about all employees easy; those involving just Hourly_Emps require a join to access the extra attributes. v Another alternative: Hourly_Emps and Contract_Emps. Ex: Hourly_Emps(,, lot, hourly_wages, hours_worked) If each employee must be in one of the two subclasses... (Q: Can we always do this, then? A: Not w/o redundancy!) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 9 ISA Hierarchy Translation Options v I. Delta table approach (recommended): Emps(,, lot) ß (All Emps partly reside here) Hourly_Emps(, wages, hrs_worked) Contract_Emps(, contractid) v II. Union of tables approach: Emps(,, lot) Things to consider: Expected queries? PK/unique constraints? Relationships/FKs? Overlap constraints? Space/time tradeoffs? Hourly_Emps(,, lot, wages, hrs_worked) Contract_Emps(,, lot, contractid) v III. Mashup table approach: Emps(kind,,, lot, wages, hrs_worked, contractid) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 10

ISA Considerations (cont d.) v Query convenience Ex: List the s of all Emps in lot 12A v PK enforcement Ex: Make sure that is unique for all Emps v Relationship targets Ex: Lawyers table REFERENCES Contract_Emps v Handling of overlap constraints Ex: Sally is under a contract for her hourly work v Space and query performance tradeoffs Ex: List all the info about hourly employee 123 Ex: What if most employees are just plain employees? Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 11 Mapping Advanced ER Features v v Multi-valued (vs. single-valued) attributes phone v Derived (vs. base/stored) Composite (vs. atomic) attributes address _phones(, phone) is an FK in this table (, phone) is its PK snum street attributes bdate (,, address_snum, address_street, address_city, address_zip) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 12 city zip age

SQL Views (and Security) v A view is just a relation, but we store its definition rather than storing the (materialized) set of tuples. CREATE VIEW YoungActiveStudents (, grade) AS SELECT S., E.grade FROM Students S, Enrolled E WHERE S.sid = E.sid and S.age < 21 v Views can be used to present needed information while hiding details of underlying table(s). Given YoungStudents (but not Students or Enrolled), we can see (young) students S who have are enrolled but not see the cid s of their courses. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 13 SQL Views (Cont d.) v Other view uses in our ER translation context might include: Derived attributes, e.g., age (vs. birthdate) Simplifying/eliminating join paths (for SQL) Beautifying the Mashup table approach (to ISA) CREATE VIEW EmployeeView (,, bdate, age) AS SELECT E., E., E.bdate, TIMESTAMPDIFF(YEAR, E.bdate, CURDATE( )) FROM E Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 14

Another Mapping Example: Binary vs. Ternary Relationships lot p age ( Better design ) Purchaser Beneficiary Dependents Policies policyid cost Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 15 Binary vs. Ternary Relationships (Cont d.) v The key constraints let us combine Purchaser with Policies and Beneficiary with Dependents. v Participation constraints lead to NOT NULL constraints. (Note: Primary key attributes are all NOT NULL as well check documentation to see if that s implicit or explicit! CREATE TABLE Policies ( policyid INTEGER, cost REAL, emp_ CHAR(11) NOT NULL, PRIMARY KEY (policyid). FOREIGN KEY (emp_) REFERENCES ON DELETE CASCADE) CREATE TABLE Dependents ( p CHAR(20), age INTEGER, policyid INTEGER) NOT NULL, PRIMARY KEY (p, policyid), FOREIGN KEY (policyid) REFERENCES Policies ON DELETE CASCADE) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 16

Review: Binary vs. Ternary Relationships lot CREATE TABLE ( CHAR(11), CHAR(20), lot INTEGER, PRIMARY KEY ()) p age CREATE TABLE Policies ( policyid INTEGER, cost REAL, emp_ CHAR(11) NOT NULL, PRIMARY KEY (policyid). FOREIGN KEY (emp_) REFERENCES ON DELETE CASCADE) Purchaser policyid Policies Beneficiary Dependents Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 17 cost CREATE TABLE Dependents ( p CHAR(20), age INTEGER, policyid INTEGER) NOT NULL, PRIMARY KEY (p, policyid), FOREIGN KEY (policyid) REFERENCES Policies ON DELETE CASCADE) Review: Putting The Basics Together cid c login oid shipto total Customer 1 Placed N Order 1 sku p color listprice N Has N LineItem lno price Product 1 For qty Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 18

Review: Putting It Together (Cont d.) CREATE TABLE Customer ( cid INTEGER, c VARCHAR(50), login VARCHAR(20) NOT NULL, PRIMARY KEY (cid), UNIQUE (login)) CREATE TABLE Product ( sku INTEGER, p VARCHAR(100), color VARCHAR(20), listprice DECIMAL(8,2), PRIMARY KEY (sku)) CREATE TABLE Order ( oid INTEGER, custid INTEGER, shipto VARCHAR(200), total DECIMAL(8,2), PRIMARY KEY (oid), FOREIGN KEY (custid) REFERENCES Customer)) CREATE TABLE LineItem ( oid INTEGER, lno INTEGER, price DECIMAL(8,2), qty INTEGER, sku INTEGER, PRIMARY KEY (oid, lno), FOREIGN KEY (oid) REFERENCES Order ON DELETE CASCADE), FOREIGN KEY (sku) REFERENCES Product)) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 19 Review: Putting It Together (Cont d.) Customer Product Order cid c login 1 Smith, James jsmith@aol.com 2 White, Susan suzie@gmail.com 3 Smith, James js@hotmail.com sku p color listprice 123 Frozen DVD null 24.95 456 Graco Twin Stroller green 199.99 789 Moen Kitchen Sink black 350.00 LineItem oid custid shipto total oid lno price qty item 1 3 J. Smith, 1 Main St., USA 199.95 1 1 169.95 1 456 2 1 Mrs. Smith, 3 State St., USA 300.00 1 2 15.00 2 123 2 1 300.00 1 789 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 20

Relational Model and E-R Schema Translation: Summary v Relational model: a tabular representation of data. v Simple and intuitive, also widely used. v Integrity constraints can be specified by the DBA based on application semantics. DBMS then checks for violations. Two important ICs: Primary and foreign keys (PKs, FKs). In addition, we always have domain constraints. v Powerful and natural query languages exist (soon!) v Rules to translate E-R to relational model Can be done by a human, or automatically (using a tool) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 21