Database System Solutions
Database System Solutions
Review Questions
4.1 Discuss each of the following concepts in the context of the relational data
model:
4.2 Describe the relationship between mathematical relations and relations in the
relational data model?
Any set of n-tuples from this Cartesian product is a relation on the n sets. Now
let A1, A2, . . ., An be attributes with domains D1, D2, . . . , Dn. Then the set
{A1:D1, A2:D2, . . . , An:Dn} is a relation schema. A relation R defined by a relation
schema S is a set of mappings from the attribute names to their corresponding
domains. Thus, relation R is a set of n-tuples:
Each element in the n-tuple consists of an attribute and a value for that attribute.
4.3 Describe the term “normalized reaction”. Why are constraints so important in a
relational database?
Codd described a normalized relation as a relation that does not contain any repeating
groups or multivalued attributes. The attributes are functionally dependent on only the
primary key. Constraints enforce data integrity, consistency, and business rules.
• has a name that is distinct from all other relation names in the relational schema;
• each cell contains exactly one atomic (single) value;
• each attribute has a distinct name;
• the values of an attribute are all from the same domain;
• each tuple is distinct; there are no duplicate tuples;
• the order of attributes has no significance;
• the order of tuples has no significance, theoretically. (However, in practice, the
order may affect the efficiency of accessing tuples.)
The primary key is the candidate key that is selected to identify tuples uniquely within a
relation. A foreign key is an attribute or set of attributes within one relation that matches
the candidate key of some (possibly the same) relation. Discussed in Section 4.2.5.
4.6 Define the two principal integrity rules for the relational model. Discuss why it
is desirable to enforce these rules.
Two rules are Entity Integrity (Section 4.3.2) and Referential Integrity (Section 4.3.3).
Views can be described as virtual tables that utilize one or more relational operations
operating on the base relations to produce another relation. Views data are dynamically
assigned by the DBMS through relational operations set by users. Views are useful in a
number of ways including those described in section 4.4.2. These include data security,
easy data customization, data flexibility, and simplification of complex operations on
base relations.
Exercises
where Hotel contains hotel details and hotelNo is the primary key;
Room contains room details for each hotel and (roomNo, hotelNo) forms the primary
key;
Booking contains details of the bookings and (hotelNo, guestNo, dateFrom) forms the
primary key;
and Guest contains guest details and guestNo is the primary key.
4.8 Identify the foreign keys in this schema. Explain how the entity and referential
integrity rules apply to these relations.
For each relation, the primary key must not contain any nulls.
Room is related to Hotel through the attribute hotelNo. Therefore, the hotelNo in Room
should either be null or contain the number of an existing hotel in the Hotel relation. In
this case study, it would probably be unacceptable to have a hotelNo in Room with a
null value.
Booking is related to Hotel through the attribute hotelNo. Therefore, the hotelNo in
Booking should either be null or contain the number of an existing hotel in the Hotel
relation. However, because hotelNo is also part of the primary key, a null value for this
attribute would be unacceptable. Similarly for guestNo. Booking is also related to Room
through the attribute roomNo.
4.9 Produce some sample tables for these relations that observe the relational
integrity rules. Suggest some general constraints that would be appropriate for this
schema.
Student should provide some sample tables, observing entity and referential integrity. In
particular, ensure the uniqueness for the composite primary keys of the Room and
Booking tables.
• There can be no two bookings for the same room in the same hotel on the same
day.
• For the Booking relation, dateFrom must be before dateTo.
• Room price must be greater than 0 and less than £200.
4.10 Analyze the RDBMSs that you are currently using. Determine the support the
system provides for primary keys, alternate keys, foreign keys, relational integrity, and
views.
This is a small student project, the result of which is dependent on the system analyzed.
4.11 Implement the above schema in one of the RDBMSs you currently use.
Generate two user-views that are accessible and updatable as well as two other user-
views that cannot be updated.
Review Questions
5.1 What is the difference between a procedural and non-procedural language?
How would you classify the relational algebra and relational calculus?
Procedural language: a language that allows user to tell the system what
data is needed and exactly how to retrieve the data.
Non-procedural language: a language that allows user to state what data is needed
rather than how it is to be retrieved.
In tuple relational calculus, we are interested in finding tuples for which a predicate is
true. The calculus is based on the use of tuple variables. A tuple variable is a variable
that “ranges over” a named relation: that is, a variable whose only permitted values are
tuples of the relation. (The word “range” here does not correspond to the mathematical
use of range, but corresponds to a mathematical domain.) For example, to specify the
range of a tuple variable S as the Staff relation, we write: Staff(S)
To express the query “Find the set of all tuples S such that F(S) is true,” we can write:
{S | F(S)}
F is called a formula (well-formed formula, or wff in mathematical logic). See section
5.2.1
Variables are used in domain relational calculus also, but in this case the variables take
their values from domains of attributes rather than tuples of relations. An expression in
the domain relational calculus has the following general form:
Where d1, d2, . . . , dn, . . . , dm represent domain variables and F(d1, d2, . . . , dm)
represents a formula composed of atoms. See section 5.2.2.
5.3 Define the five basic relational algebra operations. Define the Join,
Intersection, and Division operations in terms of these five basic operations.
• Can rewrite q-Join in terms of the basic selection and Cartesian product
operations:
R 3 F S = sF (R ´ S)
• Can express the intersection operation in terms of the set difference operation:
R Ç S = R – (R – S)
T1 = PC(R)
T2 = PC( (S x T1) – R)
T = T1 – T2
5.4 Discuss the differences between the five Join operations: Theta join, Equijoin,
Natural join, Outer join, and Semijoin. Give examples to illustrate your answer.
Theta join RFS Produces a relation that contains tuples satisfying the predicat
Cartesian product of R and S.
Equijoin RFS Produces a relation that contains tuples satisfying the predicat
only contains equality comparisons) from the Cartesian produ
S.
Natural join R S An Equijoin of the two relations R and S over all common attrib
occurrence of each common attribute is eliminated.
(Left) Outer join R S A join in which tuples from R that do not have matching values
common attributes of S are also included in the result relation.
Semijoin RFS Produces a relation that contains the tuples of R that participa
of R with S.
5.5 There are different types of join operations that can be used to retrieve data,
based on different relations. Describe the relation between theta and equal join.
The Theta join operation (R F S) defines a relation that contains tuples satisfying the
predicate F from the Cartesian product of R and S. The predicate F is of the form R.ai u
S.bi, where u may be one of the comparison operators (<,>,=, ≤, ≥, ≠). In the case
where the predicate F contains only equality (=), the term Equijoin is used instead.
Equal join is one form of theta join.
5.6 Define the structure of a (well-formed) formula in both the tuple relational
calculus and domain relational calculus.
A (well-formed) formula is made out of one or more atoms, where an atom has one of
the following forms:
• an atom is a formula;
• if F1and F2 are formulae, so are their conjunction F1 Ù F2, their
disjunction F1 Ú F2, and the negation ~F1;
• if F is a formula with free variable X, then ($X)(F) and (“X)(F) are also formulae.
A (well-formed) formula is made out of one or more atoms, where an atom has one of
the following forms:
Exercises
For the following exercises, use the Hotel schema defined at the start of the Exercises
at the end of Chapter 4.
5.8 Describe the relations that would be produced by the following relational
algebra operations:
This will produce a relation with a single attribute (hotelNo) giving the number of those
hotels with a room price greater than £50.
This will produce a join of the Hotel and Room relations containing all the attributes of
both Hotel and Room (there will be two copies of the hotelNo attribute). Essentially this
will produce a relation containing all rooms at all hotels.
This will produce a (left outer) join of Guest and those tuples of Booking with an end
date (dateTo) greater than or equal to 1-Jan-2007. All guests who don’t have a booking
with such a date will still be included in the join. Essentially this will produce a relation
containing all guests and show the details of any bookings they have beyond 1-Jan-
2002.
This will produce a (semi) join of Hotel and those tuples of Room with a price greater
than £50. Only those Hotel attributes will be listed. Essentially this will produce a
relation containing all the details of all hotels with a room price above £50.
This will produce a relation containing the names of all guests who have booked all
hotels in London.
5.9 Provide the equivalent tuple relational calculus and domain relational calculus
expressions for each of the relational algebra queries given in Exercise 4.8.
DRC: {hotelNo | ($rNo, typ, prce) (Room (rNo, hotelNo, typ, prce) Ù prce > 50)}
DRC: {hNo, hName, cty, rNo, hNo1, typ, prce | (Hotel(hNo, hName, cty) Ù Room(rNo,
hNo1, typ, prce) Ù (hNo = hNo1))}
DRC: {guestName | ($gNo, gName, gAddress, hNo, gNo1, dFrom, dTo, rNo,
5.10 Describe the relations that would be produced by the following tuple relational
calculus expressions:
• {hotelName | Hotel(H) Ù H.city = ‘London’}
This will produce a relation containing the names of all hotels in London.
This will produce a relation containing the names of all hotels that have a room price
above £50.
This will produce a relation containing the names of all hotels that have a booking for a
guest called John Smith.
This will produce a relation containing the names of guests who have more than one
booking at the same hotel, along with the hotel number and the dates of the bookings.
5.11 Provide the equivalent domain relational calculus and relational algebra
expressions for each of the tuple relational calculus expressions given in Exercise 4.10.
• {hotelName | Hotel(H) Ù H.city = ‘London’}
DRC: {hotelName | ($hNo, cty, rNo, hNo1, typ, prce) (Hotel(hNo, hotelName, cty) Ù
DRC: {hotelName | ($hNo, cty, gNo, gName, gAddress, hNo1, gNo1, dFrom, dTo,
gNo, gAddress, hNo1, gNo1, dTo1, rNo1, hNo2, gNo2, dTo2, rNo2)
(dateFrom1 ¹ dateFrom2))}
5.12 Generate the relational algebra, tuple relational calculus, and domain
relational calculus expressions for the following queries:
TRC: {H | Hotel(H)}
(b) List all single rooms with a price below £20 per night.
(d) List the price and type of all rooms at the Grosvenor Hotel.
(f) List the details of all rooms at the Grosvenor Hotel, including the name of the
guest staying in the room, if the room is occupied.
RA: (Room hotelNo (shotelName = ‘Grosvenor Hotel’(Hotel))) // Outer Join
5.13 Using relational algebra, produce a report of all employees from the IT and
planning departments who are born after 1990.
The report can be obtained through union operator by combining results of a query that
retrieves employees born after 1990 from IT and the query of the employees born after
1990 from planning.
sdeptNo=‘IT’Ù dob>’31-12-1990’(employee) υ sdeptNo=‘PL’Ù dob>’31-12-1990’(employee)
The report could also be produced by a query that uses OR (V) operator to combine
result of the employees born after 1990 from IT and the result of employees born after
1990 and from Planning department.
RA: Employee
TRC: {E | Employee(E)}
5.15 List all the details of employees who are female and born after 1990.
5.16 List all employees who are not managers and are paid more than $1500.
5.17 Produce a list of the names and addresses of all employees who work for the
IT department.
DRC: {fName, lName, address | ($empNo, DOB, sex, position, deptNo, deptNo1,
deptName, mgrEmpNo) (Empoyee(empNo, fName, lName, address, DOB, sex,
position, deptNo) Ù Department (deptNo1, deptName, mgrEmpNo) Ù (deptNo =
deptNo1) Ù (deptName = ‘IT’))}
5.18 Produce a list of the names of all employees who work on the SCCS project.
DRC: {fName, lName | ($empNo, address, DOB, sex, position, deptNo, projNo,
projName, deptNo1, empNo1, projNo1, dateWorked, hoursWorked) (Employee
(empNo, fName, lName, address, DOB, sex, position, deptNo) Ù Project (projNo,
projName, deptNo1) Ù WorksOn (empNo1, projNo1, dateWorked, hoursWorked) Ù
(empNo = empNo1) Ù (projNo = projNo1) Ù (projName = ‘SCCS’))}
5.19 Produce a complete list of all managers who are due to retire this year, in
alphabetical order of surname.
sdepno=‘IT’(project)
5.22 Using the union operator, retrieve the list of employees who are neither
managers nor supervisors. Attributes to be retrieved are first name, last name, position,
sex and department number.
where Book contains details of book titles in the library and the ISBN is
the key.
BookCopy contains details of the individual copies of books in the library and
copyNo is the key. ISBN is a foreign key identifying the book title.
Borrower contains details of library members who can borrow books and
borrowerNo is the key.
BookLoan contains details of the book copies that are borrowed by library
members and copyNo/dateOut forms the key. borrowerNo is a foreign key identifying
the borrower.
Formulate the additional queries in relational algebra, tuple relational calculus, and
domain relational calculus.
5.24 List all book titles.
RA: Ptitle(Book)
RA: Borrower
TRC: {B | Borrower(B)}
RA: Ptitle(syear=‘2012’(Book))
DRC: {title | ($ISBN, edn, yr) (Book(ISBN, title, edn, yr) Ù yr=’2012’}
5.27 List all copies of book titles that are available for borrowing.
DRC: {copyNo, title | ($ISBN, edn, yr, ISBN, avail) (Book(ISBN, title, edn, yr) Ù
BookCopy(copyNo, ISBN, avail) Ù avail=’Y’)}
5.28 List all copies of the book title “Lord of the Rings” that are available for
borrowing.
DRC: {copyNo | ($ISBN, edn, yr, ISBN, avail) (Book(ISBN, title, edn, yr) Ù
BookCopy(copyNo, ISBN, avail) Ù avail=’Y’ Ù title= ‘Lord of the Rings’)}
5.29 List the names of borrowers who currently have the book title “Lord of the
Rings” on loan.
DRC: {borrowerName | ($ISBN, title, edn, yr, copyNo, avail, bNo, bAddress, dOut,
dDue) (Book(ISBN, title, edn, yr) Ù BookCopy(copyNo, ISBN, avail) Ù
5.33 How many times has the book title with ISBN “0-321-52306-7” been
borrowed?
5.34 Produce a report of book titles that have been borrowed by “Peter Bloomfield”.
5.35 For each book title with more than 3 copies, list the names of library members
who have borrowed them.
5.36 Produce a report with the details of borrowers who currently have books
overdue.
5.37 Produce a report detailing how many times each book title has been borrowed.
5.38 Analyze the RDBMSs that you are currently using. What types of relational
languages does the system provide? For each of the languages provided, what are the
equivalent operations for the eight relational algebra operations defined in Section 5.1?
This is a small student project, the result of which is dependent on the system analyzed.
However, it is likely that the supported languages will be based around SQL and QBE,
in which case, the student should attempt to map the various SQL clauses to the
algebra and calculus. See also Exercise 5.31.
Review Questions
6.1 Briefly describe the four basic SQL DML statements and explain their use.
Section 6.3 describes the usage of basic data manipulation SQL statements; SELECT
INSERT, UPDATE and DELETE.
6.2 Explain the importance and application of the WHERE clause in the UPDATE
and DELETE statements.
Application
Importance
6.3 Explain the function of each of the clauses in the SELECT statement. What
restrictions are imposed on these clauses?
If the SELECT list includes an aggregate function and no GROUP BY clause is being
used to group data together, then no item in the SELECT list can include any reference
to a column unless that column is the argument to an aggregate function.
When GROUP BY is used, each item in the SELECT list must be single-valued per
group. Further, the SELECT clause may only contain:
• Column names.
• Aggregate functions.
•
• An expression involving combinations of the above.
All column names in the SELECT list must appear in the GROUP BY clause unless the
name is used only in an aggregate function.
6.4 What restrictions apply to the use of the aggregate functions within the
SELECT statement? How do nulls affect the aggregate functions?
An aggregate function can be used only in the SELECT list and in the HAVING clause.
Apart from COUNT(*), each function eliminates nulls first and operates only on the
remaining non-null values. COUNT(*) counts all the rows of a table, regardless of
whether nulls or duplicate values occur.
6.5 How can results from two SQL queries be combined? Differentiate how the
INTERSECT and EXCEPT commands work.
There are number of relational operators that can be used to combine results of two
queries. Some of them are UNION, INTERSECT, and EXCEPT. For the two results to
be semanticaly combined, order and number of columns from both results should be
identical, and the combining columns should be of the same domain.
The difference between the two is well explained in section 6.3.9 where the intersect
returns the rows that are common to both result sets, and except returns rows that are
available in the first result set and not in the second result set.
The subquery types are scalar, row, and table subquery. The main difference is on the
nature of results they return; scalar subquery returns a single column and single row
that is a single value, row subquery returns multiple columns and single row, while table
subquery returns multiple columns and multiple rows. It is so import to understand the
nature of subquery result so that you can make the best choice about the operator to be
used
Exercises
For the Exercises 6.7 – 6.28, use the Hotel schema defined at the start of the Exercises
at the end of Chapter 3.
Simple Queries
6.9 List the names and addresses of all guests in London, alphabetically ordered
by name.
ORDER BY guestName;
Strictly speaking, this would also find rows with an address like: ‘10 London Avenue,
New York’.
6.10 List all double or family rooms with a price below £40.00 per night, in
ascending order of price.
SELECT * FROM Room WHERE price < 40 AND type IN (‘D’, ‘F’)
ORDER BY price;
6.11 List the bookings for which no dateTo has been specified.
6.14 What is the total revenue per night from all double rooms?
6.15 How many different guests have made bookings for August?
6.16 List the price and type of all rooms at the Grosvenor Hotel.
SELECT price, type FROM Room
WHERE hotelNo =
WHERE guestNo =
hotelNo =
6.18 List the details of all rooms at the Grosvenor Hotel, including the name of the
guest staying in the room, if the room is occupied.
6.19 What is the total income from bookings for the Grosvenor Hotel today?
6.20 List the rooms that are currently unoccupied at the Grosvenor Hotel.
6.21 What is the lost income from unoccupied rooms at the Grosvenor Hotel?
SELECT SUM(price) FROM Room r
Grouping
GROUP BY hotelNo;
GROUP BY hotelNo;
6.24 What is the average number of bookings for each hotel in August?
SELECT AVG(X)
FROM ( SELECT hotelNo, COUNT(hotelNo) AS X
FROM Booking b
GROUP BY hotelNo);
6.25 What is the most commonly booked room type for each hotel in London?
SELECT MAX(X)
city = ‘London’
GROUP BY type);
6.26 What is the lost income from unoccupied rooms at each hotel today?
b.hotelNo = h.hotelNo)
GROUP BY hotelNo;
Populating Tables
6.29 Investigate the SQL dialect on any DBMS that you are currently using.
Determine the compliance of the DBMS with the ISO standard. Investigate the
functionality of any extensions the DBMS supports. Are there any functions not
supported?
This is a small student project, the result of which is dependent on the dialect of SQL
being used.
6.30 Demonstrate that queries written using the UNION operator can be rewritten
using the OR operator to produce the same result.
Hint: students should explain a query that can be written in two ways by using UNION
and OR operators.
Description of the data insert can be found in section 6.3.10. Studenst should simulate
data entry by using the provided syntax
Case Study 2
For Exercises 6.32–6.40, use the Projects schema defined in the Exercises at the end
of Chapter 5.
6.32 List all employees from BRICS countries in alphabetical order of surname.
SELECT * FROM Employee WHERE address LIKE ‘%Brazil%’ OR address LIKE
‘%Rusia%’ OR address LIKE ‘%India%’ OR address LIKE ‘%China%’ OR address
LIKE ‘%South Africa%’ ORDER BY lName, fName;
6.34 List all managers who are female in alphabetical order of surname, and then
first name.
6.35 Remove all projects that are managed by the planning department.
WHERE P.projno=w.projno);
SET deptNO=”IT”
WHERE deptNo=”PL”;
6.37 Using the UNION command, list all projects that are managed by the IT and
the HR department.
WHERE deptNo=”IT”
UNION
WHERE deptNo=”HR”;
6.38 Produce a report of the total hours worked by each female employee,
arranged by department number and alphabetically by employee surname within each
department.
AND e.sex=’F’
WHERE P.projno=w.projno);
6.40 List the total number of employees in each department for those departments
with more than 10 employees. Create an appropriate heading for the columns of the
results table.
FROM Employee
GROUP BY deptNo
Case Study 3
For Exercises 6.41–6.54, use the Library schema defined in the Exercises at the end of
Chapter 5.
6.43 List all books titles published between 2010 and 2014.
SELECT *
FROM book
6.44 Remove all books published before 1950 from the database.
6.45 List all book titles that have never been borrowed by any borrower.
SELECT *
FROM BOOK
WHERE L.copyno=C.copyno);
6.46 List all book titles that contain the word ‘database’ and are available for loan.
SELECT b.ISBN, b.title, b.edition, b.year, c.available
SELECT borrowerName
WHERE b.ISBN=c.ISBN
Group by b.title;
SELECT COUNT(copyNo)
FROM BookCopy
SELECT COUNT(*)
6.51 Produce a report of book titles that have been borrowed by “Peter Bloomfield”.
6.52 For each book title with more than 3 copies, list the names of library members
who have borrowed them.
GROUP BY bc1.ISBN
HAVING COUNT(bc1.copyNo) > 3);
6.53 Produce a report with the details of borrowers who currently have books
overdue.
6.54 Produce a report detailing how many times each book title has been borrowed.
GROUP BY ISBN;
Review Questions
The key SQL DDL statements are CREATE, ALTER and DROP. These statements are
used for creating, modifying, and destroying database objects. See Section 7.3 for a
detailed description.
7.2 Discuss the functionality and importance of the Integrity Enhancement Feature
(IEF).
DOMAIN.
TABLE.
(CREATE) ASSERTION.
• SELECT
• INSERT
• DELETE
• UPDATE
• REFERENCE
• USAGE
ISO standard specifies the views that must be updatable in a system that conforms to
the standard. Definition given in SQL standard is that a view is updatable if and only if:
• DISTINCT is not specified; that is, duplicate rows must not be eliminated from the
query results.
• Every element in the SELECT list of the defining query is a column name (rather
than a constant, expression, or aggregate function) and no column appears more
than once.
• The FROM clause specifies only one table; that is, the view must have a single
source table for which the user has the required privileges. If the source table is
itself a view, then that view must satisfy these conditions. This, therefore,
excludes any views based on a join, union (UNION), intersection (INTERSECT),
or difference (EXCEPT).
• The WHERE clause does not include any nested SELECTs that reference the
table in the FROM clause.
• There is no GROUP BY or HAVING clause in the defining query.
In addition, every row that is added through the view must not violate the integrity
constraints of the base table (Section 7.4.5).
7.7 What is a materialized view and what are the advantages of a maintaining a
materialized view rather than using the view resolution process?
Materialized view is a temporary table that is stored in the database to represent a view,
which is maintained as the base table(s) are updated.
7.8 Describe the difference between discretionary and mandatory access control.
What type of control mechanism does SQL support.
Discretionary – each user is given appropriate access rights (or privileges) on specific
database objects.
Mandatory – each database object is assigned a certain classification level (e.g. Top
Secret, Secret, Confidential, Unclassified) and each subject (e.g. user, application) is
given a designated clearance level (Top Secret > Secret > Confidential > Unclassified).
Each object has an owner. Initially, only owner has access to an object but the owner
can pass privileges to carry out certain actions on to other users via the GRANT
statement and take away given privileges using REVOKE.
Exercises
Answer the following questions using the relational schema from the Exercises at the
end of Chapter 4.
7.10 Create the Hotel table using the integrity enhancement features of SQL.
7.11 Now create the Room, Booking, and Guest tables using the integrity
enhancement features of SQL with the following constraints:
CONSTRAINT RoomBooked
FROM Booking b
WHERE b.dateTo > Booking.dateFrom AND
b.hotelNo = Booking.hotelNo)),
CONSTRAINT GuestBooked
FROM Booking b
b.guestNo = Booking.guestNo)));
7.12 Create a separate table with the same structure as the Booking table to hold
archive records. Using the INSERT statement, copy the records from the Booking table
to the archive table relating to bookings before 1st January 2013. Delete all bookings
before 1st January 2013 from the Booking table.
7.13 Assume that all hotels are loaded. Create a view containing the cheapest
hotels in the world.
7.14 Create a view containing the guests who are from BRICS countries.
AS Select *
FROM Guest
ORDER BY guestName;
7.15 Give the users Manager and Deputy full access to these views, with the
privilege to pass the access on to other users.
7.16 Give the user Accounts SELECT access to these views. Now revoke the
access from this user.
GROUP BY h.hotelNo;
For each of the following queries, state whether the query is valid and for the valid ones
should how each of the queries would be mapped onto a query on the underling base
tables.
(a) SELECT *
FROM HotelBookingCount;
GROUP BY h.hotelNo;
FROM HotelBookingCount
SELECT h.hotelNo
h.hotelNo = ‘H001’
GROUP BY h.hotelNo;
FROM HotelBookingCount;
FROM HotelBookingCount;
FROM HotelBookingCount
FROM HotelBookingCount
ORDER BY bookingCount;
GROUP BY h.hotelNo
ORDER BY bookingCount;
and a view SupplierParts, which contains the distinct part numbers that are supplied by
at least one supplier:
Discuss how you would maintain this as a materialized view and under what
circumstances you would be able to maintain the view without having to access the
underlying base tables Part and Supplier.
7.20 Analyze three different DBMSs of your choice. Identify objects that are
available in the system catalog. Compare and contrast the object organization, name
scheme, and the ways used to retrieve object description.
Upon successful analysis, student will be conversant with data dictionary and catalog
found in different DBMSs. The report should indicate the student’s recommended
DBMS.
7.21 Create the DreamHome rental database schema defined in Section 4.2.6 and
insert the tuples shown in Figure 4.3.
This is a small student project, the result of which is dependent on the DBMS being
used.
7.22 Use the view you created in exercise 7.13 to discuss how you would improve
the performance of the SQL command
The SQL statement: ALTER TABLE hotel Add column address varchar (50) not null;
Indexing is useful for attributes that are frequently retrieved; not for those used for data
insertion
Case Study 2
7.24 Create the Projects schema using the integrity enhancement features of SQL
with the following constraints:
(b) position must be one of ‘Manager’, ‘Team Leader’, ‘Analyst’, or ‘Software Developer’.
address Address,
ORDER BY projNo
7.26 Create a view consisting of the attributes empNo, fName, lName, projName,
and hoursWorked attributes.
(a) SELECT *
FROM EmpProject;
FROM EmpProject
FROM EmpProject
FROM EmpProject
GROUP BY empNo;
General
which represents the cost negotiated under each contract for a part (a part may have a
different price under each contract). Now consider the following view ExpensiveParts,
which contains the distinct part numbers for parts that cost more than £1000:
FROM Part
Discuss how you would maintain this as a materialized view and under what
circumstances you would be able to maintain the view without having to access the
underlying base table Part.
If a row is inserted into Part with a partCost less than or equal to £1000, the view would
not have to be updated. If a partNo is inserted into Part that is already in the view, no
new record has to be inserted into the view (because of the DISTINCT keyword).
Similarly for update. If a partNo is deleted from Part have to access the underlying base
table to check if there is another partNo with same value, to determine whether row
should be deleted from the view.
8.1 Advanced SQL deals with SQL/PSM and PL/SQL. What led to the introduction
of SQL/PSM?
Section 8.1 provides history behind SQL/PSM introduction. The initial vertions of SQL
had no programming constructs. SQL/PSM is an extension to SQL that supports the
declaration of variables , has assignment statements and flow of control statements (IF-
THEN ELSE-END IF; LOOP-EXIT WHEN-END LOOP; FOR-END LOOP; WHILE-END
LOOP), and exceptions.
8.3 Describe the control statements in PL/SQL. Give examples to illustrate your
answers.
Conditional IF statement
salary := salary*1.05;
ELSE
salary := salary*1.03;
END IF;
CASE lowercase(x)
y := 0;
x:=1;
myLoop:
LOOP
x := x+1;
IF (x > 3) THEN
y := 2;
DECLARE
numberOfStaff NUMBER;
…..
END LOOP
myLoop1;
8.4 Describe how the PL/SQL statements differ from the SQL standard. Give
examples to illustrate your answers.
There are two versions of PL/SQL: one is part of the Oracle server, and the other is a
separate engine embedded in a number of Oracle tools. They are very similar toeach
other and have the same programming constructs, syntax, and logic mechanisms,
although PL/SQL for Oracle tools has some extensions to suit the requirementsof the
particular tool (for example, PL/SQL has extensions for Oracle Forms).
PL/SQL has concepts similar to modern programming languages, such as variable and
constant declarations, control structures, exception handling, and modularization.
PL/SQL is a block-structured language: blocks can be entirely separate or nested within
one another. The basic units that constitute a PL/SQL program are procedures,
functions, and anonymous (unnamed) blocks.
8.5 What are SQL cursors? Give an example of the use of an SQL cursor.
PL/SQL uses cursors to allow the rows of a query result to be accessed one at a time.
In effect, the cursor acts as a pointer to a particular row of the query result. The cursor
can be advanced by 1 to access the next row. A cursor must
be declared and opened before it can be used, and it must be closed to deactivate it
after it is no longer required. Once the cursor has been opened, the rows of the query
result can be retrieved one at a time using a FETCH statement, as opposed to a
SELECT statement.