You are on page 1of 7

Midterm Examination

CMPT354 Database Systems I-- Spring 2012


Hassan Khosravi

Student ID:---------------------------------------------------------------------
Last Name: ---------------------------------------------------------------------
First Name: ---------------------------------------------------------------------

Instructions:

1. Answer all questions


2. Credits/points for each question is indicated in the brackets [ ] before the question.
3. No books (or other material) are allowed.
4. Attach extra sheets (available upon request) if required (write full name on each extra sheet).

Good Luck

Problem Score Max score

1 ------------------------------------------------------- 12
2 ------------------------------------------------------- 12
3 ------------------------------------------------------- 12
4 ------------------------------------------------------- 12
5 ------------------------------------------------------- 12
Total ----------------------------------------------- 60
1. Database management systems
[4] (a) Briefly describe two of the limitation of file systems compared to database management
systems.
They do not directly support querying and modifying data
Their support for database creation is limited to creation of files
You can lose data that has not been backed up
Uncontrolled concurrent accesses (Isolation) can lead to inconsistencies

[6] (b) Briefly describe the functionality of the following components in a database management
system.
Query Compiler
The query compiler consists of three major units
Query parser: builds tree of the structure from text
Query preprocessor: performs semantic checks on the query
Query optimizer: transforms the initial query plan into the best available sequence of operations.

Buffer Manager
The buffer manager communicates with the storage manager to get requested data from the
secondary storage (hard drive) into the main memory (RAM) where it could be used by other
components of the system.

Concurrency Control

Concurrency control manager is responsible for ensuring that transactions are executed
atomically and in isolation from one another.

[2] (c) What is meant by “Isolation” and “Atomicity”?

Isolation: refers to the requirement that no transaction should be able to interfere with another
transaction. The effect should be as if all of the transactions were running serially.

Atomicity: Requires that each transaction either happens completely or does not happen at all.
If one part of the transaction fails, the entire transaction must fails, and the database state is left
unchanged.
Consider a shop for flowers on the World Wide Web. It offers a selection of different bouquets
(bunches of flowers) described in the table “Flowers":

The table “Customer" contains information about customers:

Next, there is a table with information about orders and recipients (to whom the flowers should
be delivered)

“Prod" identifies the product, i.e. which bouquet should be delivered, “Cust" identifies the
customer (who ordered the flowers). “Date" is the date when the flowers should be delivered.
The rest of the information is for the person receiving the flowers. Some of the flowers are sent
via FedEx and other orders are given to local flower shops. Only flowers that have not yet been
delivered are kept in these two tables.
2. Entity Relationship model

[12] (a) Draw the entity relationship model for the described database.
3. Relational Algebra
Formulate the following queries in relational algebra

[2] (a) Give all information about flower bouquets which cost over $50

σ Price > 50(Flowers)

[2] (b) Print the recipient name and address of all orders to be delivered in Pittsburgh after
10/07/99. You can compare date values by the standard operators <, >, <=, >=

Π RName, RAddress(σ RCity='Pittsburgh' ^ Date > '10=07=99'(Order)

[4] (c) Print the name of “Loyal customers” and the total amount of money they have spent.
(Loyal customers are those that have spent over $300).

Π cname, spent ( σ spent >300 (


cust, sum(price)spent (flowers order Customer)
))

[4] (d) Print the name of customers that prefer mailing their flowers directly rather than send
them to local flower shops (based on only the current deliveries).
Π cname (σ m_order > s_order (
cust, count(ord)m_order (order mailed )

cust, count(ord)s_order (order Subcontracted )


))
4.SQL
Convert your answers from question three to SQL commands
[2] (a) Give all information about flower bouquets which cost over $50

Select *
From flowers
Where price >50

[2] (b) Print the recipient name and address of all orders to be delivered in Pittsburgh after
10/07/99. You can compare date values by the standard operators <, >, <=, >=

Select Rname, Raddress


From order
Where order.date > ‘1999-07-10’ AND Rcity = ‘Pittsburgh’

[4] (c) Print the name of “Loyal customers” and the total amount of money they have spent.
(Loyal customers are those that have spent over $300).

Select Cname, sum(price) as spent


From flowers Natual join order Natural join customer
Where spent > 300
Group by cust

[4] (d) Print the name of customers that prefer mailing their flowers directly rather than send
them to local flower shops (based on only the current deliveries).

Select Cname
From (
select customer, count(ord) as m_order
from order Natural join mailed order Natural join
group by cust) outer join (
select customer, count(ord) as s_order
from order Natural join subcontracted Natural join
group by cust
)
Where m_order > s_order
5. Triggers and constraints
[3] (a) Use constraint integrity on the field CZip in the Customer table to ensure that the input
value has five digits (between 10000 and 99999).

Alter table customer add constraint cust_zip check (czip = > 10000 AND czip <= 99999)

Create assertion cust_zip check (czip = > 10000 AND czip <= 99999)

[4] (b) Write a trigger to delete all the orders, mails, and subcontracts of a customer if a person
is deleted from a costumer table.

Create trigger remove


After delete on customer
For each row
Begin
Delete from mailed
Where ord in (select ord from where cust = old.cust);
Delete from subcontracts
Where ord in (select ord from order where cust = old.cust);
Delete from order
Where ord.cust = old.cust;
End

[5] (c) Use a trigger (or triggers) to ignore mailed or subcontracted orders from customers that
have over 50 undelivered flowers.
Create trigger ignore
Before insert on mailed
For each row
When 50 > (select count(*)
From(
Select ord from mailed left join order on mailed.ord = order.ord
where cust = (select cust from order where ord = new.ord)
union all
Select ord from subcontracted left join order on subcontracted.ord = order.ord
where cust = (select cust from order where ord = new.ord)
)
begin
raise (ignore);
end
You need to have a similar trigger for
Before insert on subcontracts
Before update on mailed
Before update on subcontracts

You might also like