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

Slides:



Advertisements
Similar presentations
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,
Advertisements

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. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
The Entity-Relationship Model
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Entity-Relationship Model Chapter 2.
Book Chapter 3 (part 2 ) From ER to Relational Model.
1 541: Database Systems S. Muthu Muthukrishnan. 2 Overview of Database Design  Conceptual design: (ER Model is used at this stage.)  What are the entities.
The Entity-Relationship (ER) Model
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Conceptual Design and The Entity-Relationship Model
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke. Edited by Keith Shomper, The Relational Model Chapter 3.
COMP 5138 Relational Database Management Systems Sem2, 2007 Lecture 3 Relational Data Model.
CSC 411/511: DBMS Design 1 1 Dr. Nan WangCSC411_L3_Relational Model 1 The Relational Model (Chapter 3)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Entity-Relationship Model Chapter 2.
Database Management Systems 1 Raghu Ramakrishnan The Relational Model Chapter 3 Instructor: Mirsad Hadzikadic.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
Modeling Your Data Chapter 2. Overview of Database Design Conceptual design: –What are the entities and relationships in the enterprise? – What information.
The Relational Model 198:541 Rutgers University. Why Study the Relational Model?  Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle,
Conceptual Design Using the Entity-Relationship (ER) Model
The Entity-Relationship (ER) Model CS541 Computer Science Department Rutgers University.
1 Data Modeling Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
1 Data Modeling Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Entity-Relationship Model Chapter 2.
ER continued, and ER to Relational Mappings R&G Chapters 2, 3 Lecture 22.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
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,
1 The Relational Model Chapter 3. 2 Objectives  Representing data using the relational model.  Expressing integrity constraints on data.  Creating,
The Relational Model Chapter 3
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3 Modified by Donghui Zhang.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model  Vendors: IBM, Informix, Microsoft, Oracle, Sybase  Recent.
 Relational database: a set of relations.  Relation: made up of 2 parts: › Instance : a table, with rows and columns. #rows = cardinality, #fields =
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
Christoph F. Eick: Designing E/R Diagrams 1 The Entity-Relationship Model Chapter 3+4.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
The Relational Model1 ER-to-Relational Mapping and Views.
ICS 321 Fall 2009 The Relational Model (ii) Asst. Prof. Lipyeow Lim Information and Computer Science Department University of Hawaii at Manoa 9/10/20091Lipyeow.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Management Systems,1 Conceptual Design Using the Entity-Relationship (ER) Model.
Mapping E/R Diagrams to Relational Database Schemas
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 3 The Relational Model.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
ER & Relational: Digging Deeper R &G - Chapters 2 & 3.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Mapping E/R to RM, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 Mapping E/R Diagrams to Relational Database Schemas Second Half of Chapter.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #5 (E-R  Relational, Cont.) Instructor: Chen Li.
Database Management Systems 1 Raghu Ramakrishnan The Relational Model Chapter 3 Instructor: Xin Zhang.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Multi-billion dollar industry, $15+ bill in  Vendors:
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Entity-Relationship Model Chapter 2.
Logical DB Design: ER to Relational
COP Introduction to Database Structures
These slides are based on the slides of your text book.
The Relational Model Lecture 3 INFS614, Fall
The Relational Model Chapter 3
From ER to Relational Model
The Relational Model Chapter 3
The Relational Model Chapter 3
The Entity-Relationship Model
The Relational Model Chapter 3
The Relational Model Chapter 3
Presentation transcript:

Lecture 3 Book Chapter 3 (part 2 ) From ER to Relational

2  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Logical DB Design: ER to Relational  Translate Entity sets to tables: CREATE TABLE Employees (ssn CHAR(11), name CHAR(20), lot INTEGER, PRIMARY KEY (ssn)) Employees ssn name lot

3  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Translate Relationship Sets to Tables  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.(why?)  All descriptive attributes. CREATE TABLE Works_In( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (ssn, did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments) lot dname budget did since name Works_In DepartmentsEmployees ssn

4  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Review: Key Constraints  Each dept has at most one manager, according to the key constraint on Manages.  Each department appears only once in relationship Translation to relational model? Many-to-Many1-to-11-to ManyMany-to-1 dname budget did since lot name ssn Manages Employees Departments

5  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Translate Key Constraints  Approach I  Separate tables for Employees and Departments.  Note that did is the key now! TABLE Dept_mgr(…) TABLE Employee (…) CREATE TABLE Manages( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments)

6  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Translate Key Constraints  Approach II  Combine Manages and Departments.  Each department has a unique manager TABLE Employee (…) CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees)

7  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Translate Key Constraints  Approach I  Separate tables for Employees and Departments.  Note that did is the key now!  Approach II  Combine Manages and Departments.  Each department has a unique manager TABLE Dept_mgr(…) TABLE Employee (…) CREATE TABLE Manages( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments) TABLE Employee (…) CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees)

8  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Review: Participation Constraints  Every department must have a manager ! Every did value in Departments table must appear in a row of the Manages table (with a non-null ssn value!) lot name dname budgetdid since name dname budgetdid since Manages since Departments Employees ssn Works_In

9  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Participation Constraints in SQL  Approach I. every did value in Department appears in a tuple of Works_In the corresponding tuple must have a non-null ssn values Is that capture sufficient ? TABLE Dept_mgr(…) TABLE Employee (…) CREATE TABLE Manages( ssn CHAR(11) NOT NULL, did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments,

10  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Participation Constraints in SQL  Approach I. every did value in Department appears in a tuple of Works_In the corresponding tuple must have a non-null ssn values TABLE Dept_mgr(…) TABLE Employee (…) CREATE TABLE Manages( ssn CHAR(11) NOT NULL, did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments, CHECK (…))

11  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Participation Constraints in SQL  Approach II. - capture participation constraints involving one entity set in a binary relationship, - but little else (without resorting to CHECK constraints). CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11) NOT NULL, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE NO ACTION)

12  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Participation Constraints in SQL  What if we want to capture participation for many-to- many relationships?  What if we want to capture three-way relationships?  Anwer: We need to use CHECK constraints.

13  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Review: Weak Entities  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 name age pname Dependents Employees ssn Policy cost

14  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Translating Weak Entity Sets  Weak entity set and identifying relationship set are translated into a single table.  When the owner entity is deleted, all owned weak entities must also be deleted. CREATE TABLE Dep_Policy ( pname CHAR(20), age INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARY KEY (pname, ssn), FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE CASCADE)

15  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Review: ISA Hierarchies Contract_Emps name ssn Employees lot hourly_wages ISA Hourly_Emps contractid hours_worked  As in C++, or other PLs, attributes are inherited.  If we declare A ISA B, every A entity is also considered to be a B entity.  Overlap constraints : Can Joe be an Hourly_Emps as well as a Contract_Emps entity? ( Allowed/disallowed )  Covering constraints : Does every Employees entity also have to be an Hourly_Emps or a Contract_Emps entity? (Yes/no)

16  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Translating ISA Hierarchies  General approach:  3 relations: Employees, Hourly_Emps and Contract_Emps. Employees ( ssn, name, lot) Hourly_Emps (ssn, hourly_wages, hours_worked) ; Contract_Emps (ssn, contractid); delete Hourly_Emps tuple if referenced Employees tuple is deleted (how?) + Queries involving all employees easy - Queries involving just Hourly_Emps require a Join.  Alternative: Just Hourly_Emps and Contract_Emps. Hourly_Emps (ssn, name, lot, hourly_wages, hours_worked) Hourly_Emps ( ssn, name, lot, contractid)  Each employee must be in one of these two subclasses.

17  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Review: Binary vs. Ternary Relationships  New requirements: 1. A policy cannot be owned by two or more employees. (Key Constraints) 2. Every policy must be owned by some employee (Total participation) 3. Dependents is a weak entity (Weak Entity)  What are the additional constraints in the 2nd diagram? Why? (side-effect: one policy can only cover one dependent) age pname Dependents Covers name Employees ssn lot Policies policyid cost Beneficiary age pname Dependents policyid cost Policies Purchaser name Employees ssn lot Bad design Better design

18  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Binary vs. Ternary Relationships  The key constraints allow us to combine Purchaser with Policies and Beneficiary with Dependents.  Participation constraints lead to NOT NULL constraints.  What if Policies is a weak entity set? ON DELETE CASCADE CREATE TABLE Policies ( policyid INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARY KEY (policyid). FOREIGN KEY (ssn) REFERENCES Employees) CREATE TABLE Dependents ( pname CHAR(20), age INTEGER, policyid INTEGER, PRIMARY KEY (pname, policyid). FOREIGN KEY (policyid) REFERENCES Policies, ON DELETE CASCADE)

19  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Views  A view is just a relation, but we store a definition, rather than a set of tuples. (Virtual View) CREATE VIEW YoungActiveStudents (name, sid, course) AS SELECT S.name, S.sid, E.cid FROM Students S, Enrolled E WHERE S.sid = E.sid  Views can be dropped using the DROP VIEW command.  How to handle DROP TABLE if there’s a view on the table? DROP TABLE { RESTRICT | CASCADE }

20  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Views and Security  Views can be used to present necessary information (or a summary), while hiding details in underlying relation(s).  Given YoungStudents view only (not Students or Enrolled table)  User can find students who have enrolled, but not the grades of the courses they got.

21  Relational Data Model  Relational Query Language  Integrity Constraints  ER to Relational Relational Model: Summary  A tabular representation of data.  Simple and intuitive, currently the most widely used.  Integrity constraints can be specified by the DBA, based on application semantics. DBMS checks for violations.  Two important ICs: primary and foreign keys  In addition, we always have domain constraints.  Powerful and natural query languages exist.  Rules to translate ER to relational model