a1 - Simon Fraser University

advertisement
CMPT 354
Database Systems
Simon Fraser University
Summer 2011
Instructor: Oliver Schulte
Assignment 1: Entity-Relationship Modelling, The Relational Model, and Relational
Algebra. MS SQL Server.
Total Marks: 360.
Due Date: Wednesday, June 1st, 14:20 pm.
Instructions: Check the instructions in the syllabus. The university policy on academic
dishonesty and plagiarism (cheating) will be taken very seriously in this course.
Everything submitted should be your own writing or coding. You must not let other
students copy your work. Discussions of the assignment is okay, for example to
understand the concepts involved. If you work in a group, put down the name of all
members of your group. On your assignment, put down your name, the number of the
assignment and the number of the course. Spelling and grammar count.
Handing in the Assignment. Please post your assignment on our course management
server https://courses.cs.sfu.ca/1114-cmpt-354-d100/ . You should post a word, rtf or
open office document, so we can add comments on your file directly (not pdf). For
diagrams please use a drawing program (recommended) or scan a paper and pencil
drawing.
Part I. E-R Modelling
1. Consider a model of a banking enterprise with customers and accounts. Customers
have the following attributes: id, name, street, city. The id is unique for each customer.
Accounts have the following attributes: account number, balance. There is only one
account with a given account number. There are two types of accounts, savings accounts
and checking accounts. All accounts are either savings or checking accounts, and no
account is both. For each savings account, there is an interest rate, and for each checking
account, there is an overdraft amount.
Draw an E-R diagram to represent these entities and their attributes.
2. Customers own accounts. For each account that a customer owns, we want to store a
start date, at which the account was opened, and a pin number that gives the customer
access to the account.
Draw an E-R diagram to represent these facts, using the entities you defined in part 1.
3. Let us add three more entities to our banking model. A loan has the following
attributes: loan number, loan type, and amount. Each loan has a unique loan number. The
attributes of a payment are date, amount and number. For a given loan, the number
identifies a unique payment, but payments for different loans may share the same
number. For example, payment #1 for Jack Smith identifies a unique payment in the
amount of $100, but payment #1 for Jackie Chan may be a different payment in the
amount of $1,000. A branch has the attributes city, name, and street. Each branch has a
unique name.
Draw an E-R diagram to represent these entities and their attributes.
4. Each loan is taken at a single unique branch. Customers borrow loans; a customer may
have more than one loan, and a given loan may be associated with more than one
customer (e.g., a couple may be co-signers on a mortgage). Using the entities you have
modelled in parts 1 and 3, add relationships to represent these facts. Combine these
relationships with the ones you modelled in part 2 into a single E-R diagram that captures
all the facts listed so far.
Note on E-R exercises: E-R modelling is as much a science as an art, and not a rote
procedure. The purpose of this assignment is to give you a bit of practice in capturing
informal descriptions in an E-R diagram. Just like in real life, if you feel that not all the
details that you need to know have been completely specified, use your common sense. If
in doubt, explain explicitly what assumptions you are making and how they are shaping
your model.
Total Marks for Part I: 145 Marks.
Marking Criteria:
Technical Correctness (including match between specifications and design) 60
Notational Correctness 40
Clarity of Diagram
30
Presentation (including quality of explanations, if any) 15
Part II: Translate E-R into relational models.
5. Write SQL statements that translate your E-R model for part I into the relational model
(that is, the SQL statements should create tables corresponding to the entities and
relationships in the E-R model). Chapter 3 gives you several examples of this process.
You may want to first translate the E-R model into a relational schema, then the relational
schema into SQL commands. E.g. in Chapter 3.1. the Students table is defined by
Students(sid: string, name: string, login: string, age: integer, gpa: real).
You are not required to write down the relational schema in this format. We won’t grade
you on the schema, only on the SQL commands. We just think the schema will help you.
Be sure to incorporate as many constraints mentioned in part I as possible. If there is a
constraint mentioned in part I that you cannot capture in SQL statements (using the
constructs of Ch.3, that is, no ASSERT or CHECK commands), describe what the
constraint is and explain why it cannot be captured.
Total Marks for Part II: 105 Marks
Marking Criteria:
Technical Correctness (including match between specifications and design) 60
Notational Correctness 30
Presentation (including quality of explanations, if any) 15
Part III: Relational Algebra
6. Consider the following relational schema.
Flight(number:integer, airline: string, from: string, to: string)
CanAirport(code: string, location: string)
USAirport(code: string, location: string)
The “from” and “to” fields in the Flight table point to the “code” fields in the airport
table. For example, a typical entry in the Flight table might be
56, AIRC, YEG, YYV
and a typical entry in the CanAirport table might be
YEG, Edmonton.
Write a query in relational algebra for the following requests.
a. Find all airlines that fly to all Canadian airports.
b. Find all airlines that fly to a Canadian airport but not to any US Airport.
c. Find the locations of all airports with at least one flight to New York.
Total Marks for Part III: 100.
Marking Criteria:
Technical Correctness 70
Notational Correctness 20
Presentation 10
Part IV: Programming.
7.



Please download SQL Server 2008 R2 from:
http://www.microsoft.com/express/database/
Download the AdventureWorksLT database from the course website.
Follow the instructions, found in another document, on the website, to install SQL
Server. (Words of Wisdom: when unpacking the database, put it in a high-level
directory, like your root directory, as shown in the instructions.)
Execute the following commands and write down the results. We will cover the
details and meanings of the commands later on in the course. For the moment, I want to
verify that everyone successfully set up the AdventureWorks database, so do not worry
about understanding the meaning of the commands. [1 point each]
a) Tell me the number of records in the Address table.
SELECT COUNT(*) FROM SalesLT.Address
b) Tell me the average order quantity sold.
SELECT AVG(OrderQty) FROM SalesLT.SalesOrderDetail
c) Tell me the number of Product Models (tuples/records returned as a result of this
query)
SELECT COUNT(DISTINCT(Name)) FROM SalesLT.ProductModel
d) Tell me the number of addresses in Bellevue.
SELECT COUNT(*) FROM SalesLT.Address WHERE City = 'Bellevue'
e) Tell me the amount of the largest Sales Order.
SELECT MAX(TotalDue) FROM SalesLT.SalesOrderHeader
Total Marks: 10
Marking Criteria
Each query answer 1 point.
Setting up the system 5 points.
Download