Database Applications (15-415)

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

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

Database Applications (15-415)

Database Management Systems. Chapter 3 Part 2

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

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

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

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

High-Level Database Models (ii)

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

The Relational Model (ii)

High Level Database Models

The Relational Model 2. Week 3

Database Applications (15-415)

Relational Algebra. Note: Slides are posted on the class website, protected by a password written on the board

The Relational Model. Chapter 3

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

Database Applications (15-415)

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

Database Systems ( 資料庫系統 )

Introduction to Data Management. Lecture #11 (Relational Algebra)

Introduction to Database Design

The Entity-Relationship Model

The Entity-Relationship Model. Overview of Database Design

Relational Algebra. Chapter 4, Part A. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Introduction to Database Design

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

Relational Algebra. [R&G] Chapter 4, Part A CS4320 1

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

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

Database Applications (15-415)

CONCEPTUAL DESIGN: ER TO RELATIONAL TO SQL

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

Relational Query Languages. Preliminaries. Formal Relational Query Languages. Example Schema, with table contents. Relational Algebra

MIS Database Systems Relational Algebra

ER to Relational Mapping. ER Model: Overview

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

Relational Algebra 1. Week 4

CSCC43H: Introduction to Databases. Lecture 3

Lecture #8 (Still More Relational Theory...!)

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

Introduction to Data Management. Lecture #11 (Relational Languages I)

OVERVIEW OF DATABASE DEVELOPMENT

Relational algebra. Iztok Savnik, FAMNIT. IDB, Algebra

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

Relational Algebra. Relational Query Languages

Database Management Systems. Chapter 4. Relational Algebra. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Database Systems. Course Administration

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

Relational Query Languages. Relational Algebra. Preliminaries. Formal Relational Query Languages. Relational Algebra: 5 Basic Operations

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

Database Management System. Relational Algebra and operations

Relational Algebra 1

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

Relational Algebra Homework 0 Due Tonight, 5pm! R & G, Chapter 4 Room Swap for Tuesday Discussion Section Homework 1 will be posted Tomorrow

Conceptual Design. The Entity-Relationship (ER) Model

CAS CS 460/660 Introduction to Database Systems. Relational Algebra 1.1

CIS 330: Applied Database Systems

Relational Query Languages

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

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

The Entity-Relationship Model

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

v Conceptual Design: ER model v Logical Design: ER to relational model v Querying and manipulating data

The Relational Model

Introduction to Database Design

Relational Algebra. Study Chapter Comp 521 Files and Databases Fall

Introduction to Databases

CSC 261/461 Database Systems Lecture 10

MIS Database Systems Entity-Relationship Model.

Entity-Relationship Diagrams

Database Applications (15-415)

The Entity-Relationship (ER) Model

Conceptual Design. The Entity-Relationship (ER) Model

The Entity-Relationship Model

CompSci 516: Database Systems

Relational Algebra and Calculus. Relational Query Languages. Formal Relational Query Languages. Yanlei Diao UMass Amherst Feb 1, 2007

Database Management Systems. Chapter 2 Part 2

CSE 530A. ER Model. Washington University Fall 2013

Database Design & Deployment

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

The Entity-Relationship (ER) Model 2

Today s topics. Null Values. Nulls and Views in SQL. Standard Boolean 2-valued logic 9/5/17. 2-valued logic does not work for nulls

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

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

The Entity-Relationship Model. Steps in Database Design

Relational Algebra 1

COMP Instructor: Dimitris Papadias WWW page:

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

Database Design CENG 351

Database Applications (15-415)

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

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

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

The Relational Model

Database Systems. Course Administration. 10/13/2010 Lecture #4

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

Translating an ER Diagram to a Relational Schema

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

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

Transcription:

Database Applications (15-415) ER to Relational & Relational Algebra Lecture 4, January 20, 2015 Mohammad Hammoud

Today Last Session: The relational model Today s Session: ER to relational Relational algebra Relational query languages (in general) Relational operators Announcements: PS1 is due on Thursday, Jan 22 by midnight In the next recitation we will practice on translating ER designs into relational databases The recitation time and location will remain the same for the whole semester (i.e., every Thursday at 4:30PM in Room # 1190)

Outline Translating ER Diagrams to Tables and Summary Query Languages Relational Operators

Strong Entity Sets to Tables ssn name Employees lot CREATE TABLE Employees (ssn CHAR(11), name CHAR(20), lot INTEGER, PRIMARY KEY (ssn))

Relationship Sets to Tables In translating a relationship set to a relation, attributes of the relation must include: 1. Keys for each participating entity set (as foreign keys) This set of attributes forms a superkey for the relation 2. All descriptive attributes Relationship sets 1-to-1, 1-to-many, and many-to-many Key/Total/Partial participation

M-to-N Relationship Sets to Tables name since dname ssn lot did budget Employees Works_In Departments 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)

1-to-M Relationship Sets to Tables name since dname ssn lot did budget Employees Manages 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 Departments( did INTEGER), dname CHAR(20), budget REAL, PRIMARY KEY (did), ) Approach 1: Create separate tables for Manages and Departments

1-to-M Relationship Sets to Tables name since dname ssn lot did budget Employees Manages Departments CREATE TABLE Dept_Mgr( ssn CHAR(11), did INTEGER, since DATE, dname CHAR(20), budget REAL, PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees) Can ssn take a null value? Approach 2: Create a table for only the Departments entity set (i.e., take advantage of the key constraint)

One-Table vs. Two-Table Approaches The one-table approach: (+) Eliminates the need for a separate table for the involved relationship set (e.g., Manages) (+) Queries can be answered without combining information from two relations (-) Space could be wasted! What if several departments have no managers? The two-table approach: The opposite of the one-table approach!

Translating Relationship Sets with Participation Constraints What does the following ER diagram entail (with respect to Departments and Managers)? ssn name lot since did dname budget Employees Manages Departments Works_In since Every did value in Departments table must appear in a row of the Manages table- if defined- (with a non-null ssn value!)

Translating Relationship Sets with Participation Constraints Here is how to create the Dept_Mgr table using the one-table approach: 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) Can this be captured using the two-table approach?

Translating Relationship Sets with Participation Constraints Here is how to create the Dept_Mgr table using the one-table approach: 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 SET NULL) Would this work?

Translating Weak Entity Sets A weak entity set always: Participates in a one-to-many binary relationship Has a key constraint and total participation name ssn lot cost dname age Employees Policy Dependents Which approach is ideal for that? The one-table approach

Translating Weak Entity Sets Here is how to create Dep_Policy using the one-table approach ssn name lot cost dname age Employees Policy Dependents CREATE TABLE Dep_Policy ( dname CHAR(20), age INTEGER, cost REAL, ssn CHAR(11) NOT NULL, PRIMARY KEY (dname, ssn), FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE CASCADE)

Translating ISA Hierarchies to Relations Consider the following example: ssn name lot Employees hourly_wages hours_worked ISA contractid Hourly_Emps Contract_Emps

Translating ISA Hierarchies to Relations General approach: Create 3 relations: Employees, Hourly_Emps and Contract_Emps EMP (ssn, name, lot) ssn hourly_wages hours_worked Hourly_Emps name Employees ISA lot contractid Contract_Emps H_EMP(ssn, h_wg, h_wk) CONTR(ssn, cid) How many times do we record an employee? What to do on deletions? How to retrieve all info about an employee?

Translating ISA Hierarchies to Relations Alternatively: Just create 2 relations Hourly_Emps and Contract_Emps Each employee must be in one of these two subclasses ssn hourly_wages hours_worked Hourly_Emps name Employees ISA lot contractid Contract_Emps EMP (ssn, name, lot) black is gone! H_EMP(ssn, h_wg, h_wk, name, lot) CONTR(ssn, cid, name, lot) Duplicate Values!

Translating Aggregations Consider the following example: ssn name lot Employees Monitors until pid started_on pbudget since did dname budget Projects Sponsors Departments

Translating Aggregations Standard approach: The Employees, Projects and Departments entity sets and the Sponsors relationship sets are translated as described previously ssn name Employees Monitors lot until For the Monitors relationship, we create a relation with the following attributes: The key attribute of Employees (i.e., ssn) The key attributes of Sponsors (i.e., did, pid) The descriptive attributes of Monitors (i.e., until) pid started_on Projects pbudget since Sponsors did dname Departments budget

The Relational Model: A Summary A tabular representation of data Simple and intuitive, currently one of the most widely used Object-relational variant is gaining ground Integrity constraints can be specified (by the DBA) based on application semantics (DBMS checks for violations) Two important ICs: primary and foreign keys Also: not null, unique In addition, we always have domain constraints Mapping from ER to Relational is (fairly) straightforward!

ER to Tables - Summary of Basics Strong entities: Key -> primary key (Binary) relationships: Get keys from all participating entities: 1:1 -> either key can be the primary key 1:N -> the key of the N part will be the primary key M:N -> both keys will be the primary key Weak entities: Strong key + partial key -> primary key... ON DELETE CASCADE

ER to Tables - Summary of Advanced Total/Partial participation: NOT NULL Ternary relationships: Get keys from all; decide which one(s) -> primary Key Aggregation: like relationships ISA: 3 tables (most general) 2 tables ( total coverage )

Outline Translating ER Diagrams to Tables and Summary Query Languages Relational Operators

Relational Query Languages Query languages (QLs) allow manipulating and retrieving data from databases The relational model supports simple and powerful QLs: Strong formal foundation based on logic High amenability for effective optimizations Query Languages!= programming languages! QLs are not expected to be Turing complete QLs are not intended to be used for complex calculations QLs support easy and efficient access to large datasets

Formal Relational Query Languages There are two mathematical Query Languages which form the basis for commercial languages (e.g., SQL) Relational Algebra Queries are composed of operators Each query describes a step-by-step procedure for computing the desired answer Very useful for representing execution plans Relational Calculus Queries are subsets of first-order logic Queries describe desired answers without specifying how they will be computed A type of non-procedural (or declarative) formal query language

Formal Relational Query Languages There are two mathematical Query Languages which form the basis for commercial languages (e.g., SQL) Relational Algebra Queries are composed of operators Each query describes This a step-by-step session s topic procedure for computing the desired answer Very useful for representing execution plans Relational Calculus Queries are subsets of first-order logic Next session s topic (very briefly) Queries describe desired answers without specifying how they will be computed A type of non-procedural (or declarative) formal query language

Outline Translating ER Diagrams to Tables and Summary Query Languages Relational Operators

Relational Algebra Operators (with notations): 1. Selection ( ) 2. Projection ( ) 3. Cross-product ( ) 4. Set-difference ( ) 5. Union ( ) 6. Intersection ( ) 7. Join ( ) 8. Division ( ) 9. Renaming ( ) Each operation returns a relation, hence, operations can be composed! (i.e., Algebra is closed )

Relational Algebra Operators (with notations): 1. Selection ( ) 2. Projection ( ) 3. Cross-product Basic ( ) 4. Set-difference ( ) 5. Union ( ) 6. Intersection ( ) 7. Join ( ) 8. Division ( ) 9. Renaming ( ) Additional, yet extremely useful! Each operation returns a relation, hence, operations can be composed! (i.e., Algebra is closed )

Projection: The Projection Operatation (R) att list Project out attributes that are NOT in att-list The schema of the output relation contains ONLY the fields in att-list, with the same names that they had in the input relation Example 1: Input Relation: sname, rating sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 S2 ( S2) Output Relation: sname yuppy 9 lubber 8 guppy 5 rusty 10 rating

The Projection Operation Example 2: age ( S2) Input Relation: sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 Output Relation: age 35.0 55.5 S2 The projection operator eliminates duplicates! Note: real DBMSs typically do not eliminate duplicates unless explicitly asked for

Selection: The Selection Operation (R) condition Selects rows that satisfy the selection condition The schema of the output relation is identical to the schema of the input relation Example: ( S 2) rating 8 Input Relation: sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 S2 Output Relation: sid sname rating age 28 yuppy 9 35.0 58 rusty 10 35.0

Operator Composition The output relation can be the input for another relational algebra operation! (Operator composition) Example: ( ( S )) sname, rating rating 8 2 Input Relation: sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 S2 Intermediate Relation: sid sname rating age 28 yuppy 9 35.0 58 rusty 10 35.0 Final Output Relation: sname rating yuppy 9 rusty 10

Union: R U S The Union Operation The two input relations must be union-compatible Same number of fields `Corresponding fields have the same type The output relation includes all tuples that occur in either R or S or both The schema of the output relation is identical to the schema of R Example: sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 S1 S1 S2 Input Relations: sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 S2 Output Relation: sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 44 guppy 5 35.0 28 yuppy 9 35.0

The Intersection Operation Intersection: R S The two input relations must be union-compatible The output relation includes all tuples that occur in both R and S The schema of the output relation is identical to the schema of R Example: S1 S2 Input Relations: Output Relation: sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 S1 sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 S2 sid sname rating age 31 lubber 8 55.5 58 rusty 10 35.0

The Set-Difference Operation Set-Difference: R S The two input relations must be union-compatible The output relation includes all tuples that occur in R but not in S The schema of the output relation is identical to the schema of R Example: S1 S2 Input Relations: Output Relation: sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 S1 sid sname rating age 28 yuppy 9 35.0 31 lubber 8 55.5 44 guppy 5 35.0 58 rusty 10 35.0 S2 sid sname rating age 22 dustin 7 45.0

The Cross-Product and Renaming Cross Product: Operations Each row of R is paired with each row of S The schema of the output relation concatenates S1 s and R1 s schemas Conflict: R and S might have similar field names Solution: Rename fields using the Renaming Operator Renaming: Example: sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 S1 S1XR1 RXS ( R( F), E) Input Relations: sid bid day 22 101 10/10/96 58 103 11/12/96 R1 Output Relation: (sid) sname rating age (sid) bid day Conflict: Both S1 and R1 have a field called sid 22 dustin 7 45.0 22 101 10/10/96 22 dustin 7 45.0 58 103 11/12/96 31 lubber 8 55.5 22 101 10/10/96 31 lubber 8 55.5 58 103 11/12/96 58 rusty 10 35.0 22 101 10/10/96 58 rusty 10 35.0 58 103 11/12/96

The Cross-Product and Renaming Cross Product: Operations Each row of R is paired with each row of S The schema of the output relation concatenates S1 s and R1 s schemas Conflict: R and S might have the same field name Solution: Rename fields using the Renaming Operator Renaming: Example: sid sname rating age 22 dustin 7 45.0 31 lubber 8 55.5 58 rusty 10 35.0 S1 S1XR1 RXS ( R( F), E) Input Relations: sid bid day 22 101 10/10/96 58 103 11/12/96 R1 Output Relation: (sid) sname rating age (sid) bid day 22 dustin 7 45.0 22 101 10/10/96 22 dustin 7 45.0 58 103 11/12/96 31 lubber 8 55.5 22 101 10/10/96 31 lubber 8 55.5 58 103 11/12/96 58 rusty 10 35.0 22 101 10/10/96 58 rusty 10 35.0 58 103 11/12/96 ( C(1 sid1,5 sid2), S1 R1)

Next Class Relational Algebra (Cont d)