A Sample Solution to the Midterm Test

Similar documents
Database Systems Relational Model. A.R. Hurson 323 CS Building

Information Systems. Relational Databases. Nikolaj Popov

Supplier-Parts-DB SNUM SNAME STATUS CITY S1 Smith 20 London S2 Jones 10 Paris S3 Blake 30 Paris S4 Clark 20 London S5 Adams 30 Athens

1 (10) 2 (8) 3 (12) 4 (14) 5 (6) Total (50)

Lecture 15: Database Normalization

Relational Data Structure and Concepts. Structured Query Language (Part 1) The Entity Integrity Rules. Relational Data Structure and Concepts

MySQL Introduction. Practical Course Integrative Bioinformatics. March 7, 2012

Database Redesign. 1. Additional SQL Statements 3 1) Correlated Sub-Query 3 2) EXISTS 4 3) NOT EXISTS 7 4) double NOT EXISTS (FOR ALL) 9.

Relational model continued. Understanding how to use the relational model. Summary of board example: with Copies as weak entity

Unit 3 The Relational Model

WEEK 3. EE562 Slides and Modified Slides from Database Management Systems, R.Ramakrishnan 1

SQL - Data Query language

2.2.2.Relational Database concept

Midterm Review. Winter Lecture 13

Relational Query Languages: Relational Algebra. Juliana Freire

4/10/2018. Relational Algebra (RA) 1. Selection (σ) 2. Projection (Π) Note that RA Operators are Compositional! 3.

UFCEKG 20 2 : Data, Schemas and Applications

Relational Database: The Relational Data Model; Operations on Database Relations

Database Management Systems Paper Solution

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

LABSHEET 1: creating a table, primary keys and data types

L22: The Relational Model (continued) CS3200 Database design (sp18 s2) 4/5/2018

Relational Algebra. Relational Algebra. 7/4/2017 Md. Golam Moazzam, Dept. of CSE, JU

Lassonde School of Engineering Winter 2016 Term Course No: 4411 Database Management Systems

COSC344 Database Theory and Applications. σ a= c (P) S. Lecture 4 Relational algebra. π A, P X Q. COSC344 Lecture 4 1

CMP-3440 Database Systems

EECS 647: Introduction to Database Systems

CS3DB3/SE4DB3/SE6DB3 TUTORIAL

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13

Introduction The SELECT statement: basics Nested queries Set operators Update commands Table management

Unit 4 SQL language: other definitions

CSC 261/461 Database Systems Lecture 13. Fall 2017

Keys, SQL, and Views CMPSCI 645

Informationslogistik Unit 4: The Relational Algebra

Optimization Overview

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

Chapter 2: Intro to Relational Model

Ian Kenny. November 28, 2017

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

Overview of DB & IR. ICS 624 Spring Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

U1. Data Base Management System (DBMS) Unit -1. MCA 203, Data Base Management System

CSCI 5333 DBMS Fall 2018 Mid-Term Examination. Last Name: First Name: Student Id:

The Relational Model and Relational Algebra

COMP 244 DATABASE CONCEPTS AND APPLICATIONS

Querying Data with Transact SQL

A subquery is a nested query inserted inside a large query Generally occurs with select, from, where Also known as inner query or inner select,

Course No: 4411 Database Management Systems Fall 2008 Midterm exam

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #3: SQL and Rela2onal Algebra- - - Part 1

Assignment 6: SQL III Solution

Relational Algebra. Relational Query Languages

CSE 344 JANUARY 26 TH DATALOG

12. MS Access Tables, Relationships, and Queries

Relational Model, Relational Algebra, and SQL

Relational Algebra and SQL. Basic Operations Algebra of Bags

Basic operators: selection, projection, cross product, union, difference,

Relational Algebra for sets Introduction to relational algebra for bags

Chapter 6 The Relational Algebra and Relational Calculus

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

RELATION AND RELATIONAL OPERATIONS

MIDTERM EXAMINATION Spring 2010 CS403- Database Management Systems (Session - 4) Ref No: Time: 60 min Marks: 38

Relational Model History. COSC 304 Introduction to Database Systems. Relational Model and Algebra. Relational Model Definitions.

CS5300 Database Systems

The Relational Model and Normalization

CS317 File and Database Systems

Web Science & Technologies University of Koblenz Landau, Germany. Relational Data Model

3. Relational Data Model 3.5 The Tuple Relational Calculus

8) A top-to-bottom relationship among the items in a database is established by a

MIS Database Systems Relational Algebra

Modeling Your Data. Chapter 2. cs542 1

Chapter 7 Relational Calculus

Section 2.2: Relational Databases

Announcement. Reading Material. Overview of Query Evaluation. Overview of Query Evaluation. Overview of Query Evaluation 9/26/17

What s a database system? Review of Basic Database Concepts. Entity-relationship (E/R) diagram. Two important questions. Physical data independence

Databases. Relational Model, Algebra and operations. How do we model and manipulate complex data structures inside a computer system? Until

Sankalchand Patel College of Engineering, Visnagar B.E. Semester III (CE/IT) Database Management System Question Bank / Assignment

CSE 544 Principles of Database Management Systems

CS 4604: Introduction to Database Management Systems. B. Aditya Prakash Lecture #2: The Relational Model and Relational Algebra

Databases - Relational Algebra. (GF Royle, N Spadaccini ) Databases - Relational Algebra 1 / 24

Relational Databases

This lecture. Projection. Relational Algebra. Suppose we have a relation

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

CompSci 516: Database Systems

CPS 510 Data Base I. There are 3 forms of database descriptions the ANSI/SPARK, 1975 and so on

Relational Algebra. Procedural language Six basic operators

RELATIONAL ALGEBRA II. CS121: Relational Databases Fall 2017 Lecture 3

Relational Model 2: Relational Algebra

Databases 1. Daniel POP

Relational Algebra. Mr. Prasad Sawant. MACS College. Mr.Prasad Sawant MACS College Pune

CS2300: File Structures and Introduction to Database Systems

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

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

Relational Algebra 1

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

Introduction to Database Systems CSE 444

CIS 330: Applied Database Systems

RELATIONAL ALGEBRA. CS121: Relational Databases Fall 2017 Lecture 2

Database Design Process

This lecture. Step 1

CS403- Database Management Systems Solved MCQS From Midterm Papers. CS403- Database Management Systems MIDTERM EXAMINATION - Spring 2010

Birkbeck. (University of London) BSc/FD EXAMINATION. Department of Computer Science and Information Systems. Database Management (COIY028H6)

Transcription:

CS3600.1 Introduction to Database System Fall 2016 Dr. Zhizhang Shen A Sample Solution to the Midterm Test 1. A couple of W s(10) (a) Why is it the case that, by default, there are no duplicated tuples in any table in a relational database? A solution: I found various answers. But, the fundamental reason is that a RDB table is a set, which does not allow duplicates. (b) What are the nine basic relational operations that we can use within a relational algebraic expression? Describe them briefly. A solution: Union, Intersection, Cartesian Product, Minus, Projection, Restriction, Join, Divided By, and Rename. You know what they do, don t you? (c) What is a View? Is it the same as, or different from, a table? Explain. A solution: A view is defined via a query, based on tables. It is similar to a table in the sense that they share the same structure, thus a view can be used in any context, where a table can be used. It is different from a table as it is not permanently stored in a database. (d) What does that mean by logic data independence? Give an example as when there might be a problem, and why is such an independence important? A solution: Logic data independence refers to the immunity of users and user programs to changes in the logic structure of the database. In other words, over a long period of time, even though the conceptual structure of the database will change, the application programs don et have to. As we mentioned in a class, we may consider to split the table Student table which contains supplier information, to two tables SNC: (SupplierId, SName, City), and ST: (SupplierId, SName, Status). Such a split changes the conceptual structure of the database, thus having an impact on application programs. On the other hand, we can create a view Student as SNC Join ST. Then, all the applications involved with the Student table will still be valid with the help of this view, even though that table no longer exists. (e) What are some of the issues we might run into when converting an ER relationship to a RDB relation? why? A solution: When we convert an ER entity to a RDB relation, if an attribute of this relationship might have a set as its value, we have to throw it in as part of the key for this relation. The reason is certainly the data atomicity requirement of REB. Another issue might be the key composition for such a relation, which consists of all the attributes, and keys of the participating roles. When we convert a part-of relationship, we might have to make a choice as if we should covert into one relation; or maybe combine a few.

When we convert an isa relationship, we might have to enforce the associated disjoint and covering constraints.... Problems 2 and 3 are based on the following tables, Supplier, Part, and SupplyPart. Below is an instance of such a structure. SupplierId SName Status City S1 Smith 20 London S2 Jones 10 Paris S3 Blake 30 Paris PartId PName Color Weight City P1 Nut Red 12.0 London P2 Bolt Green 17.0 Paris P3 Screw Blue 17.0 Rome P4 Screw Red 14.0 London SupplierId PartId Quantity S1 P1 300 S1 P2 200 S1 P3 400 S1 P4 200 S2 P1 300 S2 P2 400 S3 P2 200 2. What do the following queries do? Write down the respective output based on the above instance, as well as an explanation.(5) (a) π SupplierId, Status, City (σ Status>15 (Supplier)) A solution: Find out the Supplier number, status and location of those suppliers whose status is more than 15. With the above instance, after a restriction on those tuples where thestatus is above 15, and then a projection of the relevant three attributes, the result should be the following: SupplierId Status City S1 20 London S3 30 Paris (b) Select * from parispart where Color= Red, where parispart is defined as follows: Create View parispart As Select PartId, PName, Color From P Where City= Paris ; 2

A solution: When there is a view in the query, DBMS will replace the occurrence of the view with its definition first. Thus, the above query becomes Select * From ( Select PartId, PName, Color From P Where City= Paris ; ) Where Color= Red ; It is clear that this query is to find the part number, its name and color of all the red parts stored in Paris. Since there are no such parts in this instance of the database, the answer will be empty for this database instance. 3. With the above table schema, come up with either an algebraic expression or ansql query that will, respectively, (5) (a) get full details of those parts that are supplied by S2. (b) get supplier s names for those who supply the largest quantity for each and every part as listed in the Part table. You can use either a relational algebraic expression, or an SQL statement, but you may not use a mixture of those structures. A solution: In Relational Algebra: (a) Everybody should be able to do it. Part PartId (π Part.PartId (σ SupplyPart.SupplierId= S2 (SupplyPart))) (b) Not applicable since RA does not support aggregation operators. The general idea is to find out those parts and their associated maximum quantity in the SupplyPart table, then dig out the ids for those suppliers who supply such a maximum quantity, also in the SupplyPart table, and finally dig out their names in the Supplier table, e.g., via a Join. In MySQL, an implementation of SQL. Again, everybody should be able to do it. (a) mysql> Select P.* -> From Part P, SupplyPart SP -> WHERE P.PartId=SP.PartId AND SP.SupplierId= S2 ; This query should lead to the following result, with the above database instance: PartId Name Color Weight City P1 Nut Red 12 London P2 Bolt Green 17 Paris 3

If you hate the expensive Join, you can use the following instead. Select P.* From Part P Where P.PartId In ( Select SP.SupplierId From SupplierPart SP Where SP.SupplierId= S2 ); (b) There are also few ways of doing this. You can do it in a nested query as follows: mysql> Select distinct S.SName, SP.PartId, SP.Quantity -> From SupplyPart SP, Suppliers S, -> #The following table C gets us -> #a table with each part and its maximum quantity. -> (Select PartId, max(quantity) As Num -> From SupplyPart -> Group by PartId) As C -> Where S.SupplierId=SP.SupplierId -> and SP.PartId=C.PartId And SP.Quantity=C.Num; The above query leads to the following output, with the above database instance. SName PartId Quantity Smith P1 300 Jones P1 300 Jones P2 400 Smith P3 400 Smith P4 200 On the other hand, if you prefer to cut this query into a few simpler steps, you can certainly use the second tier query to create a view, as we did somewhere in Lab 4. mysql> Create View QMax As -> (Select PartId, max(quantity) As Num -> From SupplyPart -> Group by PartId); Query OK, 0 rows affected (0.04 sec) This gives us the maximum quantity as associated with each and every of the parts: mysql> select * from QMax; PartId Num P1 300 P2 400 4

P3 400 P4 200 A bit simplified query gives us the desired information: mysql> Select distinct S.SName, SP.PartId, SP.Quantity -> From SupplyPart SP, Suppliers S, QMax -> Where S.SupplierId=SP.SupplierId -> And SP.PartId=QMax.PartId -> And SP.Quantity=QMax.Num; SName PartId Quantity Smith P1 300 Jones P1 300 Jones P2 400 Smith P3 400 Smith P4 200 5