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



Similar documents
Solution to the Assignment 2

Exercise 1: Relational Model

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

Relational Databases

Oracle SQL. Course Summary. Duration. Objectives

Chapter 13: Query Optimization

Relational Database: Additional Operations on Relations; SQL

Lecture 4: More SQL and Relational Algebra

Chapter 5. SQL: Queries, Constraints, Triggers

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

SQL Tables, Keys, Views, Indexes

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

DATABASE DESIGN AND IMPLEMENTATION II SAULT COLLEGE OF APPLIED ARTS AND TECHNOLOGY SAULT STE. MARIE, ONTARIO. Sault College

SQL: Queries, Programming, Triggers

DBMS / Business Intelligence, SQL Server

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

Introduction to Microsoft Jet SQL

SQL: Queries, Programming, Triggers

Relational Algebra and SQL

Simple SQL Queries (3)

SQL NULL s, Constraints, Triggers

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

IT2305 Database Systems I (Compulsory)

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

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

Oracle Database: SQL and PL/SQL Fundamentals

SQL SELECT Query: Intermediate

Oracle Database: SQL and PL/SQL Fundamentals

Data Structure: Relational Model. Programming Interface: JDBC/ODBC. SQL Queries: The Basic From

Advanced SQL - Subqueries and Complex Joins

Relational Algebra. Query Languages Review. Operators. Select (σ), Project (π), Union ( ), Difference (-), Join: Natural (*) and Theta ( )

Querying Microsoft SQL Server 2012

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

IT2304: Database Systems 1 (DBS 1)

Evaluation of Expressions

1 Structured Query Language: Again. 2 Joining Tables

Handling Missing Values in the SQL Procedure

Oracle Database: SQL and PL/SQL Fundamentals NEW

SQL Server 2008 Core Skills. Gary Young 2011

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

IINF 202 Introduction to Data and Databases (Spring 2012)

PSU SQL: Introduction. SQL: Introduction. Relational Databases. Activity 1 Examining Tables and Diagrams

Relational Algebra. Basic Operations Algebra of Bags

Ad Hoc Advanced Table of Contents

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

IS466 Decision Support Systems. SQL Server Business Intelligence Development Studio 2008 User Guide

MySQL for Beginners Ed 3

Query-by-Example (QBE)

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

Financial Data Access with SQL, Excel & VBA

SYLLABUS FOR CS340: INTRODUCTION TO DATABASES

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

SQL (structured query language)

Introduction to SQL for Data Scientists

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

Business Intelligence Extensions for SPARQL

Databasesystemer, forår 2005 IT Universitetet i København. Forelæsning 3: Business rules, constraints & triggers. 3. marts 2005

Database Management Systems 2015

Introduction to Querying & Reporting with SQL Server

Chapter 14: Query Optimization

- Eliminating redundant data - Ensuring data dependencies makes sense. ie:- data is stored logically

Advanced Query for Query Developers

Using Temporary Tables to Improve Performance for SQL Data Services

THE OPEN UNIVERSITY OF TANZANIA FACULTY OF SCIENCE TECHNOLOGY AND ENVIRONMENTAL STUDIES BACHELOR OF SIENCE IN DATA MANAGEMENT

Mini User's Guide for SQL*Plus T. J. Teorey

Figure 4.12.Insurancedatabase.

Module 1: Getting Started with Databases and Transact-SQL in SQL Server 2008

Instant SQL Programming

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

Writing Queries Using Microsoft SQL Server 2008 Transact-SQL

SQL: QUERIES, CONSTRAINTS, TRIGGERS

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

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

Microsoft' Excel & Access Integration

Effective Use of SQL in SAS Programming

Databases 2011 The Relational Model and SQL

THE OPEN UNIVERSITY OF TANZANIA FACULTY OF SCIENCE TECHNOLOGY AND ENVIRONMENTAL STUDIES BACHELOR OF SIENCE IN INFORMATION AND COMMUNICATION TECHNOLOGY

Basic academic skills (1) (2) (4) Specialized knowledge and literacy (3) Ability to continually improve own strengths Problem setting (4) Hypothesis

Lecture 6. SQL, Logical DB Design

Information Systems SQL. Nikolaj Popov

Chapter 1: Introduction. Database Management System (DBMS) University Database Example

Netezza SQL Class Outline

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #5: En-ty/Rela-onal Models- - - Part 1

Writing Queries Using Microsoft SQL Server 2008 Transact-SQL

Beginning C# 5.0. Databases. Vidya Vrat Agarwal. Second Edition

Chapter 1 Overview of the SQL Procedure

Part 4: Database Language - SQL

Advanced SQL. Lecture 3. Outline. Unions, intersections, differences Subqueries, Aggregations, NULLs Modifying databases, Indexes, Views

VIEWS virtual relation data duplication consistency problems

How To Learn To Write A Report In A Database On A Microsoft Powerbook

Databases What the Specification Says

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

Oracle Database 10g: Introduction to SQL

Essentials of SQL. Essential SQL 11/06/2010. NWeHealth, The University of Manchester

City University of Hong Kong. Information on a Course offered by Department of Computer Science with effect from Semester A in 2014 / 2015

Week 4 & 5: SQL. SQL as a Query Language

Querying Microsoft SQL Server (20461) H8N61S

Advance DBMS. Structured Query Language (SQL)

Introducing Microsoft SQL Server 2012 Getting Started with SQL Server Management Studio

CPS352 Database Systems: Design Project

Transcription:

Quiz 2: Database Systems I Instructor: Hassan Khosravi Spring 2012 CMPT 354 1. (10) Convert the below E/R diagram to a relational database schema, using each of the following approaches: (3)The straight E/R Method Depts(name, chair) Courses(number, deptname, room) LabCourses(number, deptname, allocation) (4)The object-oriented method Lab Courses has all the attributes of Courses. Depts(name, chair) Courses(number, deptname, room) LabCourses(number, deptname, room, allocation) (3) The nulls method Courses and LabCourses are combined into one relation. Depts(name, chair) Courses(number, deptname, room, allocation)

2. (10) Design a database for a bank, including information about customer and their accounts. Information about a customer includes their name, address, phone, and Social Security number. Accounts have numbers, types (e.g., saving, checking) and balances. Also record the customer(s) who own an account. a. (3)Draw the UML diagram for this database. b. (3)Change your diagram so an account can have only one customer. c. (2)Further change your diagram so a customer can have only one account. d. (2)Change your original diagram so that a customer can have a set of address (which are street-city-state triples) and a set of phones.

3. (10) Computing the following: on the two given relations. (You can write answer in table format if you find it easier) R(A,B): {(0,1), (2,3), (0,1), (2,4), (3,4)} S(B,C): {(0,1), (2,4), (2,5), (3,4),(0,2), (3,4)} Duplicate-elimination Operator Turn a bag into a set by eliminating all but one copy of each tuple Aggregation Operators SUM Return the sum of a column with numerical values AVG Return the average of a column with numerical values MIN Return smallest value for column with numerical values MAX Return largest value for column with numerical values COUNT Return the number of values in a column (including duplicates) Grouping Operator Subscript L is a list is a list of attributes of R, L can have following kinds of element: 1. A single attribute of R 2. An expression x->y, rename attribute x to y 3. An expression E->z, E is an expression, and rename E to z. Extended Projection Subscript L is a list is a list of attributes of R, L can have following kinds of element: 1. A single attribute of R 2. An expression x->y, rename attribute x to y 3. An expression E->z, E is an expression, and rename E to z. Sorting Operator Sorting the tuples of R by their value of C, and tuples with the same C-value are ordered by their B value. Outerjoin Operator Outerjoin is formed is formed by starting with R, and R S adding any dangling tuples from R or S. a. (2) (S) B+1 C-1 1 0 3 3 3 4 4 3 1 1 4 3

b. (2) (R) A B 0 1 2 3 2 4 3 4 d (3) (S) B AVG(C) 0 1.5 2 4.5 3 4 c. (3) R S A R.B S.B C 0 1 2 4 0 1 2 5 0 1 2 4 0 1 2 5 2 3 2 4 3 4 0 1 0 2

4. (10) This question introduces an example, concerning World War 2 capital ships. It involves the following relations: Classes(class, type, country, numguns, bore, displacement) Ships(name, class, launched) Battles(name, date) Outcomes(ship, battle, result) Ships are built in classes from the same design, and the class is usually named for the first ship of that class. The relation Classes records the name of the class, the type( bb for battleship or bc for battlecruiser), the country that build the ship, the number of main guns, the bore (diameter of the gun), and the displacement(weight, in tons). Relation Ships records the name of the ship, the name of its class, and the year in which the ship was launched. Relation Battles gives the name and date of battles involving these ships, and relation Outcomes gives the result (sunk, damaged, or ok) for each in each battle. Write the following SQL queries without subqueries a. (2) Find the ships heavier than 35,000 tons. SELECT S.name FROM Ships S, Classes C WHERE S.class = C.class AND C.displacement > 35000; b. (3) Find those countries that have both battleships and battlecruisers.(assuming the intersect operator is implemented) SELECT C1.country FROM Classes C1, Classes C2 WHERE C1.country = C2.country AND C1.type = 'bb' AND C2.type = 'bc' ; or SELECT country FROM Classes C1, WHERE C1.type = 'bb' Intersect SELECT country FROM Classes C1, WHERE C1.type = 'bc'

c. (5) Find those battles with at least three ships of the same country. SELECT O.battle FROM Outcomes O, Ships S, Classes C WHERE O.ship = S.name AND S.class = C.class GROUP BY C.country, O.battle HAVING COUNT(O.ship) > 3; 5. (10) Write the following SQL queries using at least one subquery (Based on the relations in question 4) a. (4) 6.3.2.a Find the countries whose ships had the largest number of guns. SELECT C.country FROM Classes C WHERE numguns IN (SELECT MAX(numGuns) FROM Classes ); SELECT C.country FROM Classes C WHERE numguns >= ALL (SELECT numguns FROM Classes); b. (6) Find the classes of ships, at least one of which was sunk in a battle. SELECT DISTINCT C.class FROM Classes C, Ships S WHERE C.class = S.class AND EXISTS (SELECT ship FROM Outcomes O WHERE O.result='sunk' AND O.ship = S.name ) ; SELECT DISTINCT C.class FROM Classes C, Ships S WHERE C.class = S.class AND S.name IN (SELECT ship FROM Outcomes O WHERE O.result='sunk' ) ;

6. (10) Write the following SQL queries. You can use any of the operators taught in the lectures (Based on the relations in question 4) a. (4) Find the average number of guns of battleship classes. SELECT AVG(C.numGuns) AS Avg_Guns FROM Classes WHERE type ='bb' ; b. (6) Find for each class the year in which the first ship of that class was launched. SELECT C.class, MIN(S.launched) AS First_Launched FROM Classes C, Ships S WHERE C.class = S.class GROUP BY C.class ;