CS 386/586 Winter 2012 – Practice – Basic SQL
Work through the
exercises
|
Teacher table: |
|||
|
Number |
Name |
Office |
|
|
1 |
Lois |
O12 |
|
|
2 |
James |
O14 |
|
|
3 |
Sally |
O55 |
|
|
Course table: |
||
|
Number |
Name |
Description |
|
386 |
Intro to Databases |
Introduction to relational database management systems |
|
161 |
Intro to Computer Science 1 |
Introduction to problem-solving, algorithm and program design |
|
Can-teach table: |
|
|
Course |
Teacher |
|
386 |
1 |
|
386 |
2 |
|
161 |
2 |
|
2 |
James |
O14 |
from the Teacher table.
|
386 |
Intro to Databases |
Introduction to relational database management systems |
from the Course table.
|
386 |
2 |
from the Can-teach table.
|
1 |
Lois |
O12 |
|
|
2 |
James |
O14 |
|
|
3 |
Sally |
O55 |
6. SELECT *
FROM Teacher, Can-teach;
|
Number |
Name |
Office |
|
Course |
Teacher |
|
1 |
Lois |
O12 |
386 |
1 |
|
|
1 |
Lois |
O12 |
386 |
2 |
|
|
1 |
Lois |
O12 |
161 |
2 |
|
|
2 |
James |
O14 |
386 |
1 |
|
|
2 |
James |
O14 |
386 |
2 |
|
|
2 |
James |
O14 |
161 |
2 |
|
|
3 |
Sally |
O55 |
386 |
1 |
|
|
3 |
Sally |
O55 |
386 |
2 |
|
|
3 |
Sally |
O55 |
161 |
2 |
7. SELECT *
FROM Teacher, Can-teach
WHERE Teacher.Number
= Can-teach.Teacher;
|
Number |
Name |
Office |
|
Course |
Teacher |
|
1 |
Lois |
O12 |
386 |
1 |
|
|
2 |
James |
O14 |
386 |
2 |
|
|
2 |
James |
O14 |
161 |
2 |
8. SELECT *
FROM Teacher, Can-teach
WHERE Can-teach.Course < 300 AND Teacher.Number
= Can-teach.Teacher;
|
Number |
Name |
Office |
|
Course |
Teacher |
|
2 |
James |
O14 |
161 |
2 |
9. SELECT *
FROM Teacher T,
Can-teach CT, Course C
WHERE T.Number
= CT.Teacher AND CT.Course
= C.Number;
|
Number |
Name |
Office |
|
Course |
Teacher |
Number |
Name |
Description |
|
1 |
Lois |
O12 |
386 |
1 |
386 |
Intro to Databases |
Introduction to relational database management systems |
|
|
2 |
James |
O14 |
386 |
2 |
386 |
Intro to Databases |
Introduction to relational database management systems |
|
|
2 |
James |
O14 |
161 |
2 |
161 |
Intro to Computer Science 1 |
Introduction to problem-solving, algorithm and program design |
10.
SELECT T.Name
as Instructor, C.Name as CourseTitle
FROM Teacher T,
Can-teach CT, Course C
WHERE T.Number
= CT.Teacher AND CT.Course
= C.Number;
|
Instructor |
CourseTitle |
|
Lois |
Intro to Databases |
|
James |
Intro to Databases |
|
James |
Intro to Computer Science 1 |
Write a
relational algebra query that is equivalent to each of the following SQL
queries:
12. SELECT
DISTINCT Name
FROM Teacher
WHERE
Number > 1;
πName(σNumber > 1Teacher)
13. SELECT *
FROM Teacher,
Can-teach;
Teacher X Can-Teach
14. SELECT
*
FROM Teacher,
Can-teach
WHERE Teacher.Number
= Can-teach.Teacher;
σTeacher.Number = Can-teach.Teacher(Teacher X Can-teach)
or
Teacher ⋈Teacher.Number = Can-teach.Teacher Can-teach
15. SELECT
*
FROM Teacher,
Can-teach
WHERE Can-teach.Course
< 300 AND Teacher.Number = Can-teach.Teacher;
σCan-teach.Course < 300 (σTeacher.Number
= Can-teach.Teacher(Teacher X Can-teach))
or
σTeacher.Number = Can-teach.Teacher Ů
Can-teach.Course<300(Teacher X Can-teach)
17. Consider the following tables to describe
apartments and roommates.
Apartment(id, address, num-bedrooms, num-baths)
Person(id, name, gender, age)
Lease(apt-id, renter-id, year)
where
apt-id REFERENCES Apartment.id and renter-id REFERENCES Person.id
We assume that each lease lasts for one year and that every lease starts on
January 1 and ends on December 31.
a. Provide sample data that demonstrates that
one person can hold a lease for more than one apartment in the same year. Make sure that your sample data upholds the
keys and foreign keys shown. (You may
think that this is odd or undesirable – but provide sample data that upholds
the keys and foreign keys shown that allows a person to rent more than one
apartment in the same year.)
b. Provide sample data that demonstrates that
an apartment can have more than one person on a lease in a given year. Make sure that your sample data upholds the
keys and foreign keys shown.
|
Apartment table: |
|||
|
id |
address |
num-bedrooms |
num-baths |
|
10 |
333 Main St. |
2 |
2 |
|
11 |
501 North St. |
2 |
1 |
|
|
|
|
|
|
Person table: |
|||
|
id |
name |
gender |
age |
|
100 |
John |
male |
21 |
|
101 |
Mary |
female |
23 |
|
|
|
|
|
|
Lease table: |
||
|
apt-id |
renter-id |
year |
|
10 |
100 |
2012 |
|
11 |
100 |
2012 |
|
10 |
101 |
2012 |
1. Here we see that John is a person and he is able
to have two rows in the Lease table – one for apartment 10 and one for
apartment 11.
2. Here we see that John and Mary are both on the least
for apartment 10 for the year 2012.”
Notice that it is impossible for a given person, say John, to have a lease for
the same apartment in two different years.
We could not insert a row into the lease table for (10, 100, 2013), for
example, because it would violate the key for the Lease table.
18. Consider these three tables; the Lease
table has a different key compared to question 19.
Apartment(id, address, num-bedrooms, num-baths)
Person(id, name, gender, age)
Lease(apt-id, renter-id, year)
where
apt-id REFERENCES Apartment.id and renter-id REFERENCES Person.id
a. How many leases can one person have in a
given year, say 2012? (That is, how many
rows can there be in the Lease table for a particular person in a particular
year?)
Each person can have only one lease in a given year
because (renter-id, year) is the key for the Lease table. So we must eliminate one of the rows for John
for year 2012. I crossed out one below.
|
Apartment
table: |
|||
|
id |
address |
num-bedrooms |
num-baths |
|
10 |
333 Main St. |
2 |
2 |
|
11 |
501 North St. |
2 |
1 |
|
|
|
|
|
|
Person table: |
|||
|
id |
name |
gender |
age |
|
100 |
John |
male |
21 |
|
101 |
Mary |
female |
23 |
|
|
|
|
|
|
Lease table: |
||
|
apt-id |
renter-id |
year |
|
10 |
100 |
2012 |
|
|
|
|
|
10 |
101 |
2012 |