1 CS122A: Introduction to Data Management Lecture #5 (E-R  Relational, Cont.) Instructor: Chen Li.

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.
The Relational Model CMU SCS /615 C. Faloutsos – A. Pavlo Lecture #3 R & G, Chap. 3.
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.
09/03/2009Lipyeow Lim -- University of Hawaii at Manoa 1 ICS 321 Fall 2009 Introduction to Database Design Asst. Prof. Lipyeow Lim Information & Computer.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Mapping E/R Diagrams to Relational Database Schemas
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
Lecture 3 Book Chapter 3 (part 2 ) From ER to Relational.
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.
The Entity-Relationship (ER) Model. Overview of db design Requirement analysis – Data to be stored – Applications to be built – Operations (most frequent)
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.
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:
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:

1 CS122A: Introduction to Data Management Lecture #5 (E-R  Relational, Cont.) Instructor: Chen Li

2 Logical DB Design: ER to Relational (Review)  Entity sets to tables: CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER, PRIMARY KEY (ssn)) Employees ssn name lot

3 Relationship Sets to Tables (Review)  In translating a relationship set to a relation, attributes of the relation must include:  Keys for each participating entity set (as foreign keys). Note: This set of attributes forms a superkey for the relation.  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) dname budget did since lot name ssn WorksIn Employees Departments MN

4 Key Constraints (Review)  Each dept has at most one manager, according to the key constraint on Manages. Translation to relational model? Many-to-Many1-to-11-to ManyMany-to-1 dname budget did since lot name ssn Manages Employees Departments 1N

5 Translating ER Diagrams with Key Constraints  Map the relationship to a table (Manages):  Note that did is the key now!  Separate tables for Employees and Departments.  But, since each department has a unique manager, we could choose to fold Manages right into Departments. CREATE TABLE Manages ( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (did) REFERENCES Departments) CREATE TABLE Departments2 ( did INTEGER, dname CHAR(20), budget REAL, mgr_ssn CHAR(11), mgr_since DATE, PRIMARY KEY (did), FOREIGN KEY (mgr_ssn) REFERENCES Employees) vs. (Q: Why do that...?) Note: The relationship info has been pushed to the N-side’s entity table!

6 Properly Reflecting Key Constraints  So what are the translated relationship table keys ( etc. ) when…  FooBar is M:N?  FooBar is N:1?  Foobar is 1:N?  Foobar is 1:1? bar1 bar2 barId baz foo2 foo1 fooId FooBar Foo Bar  FooBar(fooId, barId, baz)  FooBar(fooId, barId, baz) (Note: unique) MNM11N11??

7 Review: Participation Constraints  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 ssn value!!) lot name dname budgetdid name dname budgetdid since Manages since Departments Employees ssn Works_In 1N

8 Participation Constraints in SQL  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, dname CHAR(20), budget REAL, mgr_ssn CHAR(11) NOT NULL, mgr_since DATE, PRIMARY KEY (did), FOREIGN KEY (mgr_ssn) REFERENCES Employees, ON DELETE NO ACTION )

9 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 1N

10 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 Dependents2 ( pname CHAR(20), age INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARY KEY (pname, ssn), FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE CASCADE )

11 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, then 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)

12 Translating ISA Hierarchies to Relations  Most general approach:  3 relations: Employees, Hourly_Emps and Contract_Emps. Hourly_Emps : Every employee recorded in Employees. For hourly emps, extra info recorded in Hourly_Emps ( hourly_wages, hours_worked, ssn) ; delete Hourly_Emps tuple if referenced Employees tuple is deleted. Queries about all employees easy; those involving just Hourly_Emps require a join to get the extra attributes.  An alternative: Hourly_Emps and Contract_Emps.  Hourly_Emps : ssn, name, lot, hourly_wages, hours_worked.  Each employee must be in one of these two subclasses. (Q: Can we always do this, then? A: Not w/o redundancy!)

13 Review: Binary vs. Ternary Relationships Beneficiary age pname Dependents policyid cost Policies Purchaser name Employees ssn lot “Better design” 1N 1N

14 Binary vs. Ternary Relationships (Contd.)  The key constraints let us combine Purchaser with Policies and Beneficiary with Dependents.  Participation constraints lead to NOT NULL constraints. ( Note: Primary key attributes are NOT NULL as well – check documentation to see if that’s implicit or explicit! CREATE TABLE Policies ( policyid INTEGER, cost REAL, emp_ssn CHAR(11) NOT NULL, PRIMARY KEY (policyid). FOREIGN KEY (emp_ssn) REFERENCES Employees ON DELETE CASCADE ) CREATE TABLE Dependents ( pname CHAR(20), age INTEGER, policyid INTEGER, PRIMARY KEY (pname, policyid), FOREIGN KEY (policyid) REFERENCES Policies ON DELETE CASCADE )

15 Review: Binary vs. Ternary Relationships Beneficiary age pname Dependents policyid cost Policies Purchaser name Employees ssn lot CREATE TABLE Dependents ( pname CHAR(20), age INTEGER, policyid INTEGER, PRIMARY KEY (pname, policyid), FOREIGN KEY (policyid) REFERENCES Policies ON DELETE CASCADE ) CREATE TABLE Policies ( policyid INTEGER, cost REAL, emp_ssn CHAR(11) NOT NULL, PRIMARY KEY (policyid). FOREIGN KEY (emp_ssn) REFERENCES Employees ON DELETE CASCADE ) CREATE TABLE Employees ( ssn CHAR (11), name CHAR (20), lot INTEGER, PRIMARY KEY (ssn))

16 An Example: Putting It Together login cname total oid Order Customer cid Placed shipto color pname Product sku listprice For LineItem lno price qty N 1 N 1 Has 1 N

17 Putting It Together (Cont’d.) CREATE TABLE Product ( sku INTEGER, pname VARCHAR(100), color VARCHAR(20), listprice DECIMAL(8,2), PRIMARY KEY (sku)) CREATE TABLE Customer ( cid INTEGER, cname VARCHAR(50), login VARCHAR(20) NOT NULL, PRIMARY KEY (cid), UNIQUE (login)) 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 ) )

18 Putting It Together (Cont’d.) cidcnamelogin 1Smith, 2White, 3Smith, skupnamecolorlistprice 123Frozen DVDnull Graco Twin Strollergreen Moen Kitchen Sinkblack oidcustidshiptototal 13J. Smith, 1 Main St., USA Mrs. Smith, 3 State St., USA oidlnopriceqtyitem Customer Product Order LineItem

19 SQL Views  A view is just a relation, but we store its definition rather than storing the (materialized) set of tuples. CREATE VIEW YoungActiveStudents (name, grade) AS SELECT S.name, E.grade FROM Students S, Enrolled E WHERE S.sid = E.sid and S.age < 21  Views can be dropped using the DROP VIEW command.  How to handle DROP TABLE if there ’ s a view on the table? DROP TABLE command has options to let the user specify this.

20 Views and Security  Views can be used to present necessary information (or a summary) while hiding some details in underlying relation(s).  Given YoungStudents, but not Students or Enrolled, we can find students S who have are enrolled, but not the cid ’ s of the courses they are enrolled in.

21 Relational Model and E-R Schema Translation: Summary  A tabular representation of data.  Simple and intuitive, also widely used.  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.  Powerful and natural query languages exist (soon!)  Rules to translate E-R to relational model  Can be done by a human, or automatically (using a tool)