0% found this document useful (0 votes)
18 views5 pages

Ejercicios Capitulo 4

Chapter 4 of the document focuses on intermediate SQL concepts, including practice exercises that cover various SQL queries and operations such as joins, outer joins, and subqueries. It also discusses testing SQL queries, defining views, and understanding authorization controls in database systems. Additionally, the chapter emphasizes the importance of integrity constraints and provides further reading and bibliographic references for deeper understanding.

Uploaded by

njkjkjnjk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

Ejercicios Capitulo 4

Chapter 4 of the document focuses on intermediate SQL concepts, including practice exercises that cover various SQL queries and operations such as joins, outer joins, and subqueries. It also discusses testing SQL queries, defining views, and understanding authorization controls in database systems. Additionally, the chapter emphasizes the importance of integrity constraints and provides further reading and bibliographic references for deeper understanding.

Uploaded by

njkjkjnjk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

176 Chapter 4 Intermediate SQL

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.

select name, title


from instructor natural join teaches natural join section natural join course
where semester = 'Spring' and year = 2017

What is wrong with this query?


4.2 Write the following queries in SQL:
a. Display a list of all instructors, showing each instructor’s ID and the num-
ber of sections taught. Make sure to show the number of sections as 0 for
instructors who have not taught any section. Your query should use an
outer join, and should not use subqueries.
b. Write the same query as in part a, but using a scalar subquery and not
using outer join.
c. Display the list of all course sections offered in Spring 2018, along with
the ID and name of each instructor teaching the section. If a section has
more than one instructor, that section should appear as many times in
the result as it has instructors. If a section does not have any instructor,
it should still appear in the result with the instructor name set to “— ”.
d. Display the list of all departments, with the total number of instructors
in each department, without using subqueries. Make sure to show depart-
ments that have no instructors, and list those departments with an instruc-
tor count of zero.
4.3 Outer join expressions can be computed in SQL without using the SQL outer
join operation. To illustrate this fact, show how to rewrite each of the following
SQL queries without using the outer join expression.
a. select * from student natural left outer join takes
b. select * from student natural full outer join takes
4.4 Suppose we have three relations r(A, B), s(B, C), and t(B, D), with all attributes
declared as not null.
a. Give instances of relations r, s, and t such that in the result of
(r natural left outer join s) natural left outer join t
attribute C has a null value but attribute D has a non-null value.
b. Are there instances of r, s, and t such that the result of
r natural left outer join (s natural left outer join t)
Exercises 177

employee (ID, person name, street, city)


works (ID, company name, salary)
company (company name, city)
manages (ID, manager id)

Figure 4.12 Employee database.

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:

create table manager


(employee ID char(20),
manager ID char(20),
primary key employee ID,
foreign key (manager ID) references manager(employee ID)
on delete cascade )

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

• Give an example of an insert operation on a view v to add a tuple t that is


not visible in the result of select * from v. Explain your answer.

Exercises

4.14 Consider the query

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

include tuples with null values for the title attribute?


4.20 Show how to define a view tot credits (year, num credits), giving the total number
of credits taken in each year.
4.21 For the view of Exercise 4.18, explain why the database system would not allow
a tuple to be inserted into the database through this view.
4.22 Show how to express the coalesce function using the case construct.
4.23 Explain why, when a manager, say Satoshi, grants an authorization, the grant
should be done by the manager role, rather than by the user Satoshi.
4.24 Suppose user A, who has all authorization privileges on a relation r, grants select
on relation r to public with grant option. Suppose user B then grants select on r
to A. Does this cause a cycle in the authorization graph? Explain why.
4.25 Suppose a user creates a new relation r1 with a foreign key referencing another
relation r2. What authorization privilege does the user need on r2? Why should
this not simply be allowed without any such authorization?
4.26 Explain the difference between integrity constraints and authorization con-
straints.

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

[Astrahan et al. (1976)] M. M. Astrahan, M. W. Blasgen, D. D. Chamberlin, K. P. Eswaran,


J. N. Gray, P. P. Griffiths, W. F. King, R. A. Lorie, P. R. McJones, J. W. Mehl, G. R. Putzolu,
I. L. Traiger, B. W. Wade, and V. Watson, “System R, A Relational Approach to Data Base

You might also like