Proposed solutions Project Assignment #1 (SQL)



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

Relational Algebra and SQL

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

SQL: Queries, Programming, Triggers

Chapter 5. SQL: Queries, Constraints, Triggers

SQL: Queries, Programming, Triggers

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

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

Query-by-Example (QBE)

Introduction to Microsoft Jet SQL

Database Security. Chapter 21

Relational Database: Additional Operations on Relations; SQL

INFO/CS 330: Applied Database Systems

6 QUERY-BY-EXAMPLE (QBE)

CMU - SCS / Database Applications Spring 2013, C. Faloutsos Homework 1: E.R. + Formal Q.L. Deadline: 1:30pm on Tuesday, 2/5/2013

SQL: QUERIES, CONSTRAINTS, TRIGGERS

CSE132A Solutions HW 1

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

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

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Security and Authorization. Introduction to DB Security. Access Controls. Chapter 21

1. SELECT DISTINCT f.fname FROM Faculty f, Class c WHERE f.fid = c.fid AND c.room = LWSN1106

SQL. Short introduction

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

UNIT 6. Structured Query Language (SQL) Text: Chapter 5

The SQL Query Language. Creating Relations in SQL. Referential Integrity in SQL. Basic SQL Query. Primary and Candidate Keys in SQL

Database Programming. Week *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

Building Queries in Microsoft Access 2007

Programming in postgresql with PL/pgSQL. Procedural Language extension to postgresql

SQL Tables, Keys, Views, Indexes

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

Multi-dimensional index structures Part I: motivation

Introduction to SQL and SQL in R. LISA Short Courses Xinran Hu

1. Write the query of Exercise 6.19 using TRC and DRC: Find the names of all brokers who have made money in all accounts assigned to them.

4. SQL. Contents. Example Database. CUSTOMERS(FName, LName, CAddress, Account) PRODUCTS(Prodname, Category) SUPPLIERS(SName, SAddress, Chain)

Databases and BigData

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

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Linear Equations and Inequalities

Microsoft Access 3: Understanding and Creating Queries

Information Systems SQL. Nikolaj Popov

Why Is This Important? Database Application Development. SQL in Application Code. Overview. SQL in Application Code (Contd.

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

When a variable is assigned as a Process Initialization variable its value is provided at the beginning of the process.

Cruise Line Agencies of Alaska. Cruise Ship Calendar for 2016 FOR PORT(S) = KTN AND SHIP(S) = ALL AND VOYAGES = ALL

IT2304: Database Systems 1 (DBS 1)

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

IT2305 Database Systems I (Compulsory)

Cruise Line Agencies of Alaska. Cruise Ship Calendar for 2016 FOR PORT(S) = KTN AND SHIP(S) = ALL AND VOYAGES = ALL

Binary Number System. 16. Binary Numbers. Base 10 digits: Base 2 digits: 0 1

Lecture 6: Query optimization, query tuning. Rasmus Pagh

CIS 631 Database Management Systems Sample Final Exam

Chapter 8 Integers 8.1 Addition and Subtraction

Intermediate SQL C H A P T E R4. Practice Exercises. 4.1 Write the following queries in SQL:

Lecture 6. SQL, Logical DB Design

Database Sample Examination

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

ITP 300: Database Web Development. Database Web Development (Monday section) Fall 2012 Course Units

In-Memory Database: Query Optimisation. S S Kausik ( ) Aamod Kore ( ) Mehul Goyal ( ) Nisheeth Lahoti ( )

Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query

Review of Scientific Notation and Significant Figures

Exposed Database( SQL Server) Error messages Delicious food for Hackers

MS Access: Advanced Tables and Queries. Lesson Notes Author: Pamela Schmidt

CSC 443 Data Base Management Systems. Basic SQL

Exercise 1: Relational Model

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

IENG2004 Industrial Database and Systems Design. Microsoft Access I. What is Microsoft Access? Architecture of Microsoft Access

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Active database systems. Triggers. Triggers. Active database systems.

Data Stream Management and Complex Event Processing in Esper. INF5100, Autumn 2010 Jarle Søberg

3/13/2012. Writing Simple C Programs. ESc101: Decision making using if-else and switch statements. Writing Simple C Programs

>

3.GETTING STARTED WITH ORACLE8i

RDBMS Using Oracle. Lecture Week 7 Introduction to Oracle 9i SQL Last Lecture. kamran.munir@gmail.com. Joining Tables

Databases 2011 The Relational Model and SQL

Advanced Query for Query Developers

Cloud and Big Data Summer School, Stockholm, Aug Jeffrey D. Ullman

Database Management Systems,

Chapter 6: Integrity Constraints

CS 564: DATABASE MANAGEMENT SYSTEMS

Chapter 8. SQL-99: SchemaDefinition, Constraints, and Queries and Views

Introduction to SQL for Data Scientists

Fixture List 2018 FIFA World Cup Preliminary Competition

CS170 Lab 11 Abstract Data Types & Objects

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

1. Then f has a relative maximum at x = c if f(c) f(x) for all values of x in some

Lecture 4: More SQL and Relational Algebra

Database Applications Microsoft Access

Advance DBMS. Structured Query Language (SQL)

Object-Oriented Query Languages: Object Query Language (OQL)

Save Time and Eliminate Data Entry Errors

Access Queries (Office 2003)

Scientific Notation. Section 7-1 Part 2

Objectives of SQL. Terminology for Relational Model. Introduction to SQL

About webpage creation

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model

SQL Server 2008 Core Skills. Gary Young 2011

public class ResultSetTable implements TabelModel { ResultSet result; ResultSetMetaData metadata; int num cols;

Cruise Line Agencies of Alaska. Cruise Ship Calendar for 2012 FOR PORT(S) = KTN AND SHIP(S) = ALL AND VOYAGES = ALL

Database Security. The Need for Database Security

Cruise Line Agencies of Alaska. Cruise Ship Calendar for 2013 FOR PORT(S) = KTN AND SHIP(S) = ALL AND VOYAGES = ALL

Transcription:

Proposed solutions Project Assignment #1 (SQL) (a) Database creation: create table sailor (sname CHAR(30), rating INTEGER) create table boat (bname CHAR (30), color CHAR (15), rating INTEGER) create table reservation (sname CHAR(30), bname CHAR (30), rday CHAR(15), starting INTEGER, ending INTEGER) (b) Instance upload: insert into sailor values ( Brutus, 1); insert into sailor values ( Andy, 8); insert into sailor values ( Horatio, 7); insert into sailor values ( Rusty, 8); insert into sailor values ( Bob, 1); insert into boat values ( SpeedQueen, white, 9); insert into boat values ( InterLake, red, 8); insert into boat values ( Marine, blue, 7); insert into boat values ( Bay, red, 3); values ( Andy, Interlake, Monday, 10, 14); values ( Andy, Marine, Saturday, 14, 16); values ( Andy, Bay, Wednesday, 8, 12); values ( Rusty, Bay, Sunday, 9, 12); values ( Rusty, Interlake, Wednesday, 13, 20); values ( Rusty, Interlake, Monday, 9, 11); values ( Andy, Bay, Wednesday, 9, 10); values ( Horatio, Marine, Tuesday, 16, 19); 1

(c) Instance listing: select * from sailor; select* from boat; select * ; (d) SQL queries: 1., bname from sailor, boat where sailor.rating >= boat.rating 2., count(bname) as number from sailor, boat where sailor.rating >= boat.rating group by sname) union, 0 as number from sailor from sailor, boat where sailor.rating boat.rating)) Note that Brutus and Bob are not qualified to sail any boat, so their count should be shown as zero. 3. (i) one using the MIN aggregate function, and from sailor where rating in (select MIN(rating) from sailor) 2

(ii) another without using MIN. select s.sname from sailor s from sailor where rating < s.rating) 4., boat where reservation.bname = boat.bname and boat.color <> red ) Without the assumption that each boat has only one color, we would write: where bname not in (select bname from boat where color = red )) 5. select s.sname from sailor s r, boat b where r.sname = s.sname and r.bname = b.bname and color = red ) Note that Brutus is one of these since he has no reservations. 3

6. (i) with NOT IN tests;, boat b where b.color = red and sname not in where bname = b.bname )) (ii) with NOT EXISTS tests; select r.sname r from boat b where b.color = red and not exists where sname = r.sname and reservation.bname = b.bname )) (iii) with COUNT aggregate functions. select r.sname r where (select count(*) from boat b where b.color = red and ( select count(*) where sname = r.sname and bname = b.bname) = 0 ) = 0 4

alternatively, select r.sname r, boat b where r.bname = b.bname and b.color = red group by r.sname having count (distinct r.bname) = (select count (distinct bname) from boat where color = red ) 7. create view unique as select distinct sailor.sname, rating, bname from sailor, reservation where sailor.sname = reservation.sname; select bname, avgrating = avg(rating * 1.0) from unique group by bname The reason for multiplying rating with 1.0 in avg(rating * 1.0) is to convert the rating to a real so that the average is not truncated to an integer. The role of the view unique is to provide the list of boats and sailors with their ratings who have reserved the boatwithout duplicates. This is to avoid counting the same sailor multiple times if the sailor has multiple reservations of the same boat. For example, the following alternative does not avoid this problem: select bname, avgrating = avg(rating) from sailor, reservation where sailor.sname = reservation.sname group by bname 5

(e) select x.bname, x.rday, x.sname as sname1, x.starting as start1, x.ending as end1, y.sname as sname2, y.starting as start2, y.ending as end2 x, reservation y where x.bname = y.bname and x.rday = y.rday and ((x.starting = y.starting and x.ending = y.ending and x.sname < y.sname) or (x.starting = y.starting and x.ending > y.ending) or (x.starting < y.starting and y.starting < x.ending)) Note that this lists only one tuple for every pair of conflicting reservations x, y. Indeed, suppose first the time intervals of the reservations are the same. Then the sailors of the two reservations should be different (otherwise x and y would be the same reservation). The double listing is avoided by requiring that x.sname < y.sname (this compares the strings). If the starting time of the two reservations are the same and the intervals are not equal then only one of them ends first, so requiring x.ending > y.ending avoids again the double listing. Finally, if the reservations have different starting times then only one starts first, so x.starting < y.starting avoids double listing. (f) Updates in SQL. 1. update boat set color = xxx where color = blue ; update boat set color = blue where color = red ; update boat set color = red where color = xxx 2. delete from sailor s from boat where s.rating boat.rating) 6