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

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

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

The Relational Model. Chapter 3

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

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

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

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

The Relational Model 2. Week 3

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

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

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

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

Database Management Systems. Chapter 3 Part 2

The Entity-Relationship Model

High-Level Database Models (ii)

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

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

Database Systems ( 資料庫系統 )

The Entity-Relationship Model. Overview of Database Design

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

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

CIS 330: Applied Database Systems

Database Systems. Course Administration

High Level Database Models

The Relational Model of Data (ii)

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

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

Relational data model

The Relational Model (ii)

Database Applications (15-415)

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

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

The Relational Data Model. Data Model

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

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

Database Management Systems. Chapter 3 Part 1

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

The Entity-Relationship Model

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

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

The Entity-Relationship Model

CONCEPTUAL DESIGN: ER TO RELATIONAL TO SQL

OVERVIEW OF DATABASE DEVELOPMENT

CompSci 516 Database Systems. Lecture 2 SQL. Instructor: Sudeepa Roy

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

ER to Relational Mapping. ER Model: Overview

Database Management Systems. Session 2. Instructor: Vinnie Costa

Lecture 2 SQL. Announcements. Recap: Lecture 1. Today s topic. Semi-structured Data and XML. XML: an overview 8/30/17. Instructor: Sudeepa Roy

Database Management Systems. Chapter 2 Part 2

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

The Relational Model. Week 2

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

Lecture 2 SQL. Instructor: Sudeepa Roy. CompSci 516: Data Intensive Computing Systems

Database Applications (15-415)

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

Database Design & Deployment

The Relational Model

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

Introduction to Database Design

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

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

Announcements If you are enrolled to the class, but have not received the from Piazza, please send me an . Recap: Lecture 1.

The Entity-Relationship (ER) Model

Review. The Relational Model. Glossary. Review. Data Models. Why Study the Relational Model? Why use a DBMS? OS provides RAM and disk

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

Translating an ER Diagram to a Relational Schema

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

Administrivia. The Relational Model. Review. Review. Review. Some useful terms

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

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

The Relational Model

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

DBMS. Relational Model. Module Title?

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

The Entity-Relationship (ER) Model 2

The Entity-Relationship Model. Steps in Database Design

CSC 261/461 Database Systems Lecture 10

e e Conceptual design begins with the collection of requirements and results needed from the database (ER Diag.)

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Conceptual Design. The Entity-Relationship (ER) Model

Conceptual Design. The Entity-Relationship (ER) Model

MIS Database Systems Entity-Relationship Model.

COMP Instructor: Dimitris Papadias WWW page:

CS425 Midterm Exam Summer C 2012

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

Database Applications (15-415)

The Relational Model. Database Management Systems

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

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

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

Introduction to Database Design

Introduction to Databases

Database Applications (15-415)

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

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

Introduction to Database Design

Entity-Relationship Diagrams

CS 2451 Database Systems: Relational Data Model

Integrity constraints, relationships. CS634 Lecture 2

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

Transcription:

Introduction to Data Management Lecture #4 (E-R à Relational Design) Instructor: Mike Carey mjcarey@ics.uci.edu Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Announcements v Reminders: Sign up on Piazza! (A few still haven t done this...) v HW #1 is coming in for a landing Our startup: TrueOnlyUSNews.com Finalizing your E-R models... (HW #2 will come out on Friday) v Today: More on Conceptual DB Design Intro to the Relational model E-R à Relational translation v Any lingering Q s from last time? Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2

Foreign Keys, Referential Integrity v Foreign key : Set of fields in one relation used to refer to a tuple in another relation. (Must refer to the primary key of the other relation.) Like a logical pointer. v E.g., sid is a foreign key referring to Students: Enrolled(sid: string, cid: string, grade: string) If all foreign key constraints are enforced, referential integrity is achieved, i.e., no dangling references. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3 Foreign Keys in SQL v Ex: Only students listed in the Students relation should be allowed to enroll for courses. CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid, cid), FOREIGN KEY (sid) REFERENCES Students ) Enrolled sid cid grade 53666 Carnatic101 C 53666 Reggae203 B 53650 Topology112 A 53666 History105 B Students sid login age gpa 53666 Jones jones@cs 18 3.4 53688 Smith smith@eecs 18 3.2 53650 Smith smith@math 19 3.8 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4

Enforcing Referential Integrity v Consider Students and Enrolled; sid in Enrolled is a foreign key that references Students. v What should be done if an Enrolled tuple with a non-existent student id is inserted? (Reject it!) v What should be done if a Students tuple is deleted? Also delete all Enrolled tuples that refer to it. Or... Disallow deletion of a Students tuple if it is referred to. Set sid in Enrolled tuples that refer to it to a default sid. (In SQL, also: Set sid in Enrolled tuples that refer to it to a special value null, denoting `unknown or `inapplicable.) v Similar if primary key of Students tuple is updated. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5 Referential Integrity in SQL v SQL/92 and SQL:1999 support all 4 options on deletes and updates. Default is NO ACTION (delete/update is rejected) CASCADE (also delete all tuples that refer to the being-deleted tuple) SET NULL / SET DEFAULT (sets foreign key value of the refering tuples) CREATE TABLE Enrolled (sid CHAR(20), cid CHAR(20), grade CHAR(2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students ON DELETE CASCADE ON UPDATE SET DEFAULT ) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6

Where do ICs Come From? v ICs are based upon the semantics of the realworld enterprise that is being described in the database relations (perhaps via an ER schema) v We can check a database instance to see if an IC is violated, but we can NEVER infer that an IC is true by looking at an instance. An IC is a statement about all possible instances! From example, we know is not a key, but the assertion that sid is a key is given to us. v Key and foreign key ICs are the most common; more general ICs supported too. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 7 Logical DB Design: ER to Relational v Entity sets to tables: CREATE TABLE ( CHAR(11), CHAR(20), INTEGER, PRIMARY KEY ()) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 8

Relationship Sets to Tables v In translating a relationship set to a relation, attributes of the relation must include: Keys for each participating entity set (as foreign keys). This set of attributes forms a superkey for the relation. All descriptive attributes. since CREATE TABLE Works_In( CHAR(11), did INTEGER, since DATE, PRIMARY KEY (, did), FOREIGN KEY () REFERENCES, FOREIGN KEY (did) REFERENCES Departments) did d budget WorksIn Departments Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 9 Key Constraints (Review) v Each dept has at most one manager, according to the key constraint on Manages. since d did budget 1 N Manages Departments Translation to relational model? 1-to-1 1-to Many Many-to-1 Many-to-Many Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 10

Translating ER Diagrams with Key Constraints v Map the relationship to a table (Manages): Note that did (alone) is the key! Still separate tables for and Departments. v But, since each department has a unique manager, we could choose to fold Manages right into Departments. (Q: Why do that...?) CREATE TABLE Manages ( CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY () REFERENCES, FOREIGN KEY (did) REFERENCES Departments) CREATE TABLE Departments2 ( did INTEGER, d CHAR(20), budget REAL, Note: The relationship mgr_ CHAR(11), info has been pushed to mgr_since DATE, the N-side s entity table! PRIMARY KEY (did), FOREIGN KEY (mgr_) REFERENCES ) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 11 vs. Properly Reflecting Key Constraints fooid foo1 foo2 baz barid bar1 bar2 Foo M 1? N 1? FooBar Bar v So what are the translated relationship table s keys (etc.) when FooBar is M:N? à FooBar(fooId, barid, baz) FooBar is N:1? à FooBar(fooId, barid, baz) Foobar is 1:N? à FooBar(fooId, barid, baz) Foobar is 1:1? à FooBar(fooId, barid, baz) (Note: unique) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 12

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!!) since did d budget 1 N Manages Departments M Works_In N since Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 13 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 14

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. cost p age 1 N Policy Dependents Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 15 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 16

Review: ISA Hierarchies 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 17 Translating 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(,,, 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 18