Assignment #2. CS Spring 2015 Due on Friday, June 19, 2015, 9 AM For instructions on how to submit your assignment check the course website.

Similar documents
University of Massachusetts Amherst Department of Computer Science Prof. Yanlei Diao

COMP7640 Assignment 2

DATABASE MANAGEMENT SYSTEMS

Integrity constraints, relationships. CS634 Lecture 2

Outer Join, More on SQL Constraints

Example Examination. Allocated Time: 100 minutes Maximum Points: 250

CSC 370 Database Systems Summer 2004 Solutions for Exercise No. 1 Version 1.0 June 14, 2004

Translating an ER Diagram to a Relational Schema

Homework 1: Relational Algebra and SQL (due February 10 th, 2016, 4:00pm, in class hard-copy please)

SQL Structured Query Language Introduction

CS2300: File Structures and Introduction to Database Systems

Database Systems CSE 303. Outline. Lecture 06: SQL. What is Sub-query? Sub-query in WHERE clause Subquery

Relational Model History. COSC 416 NoSQL Databases. Relational Model (Review) Relation Example. Relational Model Definitions. Relational Integrity

192 Chapter 14. TotalCost=3 (1, , 000) = 6, 000

QUIZ 1 REVIEW SESSION DATABASE MANAGEMENT SYSTEMS

SQL. Char (30) can store ram, ramji007 or 80- b

High-Level Database Models (ii)

Babu Banarasi Das National Institute of Technology and Management

Assignment #3. CS Spring 2015 Due on Saturday, July 25, 2015, 9 AM For instructions on how to submit your assignment check the course website.

CS419 Spring Computer Security. Vinod Ganapathy Lecture 15. Chapter 5: Database security

SQL. A Short Note. November 14, where the part appears between [ and ] are optional. A (simple) description of components are given below:

CMPT 354 Database Systems. Simon Fraser University Fall Instructor: Oliver Schulte

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

Database Management Systems (COP 5725) Homework 3

CS 327E Lecture 2. Shirley Cohen. January 27, 2016

CSC 261/461 Database Systems Lecture 10

Endterm Exam (Version B) CS 122A Spring 2017

CS W Introduction to Databases Spring Computer Science Department Columbia University

CSE 344 Midterm Nov 1st, 2017, 1:30-2:20

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Sample Question Paper

Designing Tables for an Oracle Database System. From theory to practice

An Introduction to Structured Query Language

CSE 544: Principles of Database Systems

QUERY OPTIMIZATION [CH 15]

DR B.R.AMBEDKAR UNIVERSITY B.Sc.(Computer Science): III Year THEORY PAPER IV (Elective 4) PHP, MySQL and Apache

An Introduction to Structured Query Language

An Introduction to Structured Query Language

INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

doc. RNDr. Tomáš Skopal, Ph.D. RNDr. Michal Kopecký, Ph.D.

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

EXAMINATIONS 2009 END-YEAR. COMP302 / SWEN304 Database Systems / Database System Engineering. Appendix

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

ASSIGNMENT NO 2. Objectives: To understand and demonstrate DDL statements on various SQL objects

Tables From Existing Tables

High Level Database Models

Assignment 3: SQL SELECT Queries

Database Management Systems

An Introduction to Structured Query Language

Information Systems. Database System Architecture. Relational Databases. Nikolaj Popov

The Entity-Relationship Model

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

CS 338 Basic SQL Part II

Introduction to Data Management. Lecture #10 (Relational Calculus, Continued)

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

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

CSE 344 Midterm. Wednesday, Nov. 1st, 2017, 1:30-2:20. Question Points Score Total: 100

CS 338: Computer Applications in Business: Databases

Note that it works well to answer questions on computer instead of on the board.

Chapter 3: Introduction to SQL

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

CSC 261/461 Database Systems Lecture 6. Fall 2017

CS127 Homework #2. Due: September 27th, :59 P.M.

Trigger is a stored procedure which is called implicitly by oracle engine whenever a insert, update or delete statement is fired.

Database Management Systems,

SQL DATA DEFINITION LANGUAGE

Why Relational Databases? Relational databases allow for the storage and analysis of large amounts of data.

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Table Joins and Indexes in SQL

Structure Query Language (SQL)

Intermediate SQL ( )

Question 1 (a) 10 marks

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

Omar Abdul Baki ID: Database Systems Assignment 1 January 27, Question 1.

CONCEPTUAL DESIGN: ER TO RELATIONAL TO SQL

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

An Introduction to Structured Query Language

MySQL Installation Guide (Windows)

SQL DATA DEFINITION LANGUAGE

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Ref1 for STUDENT RECORD DB: Ref2 for COMPANY DB:

Comp 5311 Database Management Systems. 4b. Structured Query Language 3

SQL- Updates, Asser0ons and Views

Pushing Predicates into Recursive SQL Common Table Expressions

Lecture 19: Query Optimization (1)

1 SQL Structured Query Language

DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E)

Unit 1 - Chapter 4,5

SQL (Structured Query Language)

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

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

Database design process

Physical Design. Elena Baralis, Silvia Chiusano Politecnico di Torino. Phases of database design D B M G. Database Management Systems. Pag.

Database Management Systems,

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

MySQL Installation Guide (Windows)

Chapter 2: The Relational Algebra

New Requirements. Advanced Query Processing. Top-N/Bottom-N queries Interactive queries. Skyline queries, Fast initial response time!

Set theory is a branch of mathematics that studies sets. Sets are a collection of objects.

Transcription:

Assignment #2 CS 348 - Spring 2015 Due on Friday, June 19, 2015, 9 AM For instructions on how to submit your assignment check the course website. 1

CS 348 - Spring 2015 : Assignment #2 QUESTION 1. Question 1. Consider the following database schema: Emp(eid : integer, ename : string, age : integer, salary : real) W orks(eid : integer, did : integer, pct time : integer) Dept(did : integer, budget : real, managerid : integer) Write SQL statements to answer the tasks that follow: 1. Create the Emp table. CREATE TABLE emp ( eid INTEGER PRIMARY KEY, ename VARCHAR(20), age INTEGER, salary DECIMAL(8, 2) ); 2. Insert the following instance into Emp. Emp eid ename age salary 1 Donald Duck 35 18, 000 4 Minnie Mouse 19 9, 000 2 Mickey Mouse 24 20, 000 3 Goof y 20 13, 000 INSERT INTO emp VALUES (1, Donald Duck, 35, 18000), (4, Minnie Mouse, 19, 9000 ), (2, Mickey Mouse, 24, 20000 ), (3, Goofy, 20, 13000 ); Page 2 of 11

CS 348 - Spring 2015 : Assignment #2 QUESTION 1. 3. Try altering the Emp table to define a constraint that will ensure that every employee makes at least $10,000. ALTER TABLE emp ADD CONSTRAINT check_salary CHECK (salary > 10000); 4. Give Minnie a salary increase of $2,000. UPDATE emp SET salary = salary + 2000 WHERE ename LIKE %Minnie% ; 5. Alter the Emp table to define a constraint that will ensure that every employee makes at least $10,000. ALTER TABLE emp ADD CONSTRAINT check_salary CHECK (salary > 10000); 6. Create the Dept table. CREATE TABLE dept ( did INTEGER PRIMARY KEY, budget DECIMAL(9, 2), managerid INTEGER ); 7. Alter the Dept table to define a foreign key constraint (with cascade delete) on managerid, referencing Emp table. ALTER TABLE dept ADD CONSTRAINT manager_fk FOREIGN KEY (managerid) REFERENCES emp (eid) ON DELETE CASCADE; 8. Insert a valid tuple in Dept. INSERT INTO dept VALUES (1, 180000, 1); 9. Delete all tuples from Emp. DELETE FROM emp; Page 3 of 11

CS 348 - Spring 2015 : Assignment #2 QUESTION 1. 10. List all tuples from Dept. SELECT * FROM dept; 11. Create the W orks table and define foreign key constraints on eid and did, referencing Emp and Dept tables, respectively. CREATE TABLE works ( eid INTEGER REFERENCES emp ON DELETE CASCADE, did INTEGER REFERENCES dept ON DELETE CASCADE, pct_time INTEGER, PRIMARY KEY (eid, did) ); 12. Populate all 3 relations with 3 to 6 valid tuples. INSERT INTO emp VALUES (1, Donald Duck, 35, 18000), (4, Minnie Mouse, 19, 11000 ), (2, Mickey Mouse, 24, 20000 ), (3, Goofy, 20, 13000 ); INSERT INTO dept VALUES (100, 180000, 1), (200, 280000, 1), (300, 360000, 3); INSERT INTO works VALUES (1, Page 4 of 11

CS 348 - Spring 2015 : Assignment #2 QUESTION 1. 100, 310), (1, 200, 120), (4, 300, 410), (3, 300, 180), (2, 200, 190); 13. Delete all information about employees whose salaries exceed that of the manager of one or more departments that they work in. Ensure that all the relevant integrity constraints are satisfied after your updates. DELETE FROM emp E WHERE E.eid IN (SELECT W.eid FROM works W, emp M, dept D WHERE W.did = D.did AND D.managerid = M.eid AND E.salary > M.salary); Page 5 of 11

Question 2. Consider the following database schema: P roduct(manufacturer, Model, T ype) Computer(M odel, Speed, RAM, HD, CD, Screen, P rice) P rinter(model, Color, T ype, P rice) and its instance: P roduct M anuf acturer M odel T ype A 1001 pc A 1002 pc A 1003 pc B 1004 pc B 1006 pc B 3002 printer B 3004 printer C 1005 pc C 1007 pc D 1008 pc D 1009 pc D 1010 pc D 2001 laptop D 2002 laptop D 2003 laptop D 3001 printer D 3003 printer E 2004 laptop E 2008 laptop F 2005 laptop G 2006 laptop G 2007 laptop H 3005 printer I 3006 printer Computer M odel Speed RAM HD CD Screen P rice 1001 3 256 120 48x 17 795 1002 2 256 120 48x 17 699 1003 3.5 512 180 48x 16 999 1004 3.5 1024 180 56x 16 1099 1005 3.5 256 160 56x 14 1099 1006 4.5 1024 200 56x 17 1199 1007 4.5 1024 300 56x 19 1249 1008 4 1024 160 56x 14 1699 1009 4.5 1024 180 56x 14 1299 1010 3.4 256 100 56x 17 700 2001 2 256 60 72x 14 1099 2002 2.1 192 40 48x 15.4 1298 2003 2.1 1024 50 56x 15 1499 2004 3 256 60 48x 15.5 1399 2005 3.5 256 50 48x 15.4 1299 2006 2 128 45 56x 17 1099 2007 2.5 256 80 56x 17 1999 2008 2 256 60 56x 17 1199 P rinter Model Color T ype P rice 3001 true ink jet 99 3002 true ink jet 69 3003 f alse laser 209 3004 true laser 391 3005 f alse ink jet 59 3006 true dry 129 Page 6 of 11

Write SQL queries to answer the following: 1. Which laptop models are shown in the P roduct relation? SELECT DISTINCT model WHERE model IS NOT NULL AND type = laptop ; 2. Find the set of all manufacturers ordered alphabetically. SELECT DISTINCT manufacturer WHERE manufacturer IS NOT NULL ORDER BY manufacturer ASC ; 3. Which computer models appear in the database? SELECT DISTINCT model FROM computer WHERE model IS NOT NULL; 4. Which computer models use 256 MB of RAM? SELECT DISTINCT model FROM computer WHERE ram = 256 AND model IS NOT NULL; 5. Which computer models cost less than $1100? SELECT DISTINCT model FROM computer WHERE price < 1100 AND model IS NOT NULL; 6. Which laptop models have a HD of at least 50 GB and cost less than $1400? SELECT DISTINCT p.model p, computer c WHERE p.model = c.model AND p.type = laptop AND c.hd >= 50 AND c.price < 1400; Page 7 of 11

7. Which laser printer models can print color documents? SELECT DISTINCT model FROM printer WHERE color = true AND type = laser AND model IS NOT NULL; 8. Which laptop models that have more than 256 MB of RAM are produced by manufacturer D? SELECT DISTINCT p.model p, computer c WHERE p.model = c.model AND p.type = laptop AND p.manufacturer = D AND c.ram > 256; 9. Which computer models that have more than 256 MB of RAM are produced by manufacturer D? SELECT DISTINCT p.model p, computer c WHERE p.model = c.model AND p.manufacturer = D AND c.ram > 256; 10. Which laptop models cost less than $1200? SELECT DISTINCT p.model p, computer c WHERE p.model = c.model AND p.type = laptop AND c.price < 1200; 11. Which manufacturers produce personal computers and printers? SELECT DISTINCT p1.manufacturer p1, product p2 WHERE p1.manufacturer = p2.manufacturer AND p1.type = pc AND p2.type = printer ; 12. Which manufacturers produce all the product types? Page 8 of 11

SELECT DISTINCT manufacturer WHERE manufacturer IS NOT NULL GROUP BY manufacturer HAVING Count(DISTINCT type) >= ALL (SELECT Count(DISTINCT type) ); 13. Which manufacturers supply only personal computers? WHERE type = pc AND manufacturer IS NOT NULL EXCEPT WHERE type <> pc ; 14. Which manufacturers supply only one type of product? WHERE manufacturer IS NOT NULL GROUP BY manufacturer HAVING Count(DISTINCT type) = 1; 15. You want to buy: a personal computer with at least 1024 MB of RAM and a CD with 56x, and a laptop with a screen larger than 15, and a printer cheaper than $300 all produced by the same manufacturer. Which manufacturers should you consider? SELECT DISTINCT pc.manufacturer FROM (product natural JOIN computer) AS pc, (product natural JOIN computer ) AS l, (product JOIN printer ON product.model = printer.model) AS pr WHERE pc.manufacturer = l.manufacturer AND pc.manufacturer = pr.manufacturer AND pc.type = pc AND pc.ram >= 1024 AND pc.cd = 56x Page 9 of 11

AND l.type = laptop AND l.screen > 15 AND pr.price < 300; 16. What HD sizes are common to at least 2 computers? SELECT hd FROM computer GROUP BY hd HAVING Count(DISTINCT model) >= 2; 17. Which manufacturers sell more than one computer that has a processor speed of less than 3 GHz? natural JOIN computer WHERE speed <= 3 AND manufacturer IS NOT NULL GROUP BY manufacturer HAVING Count(DISTINCT model) > 1; 18. Which manufacturers sell the fastest computers? SELECT DISTINCT manufacturer natural JOIN computer WHERE speed = (SELECT Max(speed) FROM computer) AND manufacturer IS NOT NULL; 19. Which manufacturers do not sell laptops? SELECT DISTINCT manufacturer WHERE manufacturer NOT IN ( WHERE type = laptop ); 20. Which manufacturers offer at least 2 laptop models? WHERE type = laptop AND manufacturer IS NOT NULL GROUP BY manufacturer HAVING Count(DISTINCT model) >= 2; Page 10 of 11

21. Which manufacturers offer exactly 3 types of personal computers? WHERE type = pc AND manufacturer IS NOT NULL GROUP BY manufacturer HAVING Count(DISTINCT model) = 3; 22. You need to buy: a computer that has at least 1024 MB of RAM, a processor speed of 3 GHz or 3.5 GHz, a HD of at least 160 GB, and a color printer. Which manufacturers can offer you this combination and at what total price? SELECT DISTINCT p1.manufacturer, c.price + i.price AS total_price FROM computer AS c, product AS p1, printer AS i, product AS p2 WHERE p1.manufacturer = p2.manufacturer AND c.model = p1.model AND c.ram >= 1024 AND c.hd >= 160 AND ( c.speed = 3 OR c.speed = 3.5 ) AND i.model = p2.model AND i.color = true; Page 11 of 11