0% found this document useful (0 votes)
129 views

HW 1

The document describes an ER diagram for part of a bank database and translates it into relational schema. It then provides SQL statements to create tables for an artwork database based on an ER diagram. Finally, it presents relational algebra expressions to answer queries on a library database schema.

Uploaded by

kp
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views

HW 1

The document describes an ER diagram for part of a bank database and translates it into relational schema. It then provides SQL statements to create tables for an artwork database based on an ER diagram. Finally, it presents relational algebra expressions to answer queries on a library database schema.

Uploaded by

kp
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Part A Database Design

1. Consider the ER diagram shown below for part of a BANK


database. Each bank can have multiple
branches, and each branch can have multiple accounts and loans.
a. List the (nonweak) entity types in the ER diagram.
Customer, Account, Loan, Bank
b. Is there a weak entity type? If so, give its name, partial key, and
identifying relationship
Bank_Branch is a weak entity. Its partial key is Branch-no and its
identifying relationship is BRANCHES with Bank.
c. What constraints do the partial key and the identifying
relationship of the weak entity type specify in this diagram?
The constraint of the partial key Branch-no is that we need to
combine Branch-no with Code, the key from its owner entity set
Bank, to uniquely identify a Bank_Branch.
The constraints of the identifying relationship are:
1) The identifying relationship between the owner entity set,
Bank, and the weak entity set, Bank_Branch must be one to
many and Bank_Branch could only have one Bank as its
owner.
2) The weak entity set, Bank_Branch, must have total
participation in the identifying relationship set, BRANCHES.
d. Translate this ER model into a relational schema (not SQL
notation)
Customer (Ssn: string, Phone:string, Name: string, Addr:
string)
Bank (Code: string, Name: string, Addr: string)
Bank_Branch (Branch-no: string, Code: string, Addr: string )
Account (Acct-no: string, Balance: real, Type: string, Ssn:
string, Branch-no: string, Code: string)
Loan (Loan-no: string, Type: string, Amount: real, Ssn: string,
Branch-no: string, Code: string)
2. Draw ER model for the following scenario.

3. Write SQL statements to create the corresponding relations to the


ER diagram you designed for
Exercise 2. If your translation cannot capture any constraints in the
ER diagram, explain why.
Artist:
CREATE TABLE Artist(Name CHAR(50), Birthplace CHAR(50), Age
INTEGER, Style CHAR(20), PRIMARY KEY(Name) )
Artwork:
CREATE TABLE Artwork(Title CHAR(50), Artist_name CHAR(50)
NOT NULL, Create_date DATE, Type CHAR(20), Price REAL,
PRIMARY KEY(Title) , FOREIGN KEY(Artist_name) REFERENCES
Artist ON DELETE NO ACTION)
Group:
CREATE TABLE Group(Name CHAR(50), PRIMARY KEY(Name))
Artwork_Group:
CREATE TABLE Artwork_Group(Title CHAR(50) NOT NULL,
Group_name CHAR(50) NOT NULL, PRIMARY KEY(Title,
Group_name), FOREIGN KEY(Title) REFERENCES Artwork ON
DELETE NO ACTION, FOREIGN KEY(Group_name) REFERENCES
Group ON NO ACTION)
Customer:
CREATE TABLE Customer (Name CHAR(50), Address CHAR(50),

Amount REAL,
PRIMARY KEY(Name))
Customer_InterestedArtist:
CREATE TABLE Customer_InterestedArtist (Customer_name
CHAR(50),
Aritist_name
CHAR(50),
PRIMARY
KEY(Customer_name,
Aritist_name),
FOREIGN
KEY(Customer_name) REFERENCES Customer ON DELETE NO
ACTION, FOREIGN KEY(Aritist_name) REFERENCES Artist ON
DELETE NO ACTION)
Customer_InterestedGroup:
CREATE TABLE Customer_InterestedGroup (Customer_name
CHAR(50),
Group_name
CHAR(50),
PRIMARY
KEY(Customer_name,
Group
_name),
FOREIGN
KEY(Customer_name) REFERENCES Customer ON DELETE NO
ACTION, FOREIGN KEY(Group _name) REFERENCES Group ON
DELETE NO ACTION)
As the relationship between artistwork and group is many to many,
we could not put Group_name attribute in artistwork table. Thus, it
is impossible to ensure the total participation in the relationship as
there is no constraint can guarantee that every group tuple will be
in the artistwork_group table.

PART B Relational Model and Algebra


4. Consider an update for the AIRLINE database to enter a
reservation of a particular flight or flight leg on a given date.
a. Give the operations for this update.
This involves two operations:
1) An INSERT operation in SEAT_RESERVATION.
2) An UPDATE operation in LEG_INSTANCE to reduce the
Number_of_available_seats
b. What type of constraints would you expect to check?
1) The INSERT operation needs to make sure the flight number,
leg number, seat number, customer name and customer
phone are not longer than the length limited. It also needs to
check whether the date is valid or not.
2) The INSERT operation needs to make sure the combination of
Flight number, leg number, Date and Seat number is unique
3) The INSERT operation needs to make sure flight number
could be found in FLIGHT relation, leg number could be found

in FLIGHT_LEG relation and date could be found in


LEG_INSTANCE relation.
4) The INSERT operation needs to make sure the any of Flight
number, leg number, Date and Seat number is not null
5) The UPDATE operation needs to check after the deduction the
value of Number_of_available_seats is still not less than 0
c. Which of these constraints are key, entity integrity and referential
integrity constraints, and which are not?
1) and 5) is domain constraint.
2) is key constraint.
3) is referential integrity constraint
4) is entity constraint.
d. Specify all the referential integrity constraints that hold on the
schema shown in the Figure below.
1) FLIGHT_LEG(Flight_number) -> Flight(Flight_number)
2) LEG_INSTANCE(Flight_number) -> Flight(Flight_number),
LEG_INSTANCE(Leg_Number) -> FLIGHT_LEG(Leg_Number)
3) FARE(Flight_number) -> Flight(Flight_number)
4) CAN_LAND(Airplane_type_name)->
AIRPLANE_TYPE(Airplane_type_name),
CAN_LAND(Airport_code)-> AIRPORT(Airport_code)
5) SEAT_RESERVATION(Flight_number) -> Flight(Flight_number),
SEAT_RESERVATION(Leg_number) ->
FLIGHT_LEG(Leg_Number), SEAT_RESERVATION(Date) ->
LEG_INSTANCE (Date)
5. Consider the LIBRARY relational database schema shown figure
below, which is used to keep track of books, and borrowers, and
book loans. Write down relational expressions for the following
queries:
a. How many copies of the book titled The Lost Tribe are owned by
the library branch whose
name is Sharpstown?

(temp1,

Book_id

(temp2,

Branch_id

NCH)))

Title=The Lost Tribe

(BOOK) ))

Branch_name=Sharpstown

(LIBRARY_BRA

No_of_copies

(temp1 BOOK_COPIES temp2)

b. How many copies of the book titled The Lost Tribe are owned by
each library branch?

(temp1,
No_of_copies

Book_id

Title=The Lost Tribe

(BOOK) ))

(temp1 BOOK_COPIES)

c. Retrieve the names of all borrowers who do not have any books
checked out.

(temp1,

Card_no

(BOOK_LOANS)

BORROWER)

(temp2, BORROWER temp1)


Name

(temp2)

d. For each book that is loaned out from the Sharpstown branch and
whose Due_date is today, retrieve the book title, the borrowers
name, and the borrowers address.

(temp1,

Branch_id

Branch_name=Sharpstown

(LIBRARY_BRA

NCH)))

(temp2, Due_date=TODAY(BOOK_LOANS) temp1)


(temp3, temp2 BOOK BORROWER)
Title, Name, Address

(temp3)

e. For each library branch, retrieve the branch name and the total

number of books loaned out from that branch

(temp1, LIBRARY_BRANCH BOOK_LOANS)


Branch_name, count

Branch_id, count()

(temp1))

f. Retrieve the names, addresses, and number of books checked out


for all borrowers who have more than five books checked out.

(temp1, BORROWER BOOK_LOANS)


(temp2,

Card_no, Name, Address, count()

(temp1))

(temp3, count>5(temp2))
Name, Address,Count

(temp3)

g. For each book authored (or coauthored) by Stephen King, retrieve


the title and the number of copies owned by the library branch
whose name is Central.

(temp1,

Branch_name=Central

Branch_id

LIBRARY_BRANCH) )

(temp2, temp1 BOOK_COPIES)


(temp3, Author_name =Stephen King BOOK_AUTHORS))
Title, No_of_copies

(temp2 temp3 BOOK)

6. Consider the two tables T1 and T2 shown in the figure below.


Show the results of the following operations:
a.

P
10
10
25

Q
a
a
a

R
5
5
6

A
10
10
25

B
b
b
c

C
6
5
3

P
15
15

Q
b
b

R
8
8

A
10
10

B
b
b

C
6
5

P
10
10
25
15

Q
a
a
a
b

R
5
5
6
8

A
10
10
25
NULL

B
b
b
c
NULL

C
6
5
3
NULL

P
15
15
NULL

Q
b
b
NULL

R
8
8
NULL

A
10
10
25

B
b
b
c

C
6
5
3

B
b

C
6

b.

c.

d.

e.
P
10
15
25
10
25
10

Q
a
b
a
b
c
b

R
5
8
6
6
3
5

f.
P
10

Q
a

R
5

A
10

You might also like