CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)

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.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
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)
The Relational Model CMU SCS /615 C. Faloutsos – A. Pavlo Lecture #3 R & G, Chap. 3.
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.
The Relational Model 198:541 Rutgers University. Why Study the Relational Model?  Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle,
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
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 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 These slides are based on the slides of your text book.
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.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1.1 CAS CS 460/660 Relational Model. 1.2 Review E/R Model: Entities, relationships, attributes Cardinalities: 1:1, 1:n, m:1, m:n Keys: superkeys, candidate.
1 Database Systems ( 資料庫系統 ) October 4, 2005 Lecture #3.
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.
Levels of Abstraction in DBMS data Many views, single conceptual (logical) schema and physical schema. – Views describe how users see the data (possibly.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model 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 Relational Model Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY.
ICS 421 Spring 2010 Relational Model & Normal Forms Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 1/19/20101Lipyeow.
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.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) 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:
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
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 Relational Data Model
The Relational Model Chapter 3
The Relational Model The slides for this text are organized into chapters. This lecture covers Chapter 3. Chapter 1: Introduction to Database Systems Chapter.
Database Systems (資料庫系統)
The Relational Model Chapter 3
The Relational Model Chapter 3
Presentation transcript:

CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)

Announcements Homework 2 is out ▫Due on Tuesday Oct 13 First midterm exam ▫Thursday Oct 15 ▫Review session on Tuesday Oct 13 ▫Cheat sheet (one side of an A4 paper)

Review: SQL Data Definition Language (DDL) statements are used to define the database structure or schema. Some examples: ▫CREATE - to create objects in the database ▫ALTER - alters the structure of the database ▫DROP - delete objects from the database 3

Review: SQL Data Manipulation Language (DML) statements are used for managing data within schema objects. Some examples: ▫SELECT - retrieve data from the a database ▫INSERT - insert data into a table ▫UPDATE - updates existing data within a table ▫DELETE - deletes all records from a table, the space for the records remain 4

Views A view ▫is a table whose rows are not explicitly stored in the database but are computed as needed from a view definition ▫A view is a relation. We store a definition, rather than a 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

Create a view that shows the ids and names of students that are enrolled in Databases.

Views 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.

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

Integrity Constraints (ICs) IC: condition that must be true for any instance of the database; e.g., domain constraints. ▫ ICs are specified when schema is defined. ▫ ICs are checked when relations are modified. A legal instance of a relation is one that satisfies all specified ICs. ▫ DBMS should not allow illegal instances.

Primary Key Constraints A set of fields is a key for a relation if : ▫No two distinct tuples can have same values in all key fields If there are two or more keys for a relation, one of the keys is chosen (by DBA) to be the primary key.

Primary Keys in SQL CREATE TABLE Students (sid CHAR (10), name CHAR (20), login CHAR (30), age INTEGER, gpa REAL, PRIMARY KEY (sid));

Primary Keys in SQL CREATE TABLE Students (sid CHAR (10) PRIMARY KEY, name CHAR (20), login CHAR (30), age INTEGER, gpa REAL );

Can two tuples have the same primary key? Can the primary key be null?

14

Primary Keys in SQL CREATE TABLE Enrolled (sid CHAR (10) cid CHAR (30), grade CHAR (2), PRIMARY KEY (sid,cid) )

Foreign Keys, Referential Integrity Foreign key ▫Set of fields in one relation that is used to ‘refer’ to a tuple in another relation. ▫Must correspond to primary key of the second relation. E.g. sid is a foreign key referring to Students: ▫ Enrolled(sid: string, cid: string, grade: string)

Foreign Keys in SQL Only students listed in the Students relation should be allowed to enroll for courses. CREATE TABLE Enrolled (sid CHAR (10), cid CHAR(30), grade CHAR (2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students(sid) ); Enrolled Students

Enforcing Referential Integrity What should be done if an Enrolled tuple with a non- existent student id is inserted? ▫Reject it!

Enforcing Referential Integrity What should be done if a Students tuple is deleted? ▫ Also delete all Enrolled tuples that refer to it. ▫ Disallow deletion of a Students tuple that is referred to. ▫ Set sid in Enrolled tuples that refer to it to a default sid. ▫ Set sid in Enrolled tuples that refer to it to a special value null, denoting `unknown’ or `inapplicable’.  Can we do that in this example?

Enforcing Referential Integrity What should be done if a Students tuple is updated? ▫ Also update all Enrolled tuples that refer to it. ▫ Disallow update of a Students tuple that is referred to. ▫ Set sid in Enrolled tuples that refer to it to a default sid. ▫ Set sid in Enrolled tuples that refer to it to a special value null, denoting `unknown’ or `inapplicable’.

Referential Integrity in SQL support all 4 options on deletes and updates. ▫Default is NO ACTION  delete/update is rejected ▫CASCADE  also delete all tuples that refer to deleted tuple ▫SET DEFAULT  Set E.sid to S.sid of the “default” student if a student is deleted from the Student relation ▫SET NULL  Sets foreign key value of referencing tuple to null

Referential Integrity in SQL CREATE TABLE Enrolled (sid CHAR (20), cid CHAR(20), grade CHAR (2), PRIMARY KEY (sid,cid), FOREIGN KEY (sid) REFERENCES Students(sid) ON DELETE NO ACTION ON UPDATE CASCADE );

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

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 key for the relation. ▫ All descriptive attributes. did dname budget Departments lot since name Works_In Employees ssn

25 did dname budget Departments lot since name Works_In Employees ssn CREATE TABLE Works_In( ssn CHAR (11), did INTEGER, since DATE, PRIMARY KEY (ssn, did), FOREIGN KEY (ssn) REFERENCES Employees(ssn), FOREIGN KEY (did) REFERENCES Departments(did))

26 Relationship Sets to Tables subor- dinate super- visor Reports_To lot name Employees ssn CREATE TABLE Report_To( supervisor_ssn CHAR (11), subordinate_ssn CHAR (11 ), PRIMARY KEY ( supervisor_ssn, subordinate_ssn), FOREIGN KEY ( supervisor_ssn ) REFERENCES Employees(ssn), FOREIGN KEY ( subordinate_ssn ) REFERENCES Employees (ssn))

Review: Key Constraints 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 budgetdid since lot name ssn ManagesEmployees Departments

Translating ER Diagrams with Key Constraints Map relationship to a table: CREATE TABLE Manages( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (ssn, did), FOREIGN KEY (ssn) REFERENCES Employees(ssn), FOREIGN KEY (did) REFERENCES Departments(did)) dname budgetdid since lot name ssn ManagesEmployees Departments

Translating ER Diagrams with Key Constraints Map relationship to a table: ▫ Note that did is the key now! ▫ Separate tables for Employees and Departments. dname budgetdid since lot name ssn ManagesEmployees Departments CREATE TABLE Manages( ssn CHAR(11), did INTEGER, since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees(ssn), FOREIGN KEY (did) REFERENCES Departments(did))

Translating ER Diagrams with Key Constraints Since each department has a unique manager, we could instead combine Manages and Departments. CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees (ssn)) dname budgetdid since lot name ssn ManagesEmployees Departments

Review: Participation Constraints Does every department have a manager? ▫ If so, this is a participation constraint ▫ 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 Departments Employees ssn CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees)

Participation Constraints in SQL We can capture participation constraints involving one entity set in a binary relationship 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(ssn))

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

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 (ssn) ON DELETE CASCADE) lot name age pname Dependents Employees ssn Policy cost

Review: ISA Hierarchies  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) name ssn Employees lot Contract_Emps hourly_wages ISA Hourly_Emps contractid hours_worked

Translating ISA Hierarchies to Relations General approach: ▫ 3 distinct relations: Employees, Hourly_Emps and Contract_Emps.  Hourly_Emps: Every employee is recorded in Employees. For hourly emps, extra info recorded in Hourly_Emps (hourly_wages, hours_worked, ssn); must delete Hourly_Emps tuple if referenced Employees tuple is deleted). name ssn Employees lot Contract_Emps hourly_wages ISA Hourly_Emps contractid hours_worked

Translating ISA Hierarchies to Relations Alternative ▫Create two relations: Just Hourly_Emps and Contract_Emps.  Hourly_Emps: ssn, name, lot, hourly_wages, hours_worked.  Each employee must be in one of these two subclasses. name ssn Employees lot Contract_Emps hourly_wages ISA Hourly_Emps contractid hours_worked

E/R to Relations E/R diagram Relational schema, e.g. account=(bname, acct_no, bal) E a 1 ….. a n E = ( a 1, …, a n ) E1 E2 R1 a 1 …. a n c 1 …. c k b 1 …. b m R1= ( a 1, b 1, c 1, …, c k ) 38

More on relationships What about: Could have : put b 1 as the key for R1, it is also the key for E2=(b 1, …., b n ) Usual strategy: ▫ignore R1 ▫Add a1, c1, …., ck to E2 instead, i.e. ▫E2=(b 1, …., b n, a 1, c 1, …, c k ) E1 E2 R1 a 1 …. a n c 1 …. c k b 1 …. b m R1= ( a 1, b 1, c 1, …, c k ) 39

More E1 E2 R1 a 1 …. a n c 1 …. c k b 1 …. b m ? ? R1 E1 = ( a 1, …, a n ) E2 = ( b 1, …, b m ) R1 = ( a 1, b 1, c 1 …, c k ) E1 = ( a 1, …, a n ) E2 = ( b 1, …, b m, a 1, c 1, …, c k ) E1 = ( a 1, …, a n, b 1, c 1, …, c k ) E2 = ( b 1, …, b m,) Treat as N:1 or 1:N Foreign key also needs to be marked as a candidate key! 40

Translating ER Diagrams with Key Constraints Since each department has a unique manager, we could instead combine Manages and Departments. CREATE TABLE Dept_Mgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR(11), since DATE, PRIMARY KEY (did), UNIQUE (ssn), FOREIGN KEY (ssn) REFERENCES Employees) dname budgetdid since lot name ssn ManagesEmployees Departments

E/R to Relational Weak entity sets E1 E2 IR a 1 …. a n b 1 …. b m E1 = ( a 1, …, a n ) E2 = (a 1, b 1, …, b m ) 42

E/R to Relational E1 S2 Isa S1 a1 … an c 1 …. c k b 1 …. b m Method 1: E = ( a 1, …, a n ) S1 = (a 1, b 1, …, b m ) S2 = ( a 1, c 1 …, c k ) Method 2: S1 = (a 1,…, a n, b 1, …, b m ) S2 = ( a 1, …, a n, c 1 …, c k ) 43

Tenary relationshipset: What about tenary: Strategy: ▫E1(a 1 …. a n ) E2(b 1 …. b m ) E3(d 1 …. d l ) ▫R1(a 1, b 1, d 1, c1,…. c k, ) E1 E2 R1 a 1 …. a n c 1 …. c k b 1 …. b m 44 E3 d 1 …. d l

Relational Model: Summary Relational database ▫A tabular representation of data. ▫Simple and intuitive, currently the most widely used. SQL query languages ▫Create and modifying relations (CREATE, DROP, ALTER) ▫Manipulate data (INSERT, UPDATE, DELETE)

Relational Model: Summary 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. Rules to translate ER to relational model

Readings Chapter 4