Ejercicios Capitulo 4
Ejercicios Capitulo 4
Practice Exercises
4.1 Consider the following SQL query that seeks to find a list of titles of all courses
taught in Spring 2017 along with the name of the instructor.
has a null value for C but a non-null value for D? Explain why or why not.
4.5 Testing SQL queries: To test if a query specified in English has been correctly
written in SQL, the SQL query is typically executed on multiple test databases,
and a human checks if the SQL query result on each test database matches the
intention of the specification in English.
a. In Section 4.1.1 we saw an example of an erroneous SQL query which was
intended to find which courses had been taught by each instructor; the
query computed the natural join of instructor, teaches, and course, and as
a result it unintentionally equated the dept name attribute of instructor and
course. Give an example of a dataset that would help catch this particular
error.
b. When creating test databases, it is important to create tuples in referenced
relations that do not have any matching tuple in the referencing relation
for each foreign key. Explain why, using an example query on the univer-
sity database.
c. When creating test databases, it is important to create tuples with null
values for foreign-key attributes, provided the attribute is nullable (SQL
allows foreign-key attributes to take on null values, as long as they are not
part of the primary key and have not been declared as not null). Explain
why, using an example query on the university database.
Hint: Use the queries from Exercise 4.2.
4.6 Show how to define the view student grades (ID, GPA) giving the grade-point
average of each student, based on the query in Exercise 3.2; recall that we used
a relation grade points(grade, points) to get the numeric points associated with
a letter grade. Make sure your view definition correctly handles the case of null
values for the grade attribute of the takes relation.
4.7 Consider the employee database of Figure 4.12. Give an SQL DDL definition
of this database. Identify referential-integrity constraints that should hold, and
include them in the DDL definition.
178 Chapter 4 Intermediate SQL
4.8 As discussed in Section 4.4.8, we expect the constraint “an instructor cannot
teach sections in two different classrooms in a semester in the same time slot”
to hold.
a. Write an SQL query that returns all (instructor, section) combinations that
violate this constraint.
b. Write an SQL assertion to enforce this constraint (as discussed in Sec-
tion 4.4.8, current generation database systems do not support such as-
sertions, although they are part of the SQL standard).
4.9 SQL allows a foreign-key dependency to refer to the same relation, as in the
following example:
Here, employee ID is a key to the table manager, meaning that each employee
has at most one manager. The foreign-key clause requires that every manager
also be an employee. Explain exactly what happens when a tuple in the relation
manager is deleted.
4.10 Given the relations a(name, address, title) and b(name, address, salary), show
how to express a natural full outer join b using the full outer-join operation with
an on condition rather than using the natural join syntax. This can be done using
the coalesce operation. Make sure that the result relation does not contain two
copies of the attributes name and address and that the solution is correct even
if some tuples in a and b have null values for attributes name or address.
4.11 Operating systems usually offer only two types of authorization control for data
files: read access and write access. Why do database systems offer so many kinds
of authorization?
4.12 Suppose a user wants to grant select access on a relation to another user. Why
should the user include (or not include) the clause granted by current role in the
grant statement?
4.13 Consider a view v whose definition references only relation r.
• If a user is granted select authorization on v, does that user need to have
select authorization on r as well? Why or why not?
• If a user is granted update authorization on v, does that user need to have
update authorization on r as well? Why or why not?
Exercises 179
Exercises
select course id, semester, year, sec id, avg (tot cred)
from takes natural join student
where year = 2017
group by course id, semester, year, sec id
having count (ID) >= 2;
Explain why appending natural join section in the from clause would not change
the result.
4.15 Rewrite the query
select *
from section natural join classroom
without using a natural join but instead using an inner join with a using condi-
tion.
4.16 Write an SQL query using the university schema to find the ID of each student
who has never taken a course at the university. Do this using no subqueries and
no set operations (use an outer join).
4.17 Express the following query in SQL using no subqueries and no set operations.
select ID
from student
except
select s id
from advisor
where i ID is not null
4.18 For the database of Figure 4.12, write a query to find the ID of each employee
with no manager. Note that an employee may simply have no manager listed or
may have a null manager. Write your query using an outer join and then write
it again using no outer join at all.
4.19 Under what circumstances would the query
180 Chapter 4 Intermediate SQL
select *
from student natural full outer join takes
natural full outer join course
Further Reading
General SQL references were provided in Chapter 3. As noted earlier, many systems
implement features in a non-standard manner, and, for that reason, a reference specific
to the database system you are using is an essential guide. Most vendors also provide
extensive support on the web.
The rules used by SQL to determine the updatability of a view, and how updates
are reflected on the underlying database relations appeared in SQL:1999 and are sum-
marized in [Melton and Simon (2001)].
The original SQL proposals for assertions date back to [Astrahan et al. (1976)],
[Chamberlin et al. (1976)], and [Chamberlin et al. (1981)].
Bibliography