Comp 3311 Database Management Systems. 2. Relational Model Exercises



Similar documents
Boyce-Codd Normal Form

Chapter 2: Entity-Relationship Model

Chapter 7: Relational Database Design

Introduction to SQL ( )

Lesson 8: Introduction to Databases E-R Data Modeling

Foundations of Information Management

Chapter 2: Entity-Relationship Model. Entity Sets. " Example: specific person, company, event, plant

We know how to query a database using SQL. A set of tables and their schemas are given Data are properly loaded

Converting E-R Diagrams to Relational Model. Winter Lecture 17

Chapter 2: Entity-Relationship Model. E-R R Diagrams

You can use command show database; to check if bank has been created.

Comp 5311 Database Management Systems. 3. Structured Query Language 1

Foundations of Information Management

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

B2.2-R3: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS

Simple SQL Queries (3)

OVERVIEW 1.1 DATABASE MANAGEMENT SYSTEM (DBMS) DEFINITION:-

Chapter 6: Integrity Constraints

Boats bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red. Figure 1: Instances of Sailors, Boats and Reserves

Chapter 7: Relational Database Design

ER modelling, Weak Entities, Class Hierarchies, Aggregation

Data Models and Database Management Systems (DBMSs) Dr. Philip Cannata

Chapter 5. SQL: Queries, Constraints, Triggers

Advance DBMS. Structured Query Language (SQL)

Introduction to database management systems

SUBQUERIES AND VIEWS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 6

Exercise 1: Relational Model

Chapter 1: Introduction. Database Management System (DBMS)

Databases and BigData

Database Design. Marta Jakubowska-Sobczak IT/ADC based on slides prepared by Paula Figueiredo, IT/DB

Entity Relationship Model

Basic Concepts. Chapter A: Network Model. Cont.) Data-Structure Diagrams (Cont( Data-Structure Diagrams. General Relationships. The DBTG CODASYL Model

Relational Algebra. Module 3, Lecture 1. Database Management Systems, R. Ramakrishnan 1

The E-R èentity-relationshipè data model views the real world as a set of basic objects èentitiesè and

Answer Key. UNIVERSITY OF CALIFORNIA College of Engineering Department of EECS, Computer Science Division

Functional Dependencies

BİL 354 Veritabanı Sistemleri. Entity-Relationship Model

ComponentNo. C_Description UnitOfMeasure. C_Quantity

SQL is capable in manipulating relational data SQL is not good for many other tasks

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

Relational Algebra and SQL

Lecture 6. SQL, Logical DB Design

SQL: Queries, Programming, Triggers

Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables

Database Sample Examination

SQL: Queries, Programming, Triggers

CPS352 Database Systems: Design Project

Designing a Database Schema

? Database Management

Fundamentals of Database System

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 7

SQL: QUERIES, CONSTRAINTS, TRIGGERS

Chapter 1: Introduction

Outline. Data Modeling. Conceptual Design. ER Model Basics: Entities. ER Model Basics: Relationships. Ternary Relationships. Yanlei Diao UMass Amherst

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

Bridge from Entity Relationship modeling to creating SQL databases, tables, & relations

Chapter 4: SQL. Schema Used in Examples

CSE 233. Database System Overview

Information Systems Analysis and Design CSC John Mylopoulos Database Design Information Systems Analysis and Design CSC340

Introdução às Bases de Dados

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

CSE 132A. Database Systems Principles

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

Introduction to SQL C H A P T E R3. Exercises

Database Security. Chapter 21

Lab Manual. Database Systems COT-313 & Database Management Systems Lab IT-216

Once the schema has been designed, it can be implemented in the RDBMS.

Object-Based Databases

Database Management Systems

Entity-Relationship Model

DATABASE MANAGEMENT SYSTEMS SOLUTIONS MANUAL. Raghu Ramakrishnan et al. University of Wisconsin Madison, WI, USA

6 QUERY-BY-EXAMPLE (QBE)

Entity Relationship Diagram

There are five fields or columns, with names and types as shown above.

A Novel Approach of Query Optimization for Distributed Database Systems

Review: Participation Constraints

Chapter 1: Introduction

Introduction to Databases

Database Fundamentals

Conceptual Design: Entity Relationship Models. Objectives. Overview

CHAPTER 3: DATA MODELING USING THE ENTITY-RELATIONSHIP MODEL

10CS54: DATABASE MANAGEMENT SYSTEM

PATIENT(PatientID, PatName, PatientDOB, PatientDiagnosis, PatientPhone, PatientReferID)

Relational Algebra 2. Chapter 5.2 V3.0. Napier University Dr Gordon Russell

TYPICAL QUESTIONS & ANSWERS

Θεµελίωση Βάσεων εδοµένων

Chapter 6: Integrity and Security. Domain Constraints

How to Write a Plug-In Interface

An Example: Video Rental System

DATABASE MANAGEMENT SYSTEMS SOLUTIONS MANUAL. Raghu Ramakrishnan et al. University of Wisconsin Madison, WI, USA

Quiz 2: Database Systems I Instructor: Hassan Khosravi Spring 2012 CMPT 354

Chapter 14: Query Optimization

2. Conceptual Modeling using the Entity-Relationship Model

Evaluation of Expressions

three Entity-Relationship Modeling chapter OVERVIEW CHAPTER

Chapter 3. Data Modeling Using the Entity-Relationship (ER) Model

Question 1. Relational Data Model [17 marks] Question 2. SQL and Relational Algebra [31 marks]

ER & EER to Relational Mapping. Chapter 9 1

LiTH, Tekniska högskolan vid Linköpings universitet 1(7) IDA, Institutionen för datavetenskap Juha Takkinen

Chapter 4: SQL. Schema Used in Examples. Basic Structure. The select Clause. modifications and enhancements! A typical SQL query has the form:

Transcription:

Comp 3311 Database Management Systems 2. Relational Model Exercises 1

E-R Diagram for a Banking Enterprise 2

Tables for ER diagram Entities Branch (branch-name, branch-city, assets) Customer (customer-id, customer-name, customer-street, customer-city) Loan (loan-number, amount) Employee (employee-id, employee-name, telephonenumber, start-date) Account (account-number, balance) Savings-account (account-number, interest-rate) Checking-account (account-number, overdraft-amount) 3

Tables for ER diagram Weak Entity Payment (loan-number, payment-number, payment-date, payment-amount) Multi-valued attribute Dependent (employee-id, dependent-name) 4

Tables for ER diagram Many-to-many relationships Borrower (customer-id, loan-number) Depositor (customer-id, account-number, access-date) One-to-many relationships Loan-branch is represented in Loan (loan-number, amount, branch-name) Cust-banker is represented in Customer (customer-id, customer-name, customer-street, customer-city, employee-id, type) Works-for is represented in Employee (employee-id, employee-name, telephone-number, start-date, manager-id) 5

ER to Tables Convert the ER diagram into tables. number# departure station destination station departure time id# name phone ROUTE SCHEDULE DRIVES DRIVER BUS_IN_USE licence# BUS capacity 6

Tables for ER diagram Entities Route (number, departure, destination) Driver (id, name, phone) Bus (license, capacity) Weak Entity Schedule (number, departure-time) Relationships Drives (number, departure-time, id) Bus-in-use (license, number, departure-time) 7

DB Design We want to design a relational database schema to represent information about an internal training program of a large company. Design an ER diagram based on the description given below: The database keeps information about every employee in the company. Each employee has a name and a unique employee number. Each course of the training program has a unique course number and a name. The courses are taught and taken by employees of the company. A course can be offered many times. Each offering has an offering number, which is unique within each course, a day and time. An offering is taught by exactly one employee. The database stores the grades of employees who took courses. 8

Solution offering# time day Offering Teaches Employee offered Enrolled emp# name course# Course name grade Employee (EmpNo, Name) Course (CourseNo, Name) Offering (CourseNo, OfferingNo, Day, Time, EmpNo) Enrolled (CourseNo, OfferingNo, EmpNo, Grade) 9

Example Let the following relational schema: B(X,W), C(P,Q,X) where X is defined as NOT NULL, D(P,X,R) Give an ER diagram for the above relational schema. 10

Solution B any_name C X W D P Q R 11

Find ids of sailors who ve reserved boat with bid 103 We use the following database schema Sailors (sid, sname), Reserves (sid, bid, date), Boats (bid,bname,color) Question: Give an ER diagram for the above tables. sid ( bid=103 Reserves) 12

Find names of sailors who ve reserved boat with bid 103 Sailors(sid, sname), Reserves(sid,bid,date), Boats(bid,bname,color) Solution 1: sname ( Reserves.sid=Sailors.sid bid=103 (Reserves x Sailors))) Solution 2: sname ( Reserves.sid=Sailors.sid (( bid=103 Reserves) x Sailors)) Solution 3: sname (( bid=103 Reserves) JOIN sid Sailors) 13

Find names of sailors who ve reserved a red boat Sailors(sid, sname), Reserves(sid,bid,date), Boats(bid,bname,color) Solution 1: sname (( color=red Boats) JOIN bid Reserves JOIN sid Sailors) Can you give a more efficient solution? sname (( bid ( color=red Boats)) JOIN bid Reserves JOIN sid Sailors) query optimization: real systems do such optimizations based on algebra 14

Find the names of sailors who ve reserved a red or a green boat Sailors(sid, sname), Reserves(sid,bid,date), Boats(bid,bname,color) Can identify all red or green boats, then find sailors who ve reserved one of these boats: sname (( color=red color=green Boats) JOIN bid Reserves JOIN sid Sailors) What happens if is replaced by in this query? 15

Find the names of sailors who ve reserved a red and a green boat Sailors(sid, sname), Reserves(sid,bid,date), Boats(bid,bname,color) Previous approach won t work! Must identify sailors who ve reserved red boats, sailors who ve reserved green boats, then find the intersection: Is this solution correct? sname (( color=red Boats) JOIN bid Reserves JOIN sid Sailors) sname (( color=green Boats) JOIN bid Reserves JOIN sid Sailors) Correct solution: sname [ sid,sname (( color=red Boats) JOIN bid Reserves JOIN sid Sailors) sid,sname (( color=green Boats) JOIN bid Reserves JOIN sid Sailors)] 16

Find ids of sailors who have made at least two reservations on the same date Sailors(sid, sname), Reserves(sid,bid,date), Boats(bid,bname,color) We have to use rename: p(r1,reserves), p(r2,reserves) R1.sid ( R1.sid=R2.sid R1.date=R2.date R1.bid R2.bid (R1 x R2)) Or equivalently: R1.sid ( R1.sid=R2.sid R1.date=R2.date R1.bid R2.bid (p(r1,reserves) x p(r2,reserves))) R1.sid (p(r1,reserves) JOIN R1.sid=R2.sid R1.date=R2.date R1.bid R2.bid p(r2,reserves)) What happens if we omit R1.date=R2.date? What happens if we omit R1.bid R2.bid? 17

Find the ids of sailors who ve reserved all boats Sailors(sid, sname), Reserves(sid,bid,date), Boats(bid,bname,color) Uses division; schemas of the input relations must be carefully chosen: ( sid,bid Reserves) / bid Boats What about the query: find the ids of sailors who have reserved all red boats ( sid,bid Reserves) / bid ( color=red Boats) What about the query: find the names of sailors who have reserved all red boats sname (Sailors JOIN sid ( sid,bid Reserves) / bid ( color=red Boats)) 18